在实时物体检测器中保存图像

问题描述 投票:0回答:1

我目前正在使用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

我该如何实现?还有其他变化吗?

tensorflow object-detection object-detection-api mobilenet
1个回答
0
投票

session.run之后,您将获得以下结果(框,分数,类,num_detections)

您只需要遍历它们并查看课程和分数,最后保存]

© www.soinside.com 2019 - 2024. All rights reserved.