【问题标题】:Print class name from tensorflow object detection api从 tensorflow 对象检测 api 打印类名
【发布时间】:2021-06-30 19:41:48
【问题描述】:

请注意:我已经尝试了网络上的其他解决方案,但没有找到有效的结果。 我正在使用 tensorflow 对象检测 api 和 opencv 从实时提要中检测对象。它工作正常并在屏幕上显示框。现在我只想在终端上打印检测到的对象。 我尝试了各种解决方案,但找不到任何有效的方法。我是新来的。 这是我的代码:

while True:
    ret, frame = cap.read()
image_np = np.array(frame)

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=.5,
    agnostic_mode=False)

cv2.imshow('object detection',  cv2.resize(
    image_np_with_detections, (800, 600)))

我尝试使用:

print(detections['detection_classes'])

但这只是打印一组值。任何帮助将不胜感激

【问题讨论】:

  • 不清楚您的目标是什么,或者您发布的代码有什么问题。
  • 我要打印检测到的类

标签: python tensorflow machine-learning object-detection object-detection-api


【解决方案1】:

category_index 是带有您的标签名称的字典。使用detections['detection_classes'] 对其进行索引以获取类别的名称。像这样的东西,但这行不通,因为我直接在这里写,没有测试:

label_names = [i['name'] for i in category_index.items()]
label_names = np.array(label_names)

print(label_names[detections['detection_classes'].numpy()])

【讨论】:

    猜你喜欢
    • 2021-12-05
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 2022-10-08
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 2017-12-02
    相关资源
    最近更新 更多