Home > OS >  How to resize the image so that results can be seen clearly
How to resize the image so that results can be seen clearly

Time:12-29

In this code i am detecting udder part of the cow and it shows a bounding box around the udder and also shows the accuracy and detection class but image result size is very small i cannot see the result.

    import cv2 
    import numpy as np
    from matplotlib import pyplot as plt
    %matplotlib inline

    category_index = label_map_util.create_category_index_from_labelmap(files['LABELMAP'])
    
    img = cv2.imread(IMAGE_PATH)
    image_np = np.array(img)
    
    input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
    detections = detect_fn(input_tensor)
    
    num_detections = int(detections.pop('num_detections'))
    detections = {key: value[0, :num_detections].numpy()
                  for key, value in detections.items()}
    detections['num_detections'] = num_detections
    
    # detection_classes should be ints.
    detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
    
    label_id_offset = 1
    image_np_with_detections = image_np.copy()
    
    viz_utils.visualize_boxes_and_labels_on_image_array(
                image_np_with_detections,
                detections['detection_boxes'],
                detections['detection_classes'] label_id_offset,
                detections['detection_scores'],
                category_index,
                use_normalized_coordinates=True,
                max_boxes_to_draw=5,
                min_score_thresh=.8,
                agnostic_mode=False)
    
    plt.imshow(cv2.cvtColor(image_np_with_detections, cv2.COLOR_BGR2RGB))
    plt.show()

In this image i am detecting udder part of cow

CodePudding user response:

Are you required to use matplotlib? Using pillow and plotly, you can highlight the area of interest like below. You can zoom in interactively and show the accuracy e.g. on hovering over a bounding box.

import PIL.Image
import plotly.express as px
import plotly.graph_objects as go

img = PIL.Image.open('l3nQn.png')

# Define bbox for drawing 
left = 105
top = 81
height = 72
width = 35
# Define accuracy for hover text
accuracy = .98

# Draw image
fig = px.imshow(img)
fig.update_xaxes(visible=False, range=[0, img.size[0]])

# Draw bounding box in separate trace
bbox_trace = go.Scatter(
    x=(left width, left, left, left width, left width),
    y=(top, top, top height, top height, top),
    fill='toself',
    name=f'acc={accuracy}',
)

fig.add_trace(bbox_trace)
fig.show()

Bounding box with plotly. Hovering over it will show the accuracy

CodePudding user response:

This happens because your original image is wery large. If you really want to resize your image, you should do it at the begining of your code, before usiong your detection model. You can use cv2.resize. For exambple to divide the original image size by 4 : resized = cv2.resize(img, (width//4, height//4), interpolation = cv2.INTER_LINEAR)

But you may prefer have a larger text rather than resizing the image itself. In this case, maybe try this, using only matplotlib and cv2 and not the "viz_utils" from object detection. You can change the size, the offset, the color (etc.) of the text in the options of cv2.putText.

import cv2
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

def detect_and_classify_udder(file):
    image = cv2.imread(file, 3)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    input_tensor = tf.convert_to_tensor(image)
    # input_tensor = input_tensor[tf.newaxis,...]  # if needed
    detections_dict = detect_fn(input_tensor) # detect_fn is your detection model
    
    num_detections = int(detections_dict.pop('num_detections'))
    detections_dict = {key:value[0, :num_detections].numpy() for key,value in detections_dict.items()}
    
    scores = np.array(detections_dict['detection_scores'])
    
    boxes = detections_dict['detection_boxes']
    # add a threshold under which the box is not drawn
    real_boxes = boxes[np.where(scores > 0.8)]
    real_scores = scores[np.where(scores > 0.8)]
    
    real_boxes_scaled =[]

    for box in real_boxes :
        x1 = int(image.shape[1]*box[1])
        x2 = int(image.shape[1]*box[3])
        y1 = int(image.shape[0]*box[0])
        y2 = int(image.shape[0]*box[2])

        real_boxes_scaled.append([x1, x2, y1, y2])
        

    for box, score in zip(real_boxes_scaled, real_scores) :
        x1, x2, y1, y2 = box 
        # draw the rectangle with the coordinates
        image = cv2.rectangle(image, (x1, y1), (x2, y2), (255, 255, 0), 5) 
        # add caption with putText
        image = cv2.putText(image, score, (x1-22, y1-16), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,255,0), 2)
    plt.figure(figsize=(8, 8))
    plt.axis("off")
    plt.imshow(image)

putText usage : cv2.putText(image, text, origin, fontType, fontScale, Color, Thickness)

  • Related