59 lines
1.5 KiB
Python
Executable File
59 lines
1.5 KiB
Python
Executable File
'''
|
||
author : Hatcher W
|
||
Email: hcwang@semi.ac.cn
|
||
use: 视频抽帧
|
||
|
||
'''
|
||
# -*- coding:utf8 -*-
|
||
import cv2
|
||
import os
|
||
import shutil
|
||
|
||
|
||
# 视频文件名字l
|
||
|
||
# 保存图片的路径
|
||
def vedio_img(input,output,fps):
|
||
isExists = os.path.exists(output)
|
||
if not isExists:
|
||
os.makedirs(output)
|
||
print('path of %s is build' % (output))
|
||
else:
|
||
shutil.rmtree(output)
|
||
os.makedirs(output)
|
||
print('path of %s already exist and rebuild' % (output))
|
||
|
||
# 视频帧率12
|
||
# fps = 1
|
||
# 保存图片的帧率间隔
|
||
#count = 1
|
||
|
||
# 开始读视频
|
||
videoCapture = cv2.VideoCapture(input)
|
||
i = 0
|
||
j = 0
|
||
|
||
while True:
|
||
success, frame = videoCapture.read()
|
||
|
||
if not success:
|
||
print('video is all read')
|
||
break
|
||
#frame = cv2.resize(frame, (1024, 768))
|
||
i += 1
|
||
if (i % fps == 0):
|
||
# 保存图片
|
||
j += 1
|
||
savedname = output+"/" + "{}".format(j).zfill(7)+ '.jpg' #density
|
||
#savedname = output + "/" + "{}".format(j).zfill(7) + '.jpg' #detection
|
||
cv2.imwrite( savedname, frame)
|
||
print('image of %s is saved' % (savedname))
|
||
|
||
if __name__ == '__main__':
|
||
filename = '/data3/DEMO/video/PYH/PYH_SHL001.mp4'
|
||
savedpath = '/data3/DEMO/images/PYH_SHL001'
|
||
os.system("sudo chmod 777 -R /data3/DEMO/")
|
||
if not os.path.exists(savedpath):
|
||
os.makedirs(savedpath)
|
||
vedio_img(filename,savedpath,1)
|