python对视频进行帧处理以及裁减部分区域

作者 : admin 本文共904个字,预计阅读时间需要3分钟 发布时间: 2024-06-9 共1人阅读

视频截取帧

废话不多说直接上代码:

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/xxx/桌面/data/wx.mp4" #视频路径
    out_path = "/home/xxx/桌面/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 = 0
    j = 0
    if is_all_frame:
        time_interval = 1
 
    while success:
        # people1
        start_h = 530
        start_w = 90
        pad_h = 283
        pad_w = 240

        frame = frame[start_h:start_h+pad_h,start_w:start_w+pad_w,:]
        i = i + 1
        if (i % time_interval == 0):
            if is_all_frame == False:
                if i >= sta_frame and i <= end_frame:
                    j = j + 1
                    print('save frame:', i)
                    save_image(frame, out_path, j)
                elif i > end_frame:
                    break
            else:
                j = j + 1
                print('save frame:', i)
                save_image(frame, out_path, j)
 
        success, frame = videoCapture.read()
本站无任何商业行为
个人在线分享 » python对视频进行帧处理以及裁减部分区域
E-->