pythonobject-detectiontensorflow2.0object-detection-apitensorflow2.x

Print the detected classes and scores when using TF Object Detection API


Hello i'm looking for a way to print out the detected classes and scores while doing an Object Detection with the object_detection_tutorial. Most solutions here are for Tensorflow 1 and do not work anymore.

I found one Solution here on StackOverflow but sadly it only prints out one of the detected Objects. I can't find out how i have to modify the code to get the scores for all detected Objects in the image.

This is the code given in the Solution i found here

def get_classes_name_and_scores(
    boxes,
    classes,
    scores,
    category_index,
    max_boxes_to_draw=20,
    min_score_thresh=.9): # returns bigger than 90% precision
display_str = {}
if not max_boxes_to_draw:
    max_boxes_to_draw = boxes.shape[0]
for i in range(min(max_boxes_to_draw, boxes.shape[0])):
    if scores is None or scores[i] > min_score_thresh:
        if classes[i] in six.viewkeys(category_index):
            display_str['name'] = category_index[classes[i]]['name']
            display_str['score'] = '{}%'.format(int(100 * scores[i]))

return display_str

I print this out using this code

def show_inference(model, image_path):
  # the array based representation of the image will be used later in order to prepare the
  # result image with boxes and labels on it.
  image_np = np.array(Image.open(image_path))
  # Actual detection.
  output_dict = run_inference_for_single_image(model, image_np)
  # Visualization of the results of a detection.
  vis_util.visualize_boxes_and_labels_on_image_array(
    image_np,
    output_dict['detection_boxes'],
    output_dict['detection_classes'],
    output_dict['detection_scores'],
    category_index,
    instance_masks=output_dict.get('detection_masks_reframed', None),
    use_normalized_coordinates=True,
    line_thickness=8)

  # Print the Name and Score of each detected Object
  print(get_classes_name_and_scores(
    output_dict['detection_boxes'],
    output_dict['detection_classes'],
    output_dict['detection_scores'],
    category_index))

  display(Image.fromarray(image_np))

Solution

  • every time through the loop these 2 lines are overwritten so the previous contents are gone:

    display_str['name'] = category_index[classes[i]]['name']
    display_str['score'] = '{}%'.format(int(100 * scores[i]))
    

    I assume you want to record each instance that the loop triggers those lines. Personally, for this I would add the result to a list and return that:

    display_str_list = []
        ### your loop code
        display_str_dict = {
            'name': category_index[classes[i]]['name'],
            'score': '{}%'.format(int(100 * scores[i])),
        }
        display_str_list.append(display_str_dict)
    return display_str_list