大家好,又见面了,我是你们的朋友全栈君。
#! encoding: UTF-8
import os
import cv2
import cv
videos_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+1
cap.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
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/151885.html原文链接:https://javaforall.cn