Skip to content

add opencv benchmark #711

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 7 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
4 changes: 4 additions & 0 deletions benchmarks/decoders/benchmark_decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
AbstractDecoder,
DecordAccurate,
DecordAccurateBatch,
OpenCVDecoder,
plot_data,
run_benchmarks,
TorchAudioDecoder,
Expand Down Expand Up @@ -61,6 +62,9 @@ class DecoderKind:
{"backend": "video_reader"},
),
"torchaudio": DecoderKind("TorchAudio", TorchAudioDecoder),
"opencv": DecoderKind(
"OpenCV[backend=FFMPEG]", OpenCVDecoder, {"backend": "FFMPEG"}
),
}


Expand Down
74 changes: 74 additions & 0 deletions benchmarks/decoders/benchmark_decoders_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,80 @@ def decode_and_resize(self, video_file, pts_list, height, width, device):
return frames


class OpenCVDecoder(AbstractDecoder):
def __init__(self, backend):
import cv2

self.cv2 = cv2

self._available_backends = {"FFMPEG": cv2.CAP_FFMPEG}
self._backend = self._available_backends.get(backend)

self._print_each_iteration_time = False

def decode_frames(self, video_file, pts_list):
cap = self.cv2.VideoCapture(video_file, self._backend)
if not cap.isOpened():
raise ValueError("Could not open video stream")

fps = cap.get(self.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:
# OpenCV uses BGR, change to RGB
frame = self.cv2.cvtColor(frame, self.cv2.COLOR_BGR2RGB)
# Update to C, H, W
frame = np.transpose(frame, (2, 0, 1))
frame = torch.from_numpy(frame)
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
Copy link
Member

Choose a reason for hiding this comment

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

I suspect what we're getting as output from opencv are numpy arrays. For a fair comparison with the other decoders, we should convert them to pytorch. I think torch.from_numpy is what we'd want to use for a fair comparison, as it returns a view and it's cheap.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I confirmed opencv returned numpy arrays, so I added a call to torch.from_numpy. While validating that the frames were correct, I had to make a few more adjustments to the color and array order to get the same result as other decoders. I can remove them if we decide they are not needed for the benchmark.


def decode_first_n_frames(self, video_file, n):
cap = self.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:
# OpenCV uses BGR, change to RGB
frame = self.cv2.cvtColor(frame, self.cv2.COLOR_BGR2RGB)
# Update to C, H, W
frame = np.transpose(frame, (2, 0, 1))
frame = torch.from_numpy(frame)
frames.append(frame)
cap.release()
assert len(frames) == n
return frames

def decode_and_resize(self, video_file, pts_list, height, width, device):

# OpenCV doesn't apply antialias, while other `decode_and_resize()` implementations apply antialias by default.
frames = [
self.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"):
self._num_threads = int(num_threads) if num_threads else None
Expand Down
Loading