我如何使用对象检测打印数字而不是 class 标签并在图像上评分

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

我是一名学生,正在学习深度学习。我有一个项目并使用 object_detection_tutorial(下面的代码)来检测水稻病害。我想打印有疾病的位置编号而不是 class 标签和分数( 如下图 ),但我不知道如何。所以我真的需要帮助来解决这个问题。谢谢。

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()

我想打印如下图 image_result:

首先这与openCV关系不大,可能是tensorflow代码。

如果我没记错的话,代码使用了 here 中的这个函数。 运行 上面的代码 returns (boxes, scores, classes, num) 对应于边界框以及相应的置信度分数和 class id 以及检测次数(这对你的情况)。

假设(你说 dao_on 是 class 名字)显示的消息包含 class 名字和我猜你被扔到线的分数:

display_str = str(class_name)

无论如何,对你来说最简单的方法是复制这个函数,visualize_boxes_and_labels_on_image_array()(它是辅助的,它不是真正的 tensorflow)并用你想要的字符串替换所有引用显示字符串的代码。我猜这是检测到的物体数量?如果你只有一个 class 那么:

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除了显示我认为的图像外并没有真正使用