# -*- coding: utf-8 -*-
import shutil
import os
import cv2
os.chdir(os.path.split(os.path.realpath(__file__))[0])
print(os.getcwd())
dirs_path = os.getcwd() # 设置路径
dirs = os.listdir(dirs_path) # 获取指定路径下的文件
for video_name in dirs: # 循环读取路径下的文件并筛选输出
if os.path.splitext(video_name)[1] == ".mp4": # 筛选mp4文件
print(video_name) # 输出所有的mp4文件
break
output_img_path = './output1/'
if not os.path.exists(output_img_path):
os.mkdir(output_img_path)
else:
# 删除原有目录
shutil.rmtree(output_img_path)
# 创建一个新目录
os.mkdir(output_img_path)
cap = cv2.VideoCapture(video_name)
#videoFPS=cap.get(cv2.CAP_PROP_FPS)
#print (videoFPS)
cnt = 1
print ("start")
while(1):
ret, frame = cap.read()
# show a frame
if ret is True:
print(frame.shape)
cnt=cnt+1
print(cnt)
if cnt % 10 == 0:
# cropped = frame[1075:128, 119:1397] # 裁剪坐标为[y0:y1, x0:x1]
cropped = frame[120:750, 260:1660] # 裁剪坐标为[y0:y1, x0:x1]
output_name = "%s%s%s"%(output_img_path,str(int(cnt/10)),'.jpg')
cv2.imwrite(output_name, cropped)
else:
break
print("finished")
cap.release()
cv2.destroyAllWindows()
为方便代码执行,直接将文件可以放置视频目录下,自动检测MP4文件,
其中cropped = frame[120:750, 260:1660] # 裁剪坐标为[y0:y1, x0:x1]
可以设置裁剪的图片大小
if cnt % 10 == 0:
为间隔10帧截取一次图片
output_img_path = './output1/'
为图片保存目录
https://download.csdn.net/download/tanghong1996/11011868