34 lines
950 B
Python
34 lines
950 B
Python
import contextlib
|
|
import time
|
|
import cv2
|
|
|
|
|
|
def create_video_writer(video_cap, output_filename):
|
|
|
|
# grab the width, height, and fps of the frames in the video stream.
|
|
frame_width = int(video_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
|
frame_height = int(video_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
|
fps = int(video_cap.get(cv2.CAP_PROP_FPS))
|
|
|
|
# initialize the FourCC and a video writer object
|
|
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
|
|
writer = cv2.VideoWriter(output_filename, fourcc, fps,
|
|
(frame_width, frame_height))
|
|
|
|
return writer
|
|
|
|
|
|
def iter_frames(video_cap):
|
|
while 1:
|
|
ret, frame = video_cap.read()
|
|
if not ret:
|
|
return
|
|
yield frame, video_cap.get(cv2.CAP_PROP_POS_MSEC)
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def time_tracker():
|
|
start = time.monotonic()
|
|
yield
|
|
end = time.monotonic()
|
|
print(f"Time to process 1 frame: {(end-start) * 1000:.0f} milliseconds") |