【OpenCV4】提取视频的每一帧

用OpenCV4提取视频的每一帧,环境python3.8+opencv4

import cv2
import os, sys

# 打开视频
cap = cv2.VideoCapture(r"../vidoes/xxxxx.mp4")

# 获取帧数
fps = cap.get(cv2.CAP_PROP_FPS)

# 获取宽高
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

print("FPS: ", fps)
print("width: ", width)
print("height: ", height)

# 保存图片的路径
os.chdir(r"path")
print(os.getcwd())

# 读取第一帧
flag, frame = cap.read()

# 图片命名计数
i = 0
while flag:
    # 读取每一帧,flag表示是否读取成功,frame为图片内容。
    fileName = "image" + str(i) + ".jpg"
    print(fileName)
    if flag is True:
        cv2.imwrite(fileName, frame, [cv2.IMWRITE_JPEG_QUALITY, 100])
    flag, frame = cap.read()
    i = i + 1
    
print("end!")

【OpenCV4】提取视频的每一帧_第1张图片

你可能感兴趣的:(OpenCV,python,opencv,人工智能)