我是一个非常实用的土木工程师(非常差的程序员)。我想检测建筑工地上的物体。现在我按照一个教程,我得到了一张图片,上面有一个检测到的物体的边界框。我这里的问题是:我怎样才能得到一个包含类别名称、索引或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()
2条答案
按热度按时间s4n0splo1#
我有同样的代码,
print(category_index[detections['detection_classes'][0]+label_id_offset]["name"])
运行得很好rlcwz9us2#
您应该将
category_index
变数初始化为下列程式码:其中models目录指的是
git clone --depth 1 https://github.com/tensorflow/models
。有关更多帮助,请访问here。