簡體   English   中英

我在Open CV中使用TF對象檢測API

[英]I'm using TF Object Detection API with Open CV

如何提取視頻檢測到的對象的類型。例如,一旦對象檢測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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM