视频截取帧
废话不多说直接上代码:
from cv2 import VideoCapture
from cv2 import imwrite# 定义保存图片函数
# image:要保存的图片名字
# addr;图片地址与相片名字的前部分
# num: 相片,名字的后缀。int 类型
def save_image(image, addr, num):address = addr + str(num) + '.jpg'imwrite(address, image)if __name__ == '__main__':video_path = "/home/makeblock/桌面/data/wx.mp4" #视频路径out_path = "/home/makeblock/桌面/data/AIswitch/out/img_" #保存图片路径+名字is_all_frame = False #是否取所有的帧sta_frame = 1 #开始帧end_frame = 400 #结束帧######time_interval = 10 #时间间隔# 读取视频文件videoCapture = VideoCapture(video_path)# 读帧success, frame = videoCapture.read()print(success)i = 0j = 0if is_all_frame:time_interval = 1while success:# people1start_h = 530start_w = 90pad_h = 283pad_w = 240frame = frame[start_h:start_h+pad_h,start_w:start_w+pad_w,:]i = i + 1if (i % time_interval == 0):if is_all_frame == False:if i >= sta_frame and i <= end_frame:j = j + 1print('save frame:', i)save_image(frame, out_path, j)elif i > end_frame:breakelse:j = j + 1print('save frame:', i)save_image(frame, out_path, j)success, frame = videoCapture.read()