Python-OpenCV read video frame and save as picture

cv2.VideoCapture() reads video frames

import cv2  # 代入OpenCV模块
VIDEO_PATH = 'video.mp4'  # 视频地址
video = cv2.VideoCapture(video_path)  # 实例化视频对象
retval, frame = video.read()  # 逐帧读取
  • vid = cv2.VideoCapture(0)

    • The parameter in VideoCapture() is 0, which means to open the built-in camera of the notebook
    • The parameter is the video file path to open the video, such asvid= cv2.VideoCapture('video.mp4')
  • retval, frame = vid.read()

    • vid.read() reads video frame by frame
    • retval, frame are the two return values ​​of the vic.read() method. Where retval is a boolean value, if the read frame is correct, it returns True, if the file is read to the end, its return value is False; frame is the image of each frame, which is a three-dimensional matrix

 

Two ways to calculate FPS

FPS: Frame Per Second transmits frames per second

Generally speaking, it refers to the number of pictures of animation or video. The more frames per second, the smoother the displayed action will be.

  • video.get(cv2.CAP_PROP_FPS)orvideo.get(cv2.cv.CAP_PROP_FPS)
    • For local video files, the frames per second can be obtained directly from get(CAP_PROP_FPS)orget(CV_CAP_PROP_FPS)
  • Manual calculation: total frames / total time
    • For webcams and many other connected cameras, the frames per second have to be calculated manually, i.e. you can read a certain number of frames from the video and see how much time it takes to calculate those frames

 

Read video frame and save as picture

# coding=utf-8
# 使用OpenCV视频中提取帧图片并保存(cv2.VideoCapture)
import os
import cv2
import shutil
import time

# 全局变量
VIDEO_PATH = 'videos/test_video3.mp4'  # 视频地址
EXTRACT_FOLDER = 'test'  # 存放帧图片的位置
EXTRACT_FREQUENCY = 10  # 帧提取频率


# 主操作
def extract_frames(video_path, dst_folder, index):
    # 实例化视频对象
    video = cv2.VideoCapture(video_path)
    frame_count = 0

    # 循环遍历视频中的所有帧
    while True:
        # 逐帧读取
        _, frame = video.read()
        if frame is None:
            break
        # 按照设置的频率保存图片
        if frame_count % EXTRACT_FREQUENCY == 0:
            # 设置保存文件名
            save_path = "{}/{:>03d}.jpg".format(dst_folder, index)
            # 保存图片
            cv2.imwrite(save_path, frame)
            index += 1  # 保存图片数+1
        frame_count += 1  # 读取视频帧数+1

    # 视频总帧数
    print(f'the number of frames: {
      
      frame_count}')
    # 打印出所提取图片的总数
    print("Totally save {:d} imgs".format(index - 1))

    # 计算FPS 方法一 get()
    (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')  # Find OpenCV version
    # (major_ver, minor_ver, subminor_ver) = (4, 5, 4)
    if int(major_ver) < 3:
        fps = video.get(cv2.cv.CV_CAP_PROP_FPS)  # 获取当前版本opencv的FPS
        print("Frames per second using video.get(cv2.cv.CV_CAP_PROP_FPS): {0}".format(fps))
    else:
        fps = video.get(cv2.CAP_PROP_FPS)  # 获取当前版本opencv的FPS
        print("Frames per second using video.get(cv2.CAP_PROP_FPS) : {0}".format(fps))

    # 计算FPS 方法二 手动计算 总帧数 / 总时间
    # new_vid = cv2.VideoCapture(video_path)
    # start = time.time()  # 开始时间
    # c = 0
    # while True:
    #     _, frames = new_vid.read()
    #     if frames is None:
    #         break
    #     c += 1
    # end = time.time()  # 结束时间
    # fps2 = c / (end - start)  # 总帧数 / 总时间
    # print(f'frames:{c}')
    # print(f'seconds:{end - start}')
    # print("Frames per second using frames / seconds : {0}".format(fps2))
    # new_vid.release()

    # 最后释放掉实例化的视频
    video.release()


def main():
    # 递归删除之前存放帧图片的文件夹,并新建一个
    try:
        shutil.rmtree(EXTRACT_FOLDER)
    except OSError:
        pass
    os.mkdir(EXTRACT_FOLDER)
    # 抽取帧图片,并保存到指定路径
    extract_frames(VIDEO_PATH, EXTRACT_FOLDER, 1)


if __name__ == '__main__':
    main()

 

Reference

opencv: extract frame pictures from video and save (cv2.VideoCapture)

Use python-opencv to read video, calculate the total number of video frames and the implementation of FPS

OpenCV calculates fps (frames per second-fps)

Guess you like

Origin blog.csdn.net/weixin_43799388/article/details/123958639