-
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
Open
jinhohwang-meta
wants to merge
2
commits into
pytorch:main
Choose a base branch
from
jinhohwang-meta:benchmark_opencv
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add opencv benchmark #674
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,6 +145,76 @@ 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 | ||
# Check backend abi/api for compatibility | ||
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) | ||
approx_frame_indices = [int(pts * fps) for pts in pts_list] | ||
|
||
current_frame = 0 | ||
frames = [] | ||
while True: | ||
ok = cap.grab() | ||
if not ok: | ||
raise ValueError("Could not grab video frame") | ||
if current_frame in approx_frame_indices: # only decompress needed | ||
ret, frame = cap.retrieve() | ||
if ret: | ||
frames.append(frame) | ||
|
||
if len(frames) == len(approx_frame_indices): | ||
break | ||
current_frame += 1 | ||
cap.release() | ||
assert len(frames) == len(approx_frame_indices) | ||
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: | ||
raise ValueError("Could not grab video frame") | ||
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 |
||
assert len(frames) == n | ||
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"): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.