简体   繁体   中英

How can i print number instead of class label and score on image with object detection

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.open(i)
            image_np = load_image_into_numpy_array(image)
            image_np_expanded = np.expand_dims(image_np, axis=0)
            (boxes, scores, classes, num) = sess.run(
                 [detection_boxes, detection_scores, detection_classes, num_detections],
                 feed_dict={image_tensor: image_np_expanded})
            vis_util.visualize_boxes_and_labels_on_image_array(image_np,np.squeeze(boxes),
                                                         np.squeeze(classes).astype(np.int32),
                                                         np.squeeze(scores),
                                                         category_index,
                                                         use_normalized_coordinates=True,
                                                         line_thickness=2)
            cv2.imshow("image_np", image_np)
            cv2.waitKey()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM