How Can I Print Number Instead Of Class Label And Score On Image With Object Detection

- 1 answer

I am a student and learning deep learning. I am having a project and using object_detection_tutorial (code below) to detect diseases of rice plant. I want to print number of position where have diseases instead of class label and score (like image below), but i don't know how. So i really need help to solve this. Thanks you.

with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
        detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
        detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
        detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
        num_detections = detection_graph.get_tensor_by_name('num_detections:0')
        for i in TEST_IMAGE_PATHS:
            image =
            image_np = load_image_into_numpy_array(image)
            image_np_expanded = np.expand_dims(image_np, axis=0)
            (boxes, scores, classes, num) =
                 [detection_boxes, detection_scores, detection_classes, num_detections],
                 feed_dict={image_tensor: image_np_expanded})
            cv2.imshow("image_np", image_np)

i want to print image like below image_result:




First of all this has little to do with openCV and it's probably tensorflow code.

If I am not mistaken the code uses this function from here. Running the above code it returns (boxes, scores, classes, num) which correspond to the bounding boxes along with the corresponding confidence score and class id and the number of detection (which is not really helpful in your case).

Assuming (a you say that dao_on is the class name) that the displayed message contains the class name and the score I am guessing you are thrown to line:

display_str = str(class_name)

Anyway, the easiest way for you would be to copy this function, visualize_boxes_and_labels_on_image_array() (it's auxiliary and it's not actual tensorflow) and replace all code referring to the displayed string with the string you want. That is the number of objects being detected I guess? If you have just one class then:

for i in range(min(max_boxes_to_draw, boxes.shape[0])):
    # delete all code in the block
    display_str = i

# Draw all boxes onto image.
  for box, color in box_to_color_map.items():

OpenCV is not really used apart from displaying the image I think.