Skip to content

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions benchmarks/decoders/benchmark_decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
TorchCodecPublic,
TorchCodecPublicNonBatch,
TorchVision,
OpenCVDecoder,
)


Expand Down Expand Up @@ -61,6 +62,7 @@ class DecoderKind:
{"backend": "video_reader"},
),
"torchaudio": DecoderKind("TorchAudio", TorchAudioDecoder),
"opencv": DecoderKind("OpenCV", OpenCVDecoder),
}


Expand Down
70 changes: 70 additions & 0 deletions benchmarks/decoders/benchmark_decoders_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +158 to +161
Copy link
Member

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?

Copy link
Contributor

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.

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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's assert that len(frames) == n to ensure no error went undetected

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"):
Expand Down