不处理已经存在的数据

时间:2019-06-06 08:53:43

标签: python-3.x

我有一个文件夹,其中有一些视频文件。我想从视频中提取帧,但是只有那些名称不在CSV中的视频才应被处理。在处理视频之前,应先检查csv中当前的视频文件名

def extractFrames(m):
    global vid_name

    vid_files=glob(m)
    print(vid_files)
    complete_videos = get_completed_videos()
    print(complete_videos)
    new_vid_files = [x for x in vid_files if get_vid_name(x) not in complete_videos]

    for vid in new_vid_files:
        print("path of video========>>>>.",vid)

        v1=os.path.basename(vid)


        try:
            vid_name = get_vid_name(vid)
            vidcap = cv2.VideoCapture(vid)
        except cv2.error as e:
            print(e)
        except:
            print('error')
        #condition 
        fsize=os.stat(vid)
        print('=============size of video ===================:' , fsize.st_size)
        try:

            if (fsize.st_size > 1000):

                fps = vidcap.get(cv2.CAP_PROP_FPS)      # OpenCV2 version 2 used "CV_CAP_PROP_FPS"
                frameCount = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
                duration = frameCount/fps
                minutes = int(duration/60)
                print('fps = ' + str(fps))
                print('number of frames = ' + str(frameCount))
                print('duration (S) = ' + str(duration))
                if (duration > 1):
                    success,image = vidcap.read()


                    count=0

                    success=True

                    while success:

                        img_name = vid_name + '_f' + str(count) + ".jpg"

                        success,image = vidcap.read()
                        if count % 10 == 0 or count ==0:
                            target_non_target(img_name, image)

                        count+=1          

                    vidcap.release()

                    cv2.destroyAllWindows()
        except:
            print("error")


        print('finished processing video ',vid)
        with open("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\"+'video_info.csv', 'a') as csv_file:
            fieldnames = ['Video_Name','Process']
            file_is_empty = os.stat("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\"+'video_info.csv').st_size == 0
            writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
            if file_is_empty:
                writer.writeheader()
            writer.writerow({'Video_Name':vid_name,'Process':'done'})

def get_vid_name(vid):
    return os.path.splitext(os.path.basename(vid))[0]



def get_completed_videos():
    completed_videos = []
    with open("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\video_info.csv") as csv_file:

        for row in csv.reader(csv_file):
            for col in range(0,len(row)):
                try:
                    completed_videos.append(row[col])
                except Exception as e:
                    print(str(e))
    print(completed_videos[0])
    return completed_videos

假设一个文件夹中有3个视频。这3个视频的代码已成功运行,并且其名称都写在了csv中。现在,如果我将视频编号4粘贴到文件夹中,则在检查csv中存在的视频名称后,它应该仅处理第4个视频。目前,每次运行脚本时,它都会重复处理所有视频文件。

2 个答案:

答案 0 :(得分:1)

首先,在for循环内

v1=os.path.basename(vid_files[v_f])

应该是

v1=os.path.basename(new_vid_files[v_f])

由于您正在new_vid_files范围内循环。使用原始列表上的那些索引将为您提供意外的项目。更好的是,您可以直接使用for-each循环(因为除了列表访问以外,似乎没有使用v_f):

for vid in new_vid_files:

vid将替换new_vid_files[v_f]的所有实例。

接下来,您正在使用vid_name来写入csv,因此在创建{{1时}与vid_files匹配之前,需要对complete_videos中的每个项目执行相同的操作}}列表。

如果您创建一种用于获取视频名称的方法,如下所示:

new_vid_files

然后您可以将列表理解更改为

def get_vid_name(vid_file):
    return os.path.splitext(os.path.basename(vid_file))[0]

编辑:如其他答案的注释中所述,new_vid_files = [x for x in vid_files if get_vid_name(x) not in complete_videos] 的输出表明未正确解析它。它同时附加了列标题和其他不需要的列。尽管如此,该代码仍然可以工作,但是需要对其进行修复。我没有解决它,因为这是相对简单的更改,我希望OP能够了解他们在做什么错。

答案 1 :(得分:0)

def extractFrames(m):
    global vid_name

    vid_files=glob(m)
    print(vid_files)
    complete_videos = get_completed_videos()
    new_vid_files = [x for x in vid_files if get_vid_name(x) not in complete_videos]

    for vid in new_vid_files:
        print("path of video========>>>>.",vid)

        v1=os.path.basename(vid)


        try:
            vid_name = get_vid_name(vid)
            vidcap = cv2.VideoCapture(vid)
        except cv2.error as e:
            print(e)
        except:
            print('error')
        #condition 
        fsize=os.stat(vid)
        print('=============size of video ===================:' , fsize.st_size)
        try:

            if (fsize.st_size > 1000):

                fps = vidcap.get(cv2.CAP_PROP_FPS)      # OpenCV2 version 2 used "CV_CAP_PROP_FPS"
                frameCount = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
                duration = frameCount/fps
                minutes = int(duration/60)
                print('fps = ' + str(fps))
                print('number of frames = ' + str(frameCount))
                print('duration (S) = ' + str(duration))
                if (duration > 1):
                    success,image = vidcap.read()


                    count=0

                    success=True

                    while success:

                        img_name = vid_name + '_f' + str(count) + ".jpg"

                        success,image = vidcap.read()
                        if count % 10 == 0 or count ==0:
                            target_non_target(img_name, image)

                        count+=1          

                    vidcap.release()

                    cv2.destroyAllWindows()
        except:
            print("error")


        print('finished processing video ',vid)
        with open("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\"+'video_info.csv', 'a') as csv_file:
            fieldnames = ['Video_Name','Process']
            file_is_empty = os.stat("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\"+'video_info.csv').st_size == 0
            writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
            if file_is_empty:
                writer.writeheader()
            writer.writerow({'Video_Name':vid,'Process':'done'})

def get_vid_name(vid):
    return os.path.splitext(os.path.basename(vid))[0]



def get_completed_videos():
    completed_videos = []
    with open("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\video_info.csv") as csv_file:

        for row in csv.reader(csv_file):
            for col in range(0,len(row)):
                try:
                    completed_videos.append(row[col])
                except Exception as e:
                    print(str(e))
    print(completed_videos[0])
    return completed_videos