大家好,又見面了,我是你們的朋友全棧君。
#! 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遞歸刪除符合條件的文件了,我是這樣刪除滴。
[email protected]:~$ 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