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:
Answer
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.
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module