# coding=utf-8
# 使用OpenCV视频中提取帧图片并保存(cv2.VideoCapture)
import os
import cv2
import shutil
import time
# 全局变量
s = ".\Dekopon orange\Dekopon orange"
for i in range(1,7,1):
VIDEO_PATH = s + str(i) + ".mp4"
EXTRACT_FOLDER = s + str(i)
# VIDEO_PATH = './green_grapefruit/green_grapefruit_demo_2.mp4' # 视频地址
VIDEO_PATH = '.\Dekopon orange\Dekopon orange1.mp4' # 视频地址
# EXTRACT_FOLDER = './green_grapefruit_2.mp4' # 存放帧图片的位置
EXTRACT_FOLDER = '.\Dekopon orange\Dekopon orange1' # 存放帧图片的位置
EXTRACT_FREQUENCY = 30 # 帧提取频率
# 主操作
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()
视频分帧代码
该代码示例展示了如何利用OpenCV库在Python中从.mp4视频文件中每30帧提取一张图片并保存到指定文件夹。同时,它还计算了视频的帧率(FPS),有两种不同的计算方法:一种是通过OpenCV的get()函数,另一种是手动计算总帧数除以总时间。
摘要由CSDN通过智能技术生成