Skip to content

Commit

Permalink
Tweak frame extraction function
Browse files Browse the repository at this point in the history
  • Loading branch information
AsiaCao committed Apr 16, 2024
1 parent 0f16897 commit 88a5e43
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions vision_agent/tools/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


def extract_frames_from_video(
video_uri: str, fps: int = 2, motion_detection_threshold: float = 0.06
video_uri: str, fps: int = 12, motion_detection_threshold: float = 0.06
) -> List[Tuple[np.ndarray, float]]:
"""Extract frames from a video
Expand All @@ -25,7 +25,8 @@ def extract_frames_from_video(
motion_detection_threshold: The threshold to detect motion between
changes/frames. A value between 0-1, which represents the percentage change
required for the frames to be considered in motion. For example, a lower
value means more frames will be extracted.
value means more frames will be extracted. A non-positive value will disable
motion detection and extract all frames.
Returns:
a list of tuples containing the extracted frame and the timestamp in seconds.
Expand Down Expand Up @@ -119,18 +120,19 @@ def _extract_frames_by_clip(
total=processable_frames, desc=f"Extracting frames from clip {start}-{end}"
)
for i, frame in enumerate(clip.iter_frames(fps=fps, dtype="uint8")):
curr_processed_frame = _preprocess_frame(frame)
total_count += 1
pbar.update(1)
# Skip the frame if it is similar to the previous one
if prev_processed_frame is not None and _similar_frame(
prev_processed_frame,
curr_processed_frame,
threshold=motion_detection_threshold,
):
skipped_count += 1
continue
prev_processed_frame = curr_processed_frame
if motion_detection_threshold > 0:
curr_processed_frame = _preprocess_frame(frame)
# Skip the frame if it is similar to the previous one
if prev_processed_frame is not None and _similar_frame(
prev_processed_frame,
curr_processed_frame,
threshold=motion_detection_threshold,
):
skipped_count += 1
continue
prev_processed_frame = curr_processed_frame
ts = round(clip.reader.pos / source_fps, 3)
frames.append((frame, ts))

Expand Down

0 comments on commit 88a5e43

Please sign in to comment.