-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Bugfix][Multi Modal] Fix broken frames in video input #25881
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?
Changes from all commits
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 | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -121,14 +121,22 @@ def load_bytes( | |||||||||||||||||||
original_fps = cap.get(cv2.CAP_PROP_FPS) | ||||||||||||||||||||
duration = total_frames_num / original_fps if original_fps > 0 else 0 | ||||||||||||||||||||
|
||||||||||||||||||||
validate_frames_list = [] | ||||||||||||||||||||
for idx in range(total_frames_num): | ||||||||||||||||||||
ok = cap.grab() | ||||||||||||||||||||
if ok: | ||||||||||||||||||||
validate_frames_list.append(idx) | ||||||||||||||||||||
|
||||||||||||||||||||
Comment on lines
+124
to
+129
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.
Suggested change
|
||||||||||||||||||||
validate_total_frames_num = len(validate_frames_list) | ||||||||||||||||||||
|
||||||||||||||||||||
# resample video to target num_frames | ||||||||||||||||||||
full_read = num_frames == -1 or total_frames_num < num_frames | ||||||||||||||||||||
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. The condition for
Suggested change
|
||||||||||||||||||||
if full_read: | ||||||||||||||||||||
num_frames = total_frames_num | ||||||||||||||||||||
num_frames = validate_total_frames_num | ||||||||||||||||||||
frame_idx = list(range(0, num_frames)) | ||||||||||||||||||||
else: | ||||||||||||||||||||
uniform_sampled_frames = np.linspace(0, | ||||||||||||||||||||
total_frames_num - 1, | ||||||||||||||||||||
validate_total_frames_num - 1, | ||||||||||||||||||||
num_frames, | ||||||||||||||||||||
dtype=int) | ||||||||||||||||||||
frame_idx = uniform_sampled_frames.tolist() | ||||||||||||||||||||
|
@@ -138,15 +146,21 @@ def load_bytes( | |||||||||||||||||||
frames = np.empty((len(frame_idx), height, width, 3), dtype=np.uint8) | ||||||||||||||||||||
|
||||||||||||||||||||
i = 0 | ||||||||||||||||||||
for idx in range(total_frames_num): | ||||||||||||||||||||
ok = cap.grab() | ||||||||||||||||||||
if not ok: | ||||||||||||||||||||
break | ||||||||||||||||||||
if idx in frame_idx: | ||||||||||||||||||||
ret, frame = cap.retrieve() | ||||||||||||||||||||
if ret: | ||||||||||||||||||||
frames[i] = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | ||||||||||||||||||||
i += 1 | ||||||||||||||||||||
cap.set(cv2.CAP_PROP_POS_FRAMES, 0) | ||||||||||||||||||||
validate_list_idx = frame_idx[i] | ||||||||||||||||||||
target_frame_pos = validate_frames_list[validate_list_idx] | ||||||||||||||||||||
Comment on lines
+149
to
+151
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. I think |
||||||||||||||||||||
for pos in range(total_frames_num): | ||||||||||||||||||||
cap.grab() | ||||||||||||||||||||
if target_frame_pos != pos: | ||||||||||||||||||||
continue | ||||||||||||||||||||
ret, frame = cap.retrieve() | ||||||||||||||||||||
if ret: | ||||||||||||||||||||
frames[i] = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | ||||||||||||||||||||
i += 1 | ||||||||||||||||||||
if i >= len(frame_idx): | ||||||||||||||||||||
break | ||||||||||||||||||||
validate_list_idx = frame_idx[i] | ||||||||||||||||||||
target_frame_pos = validate_frames_list[validate_list_idx] | ||||||||||||||||||||
Comment on lines
148
to
+163
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. The new frame reading loop has several critical issues that make it fragile:
A more robust implementation is needed to handle these cases correctly. Here is a suggested replacement that addresses these issues by using a Additionally, i = 0
pos = 0
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
while i < len(frame_idx) and pos < total_frames_num:
# The frame position in the original video that we want to read.
target_pos = validate_frames_list[frame_idx[i]]
if pos < target_pos:
# Seek forward by grabbing frames.
if not cap.grab():
break # End of stream.
pos += 1
continue
# At this point, pos should be equal to target_pos.
# Grab the frame at the current position `pos`.
if not cap.grab():
break # End of stream.
ret, frame = cap.retrieve()
if ret:
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# This frame might be requested multiple times (upsampling).
# Fill all duplicates for this position.
while i < len(frame_idx) and validate_frames_list[frame_idx[i]] == target_pos:
frames[i] = rgb_frame
i += 1
else:
# If retrieve fails, we cannot read this frame.
# Skip all requests for this frame to avoid getting stuck.
while i < len(frame_idx) and validate_frames_list[frame_idx[i]] == target_pos:
i += 1
pos += 1 |
||||||||||||||||||||
|
||||||||||||||||||||
assert i == num_frames, (f"Expected reading {num_frames} frames, " | ||||||||||||||||||||
f"but only loaded {i} frames from video.") | ||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
BTW, iterate over the whole video can be quite expensive for long video, even if we don't load any frame to memory. I think we should avoid iterating over the whole video twice as much as possible.
Uh oh!
There was an error while loading. Please reload this page.
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.
Yes, and I test the time cost in my development machine,
cap.grab()
operation cost5e-4
s ,cap.retrieve()
cost1e-3
s. I test a video with 543 frames and the result is it cost 0.3s before fix and 0.6s after fix. I triedcap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
but it cost even more time...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.
Another method I considered is repeat the last frame to fill the frame list, it cost no time.
Uh oh!
There was an error while loading. Please reload this page.
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.
I think you can just return the slice of valid frames: