import os
import time
source = './11/2021-4-13-10-41-51.mp4'
# 将视频转成图片序列后的分辨率
new_width = 224
new_height = 224
import mmcv
s = time.time()
# 读取视频
video = mmcv.VideoReader(source)
if video is None :
print('Warning: unable to open video source: ', source)
lll = len(video)
for i in range(lll):
if video[i] is not None:
# 改变图片分辨率
if new_width>0 and new_height>0:
out_img = mmcv.imresize(video[i], (new_width,new_height))
else:
out_img = video[i]
mmcv.imwrite(out_img, os.path.join('out_dir','img_%05d.jpg'%i))
else:
warnings.warn(
'Length inconsistent!'
f'Early stop with {i + 1} out of {len(vr)} frames.')
break
e = time.time()
print(e-s) # 2.1249728202819824 2.2611641883850098 2.648266315460205 2.231487512588501
import cv2
s = time.time()
# opencv版本的视频转图像序列
cap = cv2.VideoCapture(source)
if cap is None or not cap.isOpened():
print('Warning: unable to open video source: ', source)
i = 0
while True:
ret, img = cap.read()
if ret == False:
break
if img is not None:
# 改变图片分辨率
if new_width>0 and new_height>0:
out_img = cv2.resize(img, (new_width,new_height))
else:
out_img = img
cv2.imwrite(os.path.join('out_dir','img_%05d.jpg'%i), out_img)
else:
warnings.warn(
'Length inconsistent!'
f'Early stop with {i + 1} out of {len(vr)} frames.')
break
i += 1
e = time.time()
print(e-s) # 1.9834942817687988 2.1920833587646484 2.027904987335205 2.0143558979034424
热运行多次(这里只记录了4次),综合来开,cv2速度是更快的,大约平均快 0.3s +
环境:windows10、EditPlus5.0、python3.7.6、mmcv1.3.1、open-cv4.4.0.46