python - 我在Open CV中使用TF对象检测API

标签 python opencv tensorflow object-detection

如何提取视频检测到的对象的类型。例如,一旦对象检测API中的视频检测到“笔记本电脑”,如何获取“笔记本电脑”标签及其ID并在单独的文件中显示?

import cv2
 cap = cv2.VideoCapture(0)

 with detection_graph.as_default():
   with tf.Session(graph=detection_graph) as sess:
    ret = True
    while (ret):
       ret,image_np = cap.read()

       image_np_expanded = np.expand_dims(image_np, axis=0)
       image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

       boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
       # Each score represent how level of confidence for each of the objects.
       # Score is shown on the result image, together with the class label.
       scores = detection_graph.get_tensor_by_name('detection_scores:0')
       classes = detection_graph.get_tensor_by_name('detection_classes:0')
       num_detections = detection_graph.get_tensor_by_name('num_detections:0')
       # Actual detection.
       (boxes, scores, classes, num_detections) = sess.run(
           [boxes, scores, classes, num_detections],
           feed_dict={image_tensor: image_np_expanded})
       # Visualization of the results of a detection.
       vis_util.visualize_boxes_and_labels_on_image_array(
           image_np,
           np.squeeze(boxes),
           np.squeeze(classes).astype(np.int32),
           np.squeeze(scores),
           category_index,
           use_normalized_coordinates=True,
           line_thickness=8)

       cv2.imshow('image',cv2.resize(image_np,(600,400)))      

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

最佳答案

假设您有用于标签映射的pbtxt文件,如下所示:

item {
  name: "/m/01g317"
  id: 1
  display_name: "person"
}
item {
  name: "/m/0199g"
  id: 2
  display_name: "bicycle"
}
item {
  name: "/m/0k4j"
  id: 3
  display_name: "car"
}
...

您可以使用 label_map_util [https://github.com/tensorflow/models/blob/master/research/object_detection/utils/label_map_util.py]
label_map = label_map_util.load_labelmap(LABELS_PBTXT_FILE_PATH)

categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=n_classes, use_display_name=True)

idx_to_label = {}
for cat in categories:
    idx_to_label[cat['id']] = cat['name']

然后-当您获得 idx_to_label 字典时,只需使用
idx_to_label.get(curr_id, 'N/A')

关于python - 我在Open CV中使用TF对象检测API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48701372/

相关文章:

python - Python 中格式化程序 %r 和\n 发生了什么?

Python Peewee - 将 Peewee 合并到具有缺少 ID AUTO_INCREMENT 字段的表的现有数据库中

c++ - OpenCV 3.0 中的事件轮廓模型

python - 如何在自定义 Keras 损失中将 softmax 输出转换为 one-hot 格式

python - 选择随机 Action 的 Tensorflow 代理

python - 使用 spacy 对 Pandas Dataframe 中的一列已解析的 html 文本进行词形还原

python - 在 Python 中,如何将字典转换为 df 列,其中键与 df.index 值匹配?

opencv - OpenCV制作错误

c++ - 使用套接字发送 Iplimage

python - TensorFlow:按名称访问变量的内容