Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将帧保存到对象检测中的特定位置_Python_Object Detection Api - Fatal编程技术网

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

Python 将帧保存到对象检测中的特定位置,python,object-detection-api,Python,Object Detection Api,我想应用一个检查,如果某个位置已经存在一个包含某些帧的文件夹,那么它应该抛出一条消息并跳过该文件夹,然后检查是否存在下一个文件夹 我已经制作了脚本来保存具有边界框的帧,并应用了检查文件夹是否存在,如果存在,那么它是否为空。如果未应用该检查,则代码工作正常,并保存检测到对象的所有帧。但当应用check时,所有文件夹中仅保存1帧,并显示错误消息。如何修改代码 def recognize_object(img_name, image): model_name='inference_graph

我想应用一个检查,如果某个位置已经存在一个包含某些帧的文件夹,那么它应该抛出一条消息并跳过该文件夹,然后检查是否存在下一个文件夹

我已经制作了脚本来保存具有边界框的帧,并应用了检查文件夹是否存在,如果存在,那么它是否为空。如果未应用该检查,则代码工作正常,并保存检测到对象的所有帧。但当应用check时,所有文件夹中仅保存1帧,并显示错误消息。如何修改代码

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)
def识别对象(img\u名称、图像):
模型\u name='推断\u图'
路径\u至\u CKPT=“C:\\multi\u cat\u 3\\models\\research\\object\u detection\\expression\u graph\\freezed\u expression\u graph.pb”
指向标签的路径=“C:\\multi\u cat\u 3\\models\\research\\object\u detection\\training\\labelmap.pbtxt”
out=“C:\\multi\u cat\u 3\\models\\research\\object\u detection\\my\u imgs\\\”+“video”+视频名称
os.makedirs(out,exist\u ok=True)
打印(输出)
如果os.path.exists(out)和os.listdir(out)存在:
打印(“已为文件夹video_{0}处理帧”。格式(vid_名称))
通过
其他:
NUM_CLASSES=4
label\u map=label\u map\u util.load\u labelmap(路径到标签)
categories=label\u map\u util。将标签映射转换为类别(label\u map,max\u num\u classes=num\u classes,use\u display\u name=True)
类别索引=标签映射工具。创建类别索引(类别)
检测图=tf.graph()
使用检测图。作为默认值():
od_graph_def=tf.GraphDef()
将tf.gfile.gfile(路径_到_CKPT,'rb')作为fid:
序列化的_图=fid.read()
od_图形_def.ParseFromString(序列化_图形)
tf.import_graph_def(od_graph_def,name='')
sess=tf.Session(图=检测图)
image\u tensor=detection\u graph。通过\u名称(“image\u tensor:0”)获取\u tensor\u
detection\u Box=detection\u graph。通过名称(“detection\u Box:0”)获取张量
detection\u scores=detection\u graph。通过名称获取张量(“detection\u scores:0”)
detection\u classes=detection\u图。通过名称获取张量('detection\u classes:0')
num\u detections=detection\u图。通过名称获取张量(num\u detections:0)
图像\u展开=np。展开\u dims(图像,轴=0)
(框,分数,类,num)=sess.run([检测框,检测分数,检测类,num检测],提要内容={image\u tensor:image\u expanded})
vis_util.visualize_box_和_标签_在_image_数组上(
形象,,
np.挤压(箱),
np.squence(classes).astype(np.int32),
np.挤压(分数),
类别索引,
使用标准化坐标=真,
线厚度=4,
最小得分阈值=0.80,
跳过(分数=真)
坐标=vis_util.return_坐标(
形象,,
np.挤压(箱),
np.squence(classes).astype(np.int32),
np.挤压(分数),
类别索引,
使用标准化坐标=真,
线厚度=4,
最小分数(阈值=0.80)
阈值=0.80
对象=[]
z=0
对于索引,枚举中的值(类[0]):
对象_dict={}
如果分数[0,索引]>阈值:
object_dict[(category_index.get(value)).get('name')]=分数[0,索引]
objects.append(object\u dict)
打印(对象)
while(z)