京东猪脸识别比赛数据预处理:用Python将视频每一帧提取存储为图片

来源:互联网 发布:单片机地址寄存器 编辑:程序博客网 时间:2024/06/01 10:34

最近参加京东的猪脸识别比赛,训练集是30个视频,需要将视频的每一帧提取出来存储为图片,存入对应的文件夹(分类标签)。

本例是直接调用了cv2 模块中的 VideoCapture。一次运行,大概10分钟,就能得到预处理后的分类图片了,具体代码如下。

视频每一帧提取存储为图片代码

#! encoding: UTF-8import osimport cv2import cvvideos_src_path='/sata_disk/E_office/zhouhongli/pig/train'images_save_path='/sata_disk/E_office/zhouhongli/pig/frame'videos = os.listdir(videos_src_path)videos = filter(lambda x: x.endswith('mp4'), videos)for each_video in videos:    print each_video    # get the name of each video, and make the directory to save frames    each_video_name,_=each_video.split('.')    os.mkdir(images_save_path +'/'+ each_video_name)    each_video_save_full_path=os.path.join(images_save_path, each_video_name) + '/'    # get the full path of each video, which will open the video tp extract frames    each_video_full_path=os.path.join(videos_src_path, each_video)    cap=cv2.VideoCapture(each_video_full_path)    frame_count = 1    success = True    while(success):        success, frame=cap.read()        print 'Read a new frame: ', success        params = []        params.append(cv.CV_IMWRITE_PXM_BINARY)        params.append(1)        cv2.imwrite(each_video_save_full_path + each_video_name + "_%d.jpg" % frame_count, frame, params)        frame_count = frame_count+1cap.release()

递归删除文件的问题

但有个问题,每一个视频转换得到的30个子文件夹里,都有2952张图片,但第2952张是空的,所以只有运用强大的Linux递归删除符合条件的文件了,我是这样删除滴。

zhouhongli@1080TI:~$ find  . -name '*_2952.jpg' -size 0 -print0 |xargs -0 rm

参考

python tools:将视频的每一帧提取并保存
http://blog.csdn.net/u010167269/article/details/53268686
Linux find 与 rm 联动删除符合条件的文件
https://maoxian.de/2015/12/1362.html

原创粉丝点击