将帧保存到对象检测中的特定位置

时间:2019-05-16 21:46:55

标签: python object-detection-api

我要进行检查,如果某个位置已经存在带有某些框架的文件夹,那么它应该抛出一条消息并跳过该文件夹并检查下一个文件夹是否存在

我制作了脚本来保存具有边界框的帧,并应用了检查文件夹是否存在以及是否存在文件夹,然后检查该文件夹是否为空。如果未应用检查,则代码可以正常工作,并且保存了检测到对象的所有帧。但是,当应用检查时,所有文件夹中仅保存一帧,并且还会显示错误消息。我该如何修改代码

def recognize_object(img_name, image):

    model_name='inference_graph'
    PATH_TO_CKPT="C:\\multi_cat_3\\models\\research\\object_detection\\inference_graph\\frozen_inference_graph.pb"
    PATH_TO_LABELS="C:\\multi_cat_3\\models\\research\\object_detection\\training\\labelmap.pbtxt"
    out="C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\"+"video_"+vid_name

    os.makedirs(out,exist_ok=True)
    print(out)

    if os.path.exists(out) and os.listdir(out):
        print("Frames already processed for folder video_{0}".format(vid_name))
        pass
    else:

        NUM_CLASSES = 4

        label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        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)

        detection_graph = tf.Graph()

        with detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

            sess = tf.Session(graph=detection_graph)

        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

        detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
        detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
        detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
        num_detections = detection_graph.get_tensor_by_name('num_detections:0')

        image_expanded = np.expand_dims(image, axis=0)

        (boxes, scores, classes, num) = sess.run([detection_boxes, detection_scores, detection_classes, num_detections],feed_dict={image_tensor: image_expanded})

        vis_util.visualize_boxes_and_labels_on_image_array(
        image,
        np.squeeze(boxes),
        np.squeeze(classes).astype(np.int32),
        np.squeeze(scores),
        category_index,
        use_normalized_coordinates=True,
        line_thickness=4,
        min_score_thresh=0.80,
        skip_scores=True)

        coordinates=vis_util.return_coordinates(
        image,
        np.squeeze(boxes),
        np.squeeze(classes).astype(np.int32),
        np.squeeze(scores),
        category_index,
        use_normalized_coordinates=True,
        line_thickness=4,
        min_score_thresh=0.80)

        threshold=0.80

        objects = []
        z = 0
        for index, value in enumerate(classes[0]):
            object_dict = {}
            if scores[0, index] > threshold:
                object_dict[(category_index.get(value)).get('name')] = scores[0, index]
                objects.append(object_dict)
                print(objects)
            while (z<len(objects)):
                print({'File_Name':img_name,'Object_Detected':list(objects[z].keys())[0],'Probability':list(objects[z].values())[0]})
                z=z+1

        cv2.imwrite(out+"/{}.jpg".format(img_name),image)
        cv2.waitKey(0)

如果文件夹具有子文件夹A,B,C,并且在运行代码时未应用检查,则在每个子文件夹内会保存多个检测到对象的帧。但是,尽管在处理每一帧时都会显示该消息,但应用检查时不会发生同样的情况。执行代码后,每个子文件夹只有一帧。我想保存相同的多个帧,即使应用了检查,也保存了不应用检查的情况

0 个答案:

没有答案