【发布时间】:2018-02-09 08:15:55
【问题描述】:
如何提取视频检测到的对象的类型。例如,一旦对象检测 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
【问题讨论】:
-
目标检测器的输出在
(boxes, scores, classes, num_detections)元组中给出,按照置信度从高到低排序,类信息包含在可以使用标签映射提取的类中(以id为索引)文件
标签: python opencv tensorflow object-detection