-
Notifications
You must be signed in to change notification settings - Fork 37
add opencv benchmark #674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
add opencv benchmark #674
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,6 +145,74 @@ def decode_and_resize(self, video_file, pts_list, height, width, device): | |
] | ||
return frames | ||
|
||
class OpenCVDecoder(AbstractDecoder): | ||
def __init__(self): | ||
import cv2.videoio_registry as vr | ||
|
||
self._print_each_iteration_time = False | ||
api_pref = None | ||
for backend in vr.getStreamBufferedBackends(): | ||
if not vr.hasBackend(backend): | ||
continue | ||
if not vr.isBackendBuiltIn(backend): | ||
_, abi, api = vr.getStreamBufferedBackendPluginVersion(backend) | ||
if (abi < 1 or (abi == 1 and api < 2)): | ||
continue | ||
api_pref = backend | ||
break | ||
self._backend = api_pref | ||
|
||
def decode_frames(self, video_file, pts_list): | ||
import cv2 | ||
|
||
cap = cv2.VideoCapture(video_file, self._backend, []) | ||
if not cap.isOpened(): | ||
raise ValueError("Could not open video stream") | ||
|
||
fps = cap.get(cv2.CAP_PROP_FPS) | ||
frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
approx_frame_numbers = [int(pts * fps) for pts in pts_list] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
|
||
current_frame = 0 | ||
frames = [] | ||
while True: | ||
ok = cap.grab() | ||
if not ok: | ||
break | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's raise here instead of a |
||
if current_frame in approx_frame_numbers: # only decompress needed | ||
ret, frame = cap.retrieve() | ||
if ret: | ||
frames.append(frame) | ||
|
||
if len(frames) == len(approx_frame_numbers): | ||
break | ||
current_frame += 1 | ||
cap.release() | ||
return frames | ||
|
||
def decode_first_n_frames(self, video_file, n): | ||
import cv2 | ||
|
||
cap = cv2.VideoCapture(video_file, self._backend, []) | ||
if not cap.isOpened(): | ||
raise ValueError("Could not open video stream") | ||
|
||
frames = [] | ||
for i in range(n): | ||
ok = cap.grab() | ||
if not ok: | ||
break | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, we should raise instead of |
||
ret, frame = cap.retrieve() | ||
if ret: | ||
frames.append(frame) | ||
cap.release() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's assert that |
||
return frames | ||
|
||
def decode_and_resize(self, video_file, pts_list, height, width, device): | ||
import cv2 | ||
frames = [cv2.resize(frame, (width, height)) for frame in self.decode_frames(video_file, pts_list)] | ||
return frames | ||
|
||
|
||
class TorchCodecCore(AbstractDecoder): | ||
def __init__(self, num_threads=None, color_conversion_library=None, device="cpu"): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment explaining why this is needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jinhohwang-meta, can we be more descriptive of what the meaning of these backends are? Why are we checking for specific ranges of the ABI and API?
I think we'll also want
assert api_pref is not None
after the loop.