如何在物体检测的图像中打印数字而不是类别标签和分数

2024-04-24 17:19:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个学习深入的学生。我有一个项目,并使用object\u detection\u教程(代码如下)检测水稻病害。我想打印有疾病的位置编号,而不是类别标签和分数(如下图所示),但我不知道如何打印。所以我真的需要帮助来解决这个问题。谢谢你。你知道吗

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


Tags: nameimagegetbywithnpnumclasses
1条回答
网友
1楼 · 发布于 2024-04-24 17:19:55

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

如果我没有弄错的话,代码使用的是来自here的函数。运行上面的代码,它返回(boxes, scores, classes, num),它对应于边界框,以及相应的置信度、类id和检测次数(这在您的情况下并没有真正的帮助)。你知道吗

假设(您说dao_on是类名)显示的消息包含类名和分数,我猜您将被抛出到第行:

display_str = str(class_name)

无论如何,最简单的方法是复制这个函数visualize_boxes_and_labels_on_image_array()(它是辅助函数,不是实际的tensorflow),并用所需的字符串替换所有引用所显示字符串的代码。我猜这就是被检测到的物体的数量?如果你只有一门课,那么:

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除了显示图像之外并没有真正使用。你知道吗

相关问题 更多 >