-
-
Notifications
You must be signed in to change notification settings - Fork 344
add Random Frame Extractor #464
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,57 @@ | ||||||||||
import cv2 | ||||||||||
import os | ||||||||||
import random | ||||||||||
import uuid | ||||||||||
import argparse | ||||||||||
|
||||||||||
def extract_random_frames(video_path, num_frames=15, output_folder="./datasets/images"): | ||||||||||
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 function should validate that num_frames is a positive integer to prevent unexpected behavior with zero or negative values.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||
# Create output directory if it doesn't exist | ||||||||||
os.makedirs(output_folder, exist_ok=True) | ||||||||||
|
||||||||||
# Load video | ||||||||||
cap = cv2.VideoCapture(video_path) | ||||||||||
if not cap.isOpened(): | ||||||||||
print("Error: Could not open video.") | ||||||||||
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 error message should be more descriptive and include the video path to help users identify which file failed to open.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||
return | ||||||||||
|
||||||||||
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) | ||||||||||
if total_frames < num_frames: | ||||||||||
print(f"Video has only {total_frames} frames. Reducing number of outputs.") | ||||||||||
num_frames = total_frames | ||||||||||
|
||||||||||
# Generate a short UUID for file prefix | ||||||||||
video_id = str(uuid.uuid4().hex)[:8] | ||||||||||
|
||||||||||
# Select random frame indices | ||||||||||
selected_frames = sorted(random.sample(range(total_frames), num_frames)) | ||||||||||
print(f"Extracting frames at indices: {selected_frames}") | ||||||||||
|
||||||||||
frame_idx = 0 | ||||||||||
save_idx = 1 | ||||||||||
|
||||||||||
while cap.isOpened(): | ||||||||||
ret, frame = cap.read() | ||||||||||
if not ret: | ||||||||||
break | ||||||||||
|
||||||||||
if frame_idx in selected_frames: | ||||||||||
filename = os.path.join(output_folder, f"{video_id}_{save_idx}.png") | ||||||||||
cv2.imwrite(filename, frame) | ||||||||||
print(f"Saved: {filename}") | ||||||||||
save_idx += 1 | ||||||||||
|
||||||||||
frame_idx += 1 | ||||||||||
if save_idx > 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. This condition should be
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||
break | ||||||||||
|
||||||||||
cap.release() | ||||||||||
print("Done.") | ||||||||||
|
||||||||||
if __name__ == "__main__": | ||||||||||
parser = argparse.ArgumentParser(description="Extract random frames from a video.") | ||||||||||
parser.add_argument("video_path", help="Path to the input video file") | ||||||||||
parser.add_argument("num_frames", type=int, help="Number of random frames to extract") | ||||||||||
parser.add_argument("--output", default="./datasets/images", help="Output folder for images (default: ./datasets/images)") | ||||||||||
|
||||||||||
args = parser.parse_args() | ||||||||||
extract_random_frames(args.video_path, args.num_frames, args.output) |
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.
Multiple issues: 'extractor' should be capitalized as 'Extractor' for consistency, there's an extra space before the closing bracket in the link text, the URL has '%Frame%20Extractor' which should be '%20Frame%20Extractor', and the description has grammatical errors - should be 'This Python-based command-line tool will help you generate n number of frames from a video input.'
Copilot uses AI. Check for mistakes.