0

我目前正在使用 TensorFlow 1.x 中的 SSD MobileNetv2 运行实时对象检测器,并且想知道当视频流检测到其中一个类时是否有任何方法可以保存图像。

PATH_TO_FROZEN_GRAPH = 'path-to-inference-graph.pb'
PATH_TO_LABEL_MAP = 'path-to-label-map.pbtxt'
NUM_CLASSES = 4
cap = cv2.VideoCapture(0)

基本上,我已经构建了检测器来检测 4 个类,并希望在检测到其中一个类时保存图像(也许它可能会以一系列图像的形式出现,仍然可以)。

label_map = label_map_util.load_labelmap(PATH_TO_LABEL_MAP)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)

with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        while True:
            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')
            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')

            (boxes, scores, classes, num_detections) = sess.run(
                [boxes, scores, classes, num_detections],
                feed_dict={image_tensor: image_np_expanded})

            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=3,
                )

            cv2.imshow('Detection', cv2.resize(image_np, (1200, 800)))
            if cv2.waitKey(25) & 0xFF == ord('q'):
                cv2.destroyAllWindows()
                break

我如何实现这一目标?它还有其他变化吗?

4

1 回答 1

0

在 session.run 之后,您会在 (boxes, scores, classes, num_detections) 中获得结果

你只需要遍历它们并查看类和分数,最后保存

if 'req_class_name' in classes:
   #check for confidence score also
   cv2.imwrite('/path/to/destination/image.png', image_np)
于 2020-05-31T13:50:49.583 回答