【问题标题】:Tensorflow Object detection API: Print detected class as outputTensorflow 对象检测 API:将检测到的类打印为输出
【发布时间】:2021-05-08 18:43:31
【问题描述】:

我正在使用 TF 对象检测 API 来检测图像,它工作正常,并且给定图像,它将绘制带有标签和置信度分数的边界框。我的问题是如何打印检测到的类(作为字符串),即不仅在图像上,而且作为终端的输出。

这是实时检测代码。

cap = cv2.VideoCapture(0)  
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))  
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))    

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



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

if cv2.waitKey(1) & 0xFF == ord('q'):
    cap.release()
    break

【问题讨论】:

标签: python tensorflow object-detection


【解决方案1】:

Classes 将被加密到category_index 变量中。使用下面的代码 sn -p 来获取检测到的类。

print ([category_index.get(value) for index,value in enumerate(classes[0]) if scores[0,index] > 0.5])

【讨论】:

    猜你喜欢
    • 2021-06-30
    • 2021-12-05
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 2018-01-17
    • 1970-01-01
    • 2023-01-07
    相关资源
    最近更新 更多