我想以最快的方式解析特定帧频的视频。我正在使用机器学习来分析视频中的帧,但是我不需要分析每个帧。
这是一项功能,可以在可变帧频下测试视频解析时间。
def extract_img_from_vid(path_in, desired_fps):
cap = cv2.VideoCapture(path_in)
count = 0
frame_count = 0
length = int(cap.get(cv2.cv2.CAP_PROP_FRAME_COUNT))
width = int(cap.get(cv2.cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.cv2.CAP_PROP_FPS)
print ('Length: {:.2f} | Width: {:.2f} | Height: {:.2f} | Fps: {:.2f}'.format(length, width, height, fps))
skip = round(int(fps)/desired_fps) #determine how ofter to skip a frame
start = time.time()
while cap.isOpened():
success, frame = cap.read()
if success:
count += 1
if count % skip != 0:
continue
#do some analysis on the frame
frame_count += 1
else:
break
end = time.time()
print("Time to analyze individual frame: {:.4f}sec | Total Time: {:.3f} ".format(((end-start)/count), end-start))
print('Total Frames: {} | Frames Analyzed {}'. format(count, frame_count))
这是FPS为1时的输出。您可以使用此方法推断出帧速率不影响解析速度。
Length: 10887.00 | Width: 1920.00 | Height: 1080.00 | Fps: 60.00
Time to analyze individual frame: 0.0082sec | Total Time: 88.798
Total Frames: 10886 | Frames Analyzed 181
有人有这样做的更有效方法吗?