为了从视频中提取每一帧图片,编写Python脚本实现该功能
video_path为指定的视频路径
interval为指定分割视频是是否跳帧,默认不跳帧,即全部分割
width, height 为指定对分割帧图片调整大小,默认不调整
该脚本自动对帧图片编号,设置为7位编码,最多可分割9999999帧图片,即92小时的30FPS视频
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# ============================================================
# @Date : 2021/12/08 14:40:31
# @Author : LiShan
# @Email : lishan@st.xatu.edu.com
# @File : extract.py
# @IDE : PyCharm
# @Func : Extract video image
# ============================================================
import os.path
import time
import cv2
video_path = "./assets/intersection.mp4"
save_path = video_path[:video_path.rfind('.')]
os.makedirs(save_path, exist_ok=True)
cap = cv2.VideoCapture(video_path)
fps = int(cap.get(cv2.CAP_PROP_FPS))
print('FPS:{:.2f}'.format(fps)
rate = cap.get(5)
frame_num = cap.get(7)
duration = frame_num/rate
print('video total time:{:.2f}s'.format(duration))
# width, height = 1920, 1080
# interval = int(fps) * 4
interval = 1
process_num = frame_num // interval
print('process frame:{:.0f}'.format(process_num))
cnt = 0
num = 0
t0 = time.time()
while cap.isOpened():
ret, frame = cap.read()
if ret:
cnt += 1
if cnt % interval == 0:
num += 1
# frame = cv.resize(frame, (width, height))
cv2.imwrite(save_path + "/%07d.jpg" % num, frame)
remain_frame = process_num - num
t1 = time.time() - t0
t0 = time.time()
print("Processing %07d.jpg, remain frame: %d, remain time: %.2fs" % (num, remain_frame, remain_frame * t1))
else:
break
if cv2.waitKey(1) & 0xff == 27:
break
cap.release()
cv2.destroyAllWindows()
print("done")
更新:2022年1月11日21:54:04
增加多对个视频文件提取制作数据集的功能
新建文件夹video,将待分割的所有视频文件放入该文件夹
运行下列脚本文件,分割后的图片将保存到JPEGImage文件夹
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# ============================================================
# @Date : 2021/12/08 14:40:31
# @Author : LiShan
# @Email : lishan@st.xatu.edu.com
# @File : extract.py
# @IDE : PyCharm
# @Func : Extract video image
# ============================================================
import os.path
import time
import cv2
def get_video_list(path):
video_ext = [".mp4", ".avi"]
video_names = []
for maindir, subdir, file_name_list in os.walk(path):
for filename in file_name_list:
apath = os.path.join(maindir, filename)
ext = os.path.splitext(apath)[1]
if ext in video_ext:
video_names.append(apath)
return video_names
video_path = "./video/"
save_path = "./JPEGImages/"
print("frame image save path:{}".format(save_path))
os.makedirs(save_path, exist_ok=True)
num = 0
video_cnt = 0
if os.path.isdir(video_path):
files = get_video_list(video_path)
else:
files = [video_path]
files.sort()
video_num = len(files)
for i in range(video_num):
video_name = files[i]
print('video name: {}'.format(video_name))
print('progress: {:.0f}/{:.0f}'.format(i + 1, video_num))
cap = cv2.VideoCapture(video_name)
fps = int(cap.get(cv2.CAP_PROP_FPS))
print('FPS: {:.2f}'.format(fps))
rate = cap.get(5)
frame_num = cap.get(7)
duration = frame_num/rate
print('video total time: {:.2f}s'.format(duration))
# width, height = 1920, 1080
# interval = 1
interval = int(fps) * 4
process_num = frame_num // interval
print('process frame: {:.0f}'.format(process_num))
cnt = 0
current_num = 0
t0 = time.time()
while cap.isOpened():
ret, frame = cap.read()
if ret:
cnt += 1
if cnt % interval == 0:
num += 1
current_num += 1
# frame = cv.resize(frame, (width, height))
cv2.imwrite(save_path + "/%07d.jpg" % num, frame)
remain_frame = process_num - current_num
t1 = time.time() - t0
t0 = time.time()
print("Processing %07d.jpg, remain frame: %d, remain time: %.2fs" %
(num, remain_frame, remain_frame * t1))
else:
break
cap.release()
print("done")
分割结果