opencv Tensorflow对象检测API:打印检测到的对象标签名称

yk9xbfzb  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(132)

我正在学习Nicollas renotte关于使用TensorFlow和OpenCV进行实时手势检测的教程,并完成了代码。

import cv2

import numpy as np

import time

category_index = label_map_util.create_category_index_from_labelmap(ANNOTATION_PATH+'/label_map.pbtxt')

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=5,
                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
cap.release()

detections = detect_fn(input_tensor)

因此,这段代码运行良好,可以识别手势,并在手势周围绘制一个框,然后标记它,但我希望在终端本身中打印已识别手势的名称(用于与pyttx3一起使用,以说出检测到的信号)我尝试只打印检测结果['detection_classes']但它只给予了某种数组作为输出,有人能解释一下我如何打印出用分数检测到对象的名称吗?
提前感谢,堆栈溢出的第一篇文章,所以请对我轻点

ffvjumwh

ffvjumwh1#

detections['detection_classes']返回检测到的每个边界框的类别id。
类别索引是将整数idMap到包含类别的字典的字典,例如{1: {'id': 1, 'name': 'dog'}, 2: {'id': 2, 'name': 'cat'}, ...}
因此,如果打印category_index,则会得到如下结果:

{1: {'id': 1, 'name': 'Aa'}, 2: {'id': 2, 'name': 'Bb'}, ...}

假设您要处理的是字母的手势。
有了这些知识,就很容易打印出检测到的手势的标签。

# flatten the category_index to a single dictionary
category_dict = {value.get('id'):value.get('name') for _,value in category_index.items()}
detected_signs = []
for sign_index in detections['detection_classes']:
   sign_label = category_dict.get(sign_index)
   detected_signs.append(sign_label)
print(detected_signs)
# Feed detected_signs to downstream system like pyttx3 to speak out the sign

相关问题