opencv 获取类别

wr98u20j  于 2022-11-15  发布在  其他
关注(0)|答案(2)|浏览(123)

我是一个非常实用的土木工程师(非常差的程序员)。我想检测建筑工地上的物体。现在我按照一个教程,我得到了一张图片,上面有一个检测到的物体的边界框。我这里的问题是:我怎样才能得到一个包含类别名称、索引或id的字符串?

使用以下代码:

input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
detections = detect_fn(input_tensor)

num_detections = int(detections.pop('num_detections'))
detections = {key: value[0, :num_detections].numpy()
              for key, value in detections.items()}
detections['num_detections'] = num_detections

# detection_classes should be ints.
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)

label_id_offset = 1
image_np_with_detections = image_np.copy()

viz_utils.visualize_boxes_and_labels_on_image_array(
            image_np_with_detections,
            detections['detection_boxes'],
            detections['detection_classes']+label_id_offset,
            detections['detection_scores'],
            category_index,
            use_normalized_coordinates=True,
            max_boxes_to_draw=5,
            min_score_thresh=.6,
            line_thickness=10,
            keypoint_edges=4,
            agnostic_mode=False)

plt.imshow(cv2.cvtColor(image_np_with_detections, cv2.COLOR_BGR2RGB))
plt.show()
s4n0splo

s4n0splo1#

我有同样的代码,print(category_index[detections['detection_classes'][0]+label_id_offset]["name"])运行得很好

rlcwz9us

rlcwz9us2#

您应该将category_index变数初始化为下列程式码:

PATH_TO_LABELS = './models/research/object_detection/data/mscoco_label_map.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
# category_index is a dictionary as seen below:
# {1: {"id": 1, "name": "category1"}, 2: {"id": 2, "name": "category2"}}

其中models目录指的是git clone --depth 1 https://github.com/tensorflow/models
有关更多帮助,请访问here

相关问题