Candle
commited on
Commit
·
c39093b
1
Parent(s):
668dd21
random data
Browse files- extract_frames.py +1 -1
- random_frame_extractor.py +205 -0
extract_frames.py
CHANGED
|
@@ -95,7 +95,7 @@ def main():
|
|
| 95 |
expanded_dir.mkdir(exist_ok=True)
|
| 96 |
|
| 97 |
# Find all WebP files
|
| 98 |
-
webp_files = list(shots_dir.glob('sample-
|
| 99 |
|
| 100 |
if not webp_files:
|
| 101 |
print(f"No WebP files found in {shots_dir}")
|
|
|
|
| 95 |
expanded_dir.mkdir(exist_ok=True)
|
| 96 |
|
| 97 |
# Find all WebP files
|
| 98 |
+
webp_files = list(shots_dir.glob('sample-193-*.webp'))
|
| 99 |
|
| 100 |
if not webp_files:
|
| 101 |
print(f"No WebP files found in {shots_dir}")
|
random_frame_extractor.py
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Script to randomly extract k frames from WebP files in data/shots/ and save them as PNG files.
|
| 4 |
+
|
| 5 |
+
Usage:
|
| 6 |
+
python random_frame_extractor.py <k> <output_dir>
|
| 7 |
+
|
| 8 |
+
Where:
|
| 9 |
+
k: Number of frames to extract
|
| 10 |
+
output_dir: Directory to save the extracted frames
|
| 11 |
+
|
| 12 |
+
Input: data/shots/sample-XXX-Y.webp (randomly selected)
|
| 13 |
+
Output: <output_dir>/sample-XXX-Y-fZ.png (where Z is the frame number, e.g., f20 for frame 20)
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
import os
|
| 17 |
+
import sys
|
| 18 |
+
import random
|
| 19 |
+
from pathlib import Path
|
| 20 |
+
from PIL import Image
|
| 21 |
+
import re
|
| 22 |
+
import argparse
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def get_frame_count(webp_path):
|
| 26 |
+
"""
|
| 27 |
+
Get the total number of frames in a WebP file.
|
| 28 |
+
|
| 29 |
+
Args:
|
| 30 |
+
webp_path (Path): Path to the WebP file
|
| 31 |
+
|
| 32 |
+
Returns:
|
| 33 |
+
int: Number of frames in the file
|
| 34 |
+
"""
|
| 35 |
+
try:
|
| 36 |
+
with Image.open(webp_path) as img:
|
| 37 |
+
frame_count = 0
|
| 38 |
+
try:
|
| 39 |
+
while True:
|
| 40 |
+
img.seek(frame_count)
|
| 41 |
+
frame_count += 1
|
| 42 |
+
except EOFError:
|
| 43 |
+
pass
|
| 44 |
+
return max(1, frame_count) # At least 1 frame for static images
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"Error reading {webp_path}: {e}")
|
| 47 |
+
return 0
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def extract_random_frames(webp_path, output_dir, num_frames_to_extract):
|
| 51 |
+
"""
|
| 52 |
+
Extract random frames from a WebP file and save them as PNG files.
|
| 53 |
+
|
| 54 |
+
Args:
|
| 55 |
+
webp_path (Path): Path to the input WebP file
|
| 56 |
+
output_dir (Path): Directory to save the extracted frames
|
| 57 |
+
num_frames_to_extract (int): Number of frames to extract from this file
|
| 58 |
+
|
| 59 |
+
Returns:
|
| 60 |
+
list: List of extracted frame filenames
|
| 61 |
+
"""
|
| 62 |
+
try:
|
| 63 |
+
# Parse filename to get sample and shot numbers
|
| 64 |
+
filename = webp_path.stem # e.g., "sample-123-2"
|
| 65 |
+
match = re.match(r'sample-(\d+)-(\d+)', filename)
|
| 66 |
+
if not match:
|
| 67 |
+
print(f"Warning: Filename {filename} doesn't match expected pattern")
|
| 68 |
+
return []
|
| 69 |
+
|
| 70 |
+
sample_num = match.group(1)
|
| 71 |
+
shot_num = match.group(2)
|
| 72 |
+
|
| 73 |
+
# Get total frame count
|
| 74 |
+
total_frames = get_frame_count(webp_path)
|
| 75 |
+
if total_frames == 0:
|
| 76 |
+
return []
|
| 77 |
+
|
| 78 |
+
# Determine how many frames to extract (don't exceed available frames)
|
| 79 |
+
frames_to_extract = min(num_frames_to_extract, total_frames)
|
| 80 |
+
|
| 81 |
+
# Select random frame indices
|
| 82 |
+
if total_frames == 1:
|
| 83 |
+
selected_frames = [0]
|
| 84 |
+
else:
|
| 85 |
+
selected_frames = sorted(random.sample(range(total_frames), frames_to_extract))
|
| 86 |
+
|
| 87 |
+
extracted_files = []
|
| 88 |
+
|
| 89 |
+
with Image.open(webp_path) as img:
|
| 90 |
+
for frame_idx in selected_frames:
|
| 91 |
+
try:
|
| 92 |
+
# Seek to the specific frame
|
| 93 |
+
img.seek(frame_idx)
|
| 94 |
+
|
| 95 |
+
# Convert to RGB if necessary
|
| 96 |
+
frame = img.convert('RGB')
|
| 97 |
+
|
| 98 |
+
# Create output filename: sample-XXX-Y-fZ.png
|
| 99 |
+
output_filename = f"sample-{sample_num}-{shot_num}-f{frame_idx}.png"
|
| 100 |
+
output_path = output_dir / output_filename
|
| 101 |
+
|
| 102 |
+
# Save the frame as PNG
|
| 103 |
+
frame.save(output_path)
|
| 104 |
+
extracted_files.append(output_filename)
|
| 105 |
+
print(f"Extracted frame {frame_idx} from {webp_path.name}: {output_filename}")
|
| 106 |
+
|
| 107 |
+
except Exception as e:
|
| 108 |
+
print(f"Error extracting frame {frame_idx} from {webp_path}: {e}")
|
| 109 |
+
|
| 110 |
+
return extracted_files
|
| 111 |
+
|
| 112 |
+
except Exception as e:
|
| 113 |
+
print(f"Error processing {webp_path}: {e}")
|
| 114 |
+
return []
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def main():
|
| 118 |
+
"""Main function to randomly extract k frames from WebP files"""
|
| 119 |
+
|
| 120 |
+
# Parse command line arguments
|
| 121 |
+
parser = argparse.ArgumentParser(description='Randomly extract k frames from WebP files')
|
| 122 |
+
parser.add_argument('k', type=int, help='Number of frames to extract')
|
| 123 |
+
parser.add_argument('output_dir', type=str, help='Output directory for extracted frames')
|
| 124 |
+
parser.add_argument('--frames-per-file', type=int, default=3,
|
| 125 |
+
help='Maximum frames to extract per WebP file (default: 3)')
|
| 126 |
+
parser.add_argument('--seed', type=int, help='Random seed for reproducible results')
|
| 127 |
+
|
| 128 |
+
args = parser.parse_args()
|
| 129 |
+
|
| 130 |
+
if args.k <= 0:
|
| 131 |
+
print("Error: k must be a positive integer")
|
| 132 |
+
sys.exit(1)
|
| 133 |
+
|
| 134 |
+
# Set random seed if provided
|
| 135 |
+
if args.seed is not None:
|
| 136 |
+
random.seed(args.seed)
|
| 137 |
+
print(f"Using random seed: {args.seed}")
|
| 138 |
+
|
| 139 |
+
# Set up paths
|
| 140 |
+
script_dir = Path(__file__).parent
|
| 141 |
+
shots_dir = script_dir / "data" / "shots"
|
| 142 |
+
output_dir = Path(args.output_dir)
|
| 143 |
+
|
| 144 |
+
# Check if shots directory exists
|
| 145 |
+
if not shots_dir.exists():
|
| 146 |
+
print(f"Error: Shots directory not found: {shots_dir}")
|
| 147 |
+
sys.exit(1)
|
| 148 |
+
|
| 149 |
+
# Create output directory if it doesn't exist
|
| 150 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
| 151 |
+
print(f"Output directory: {output_dir}")
|
| 152 |
+
|
| 153 |
+
# Find all WebP files
|
| 154 |
+
webp_files = list(shots_dir.glob('sample-*.webp'))
|
| 155 |
+
|
| 156 |
+
if not webp_files:
|
| 157 |
+
print(f"No WebP files found in {shots_dir}")
|
| 158 |
+
sys.exit(1)
|
| 159 |
+
|
| 160 |
+
print(f"Found {len(webp_files)} WebP files available")
|
| 161 |
+
|
| 162 |
+
# Calculate how many files we need to process
|
| 163 |
+
frames_per_file = args.frames_per_file
|
| 164 |
+
files_needed = (args.k + frames_per_file - 1) // frames_per_file # Ceiling division
|
| 165 |
+
files_needed = min(files_needed, len(webp_files))
|
| 166 |
+
|
| 167 |
+
# Randomly select WebP files
|
| 168 |
+
selected_files = random.sample(webp_files, files_needed)
|
| 169 |
+
print(f"Randomly selected {len(selected_files)} WebP files")
|
| 170 |
+
|
| 171 |
+
total_extracted = 0
|
| 172 |
+
extracted_files = []
|
| 173 |
+
|
| 174 |
+
# Process each selected WebP file
|
| 175 |
+
for i, webp_file in enumerate(selected_files):
|
| 176 |
+
remaining_frames = args.k - total_extracted
|
| 177 |
+
if remaining_frames <= 0:
|
| 178 |
+
break
|
| 179 |
+
|
| 180 |
+
# Calculate how many frames to extract from this file
|
| 181 |
+
frames_from_this_file = min(frames_per_file, remaining_frames)
|
| 182 |
+
|
| 183 |
+
print(f"\nProcessing ({i+1}/{len(selected_files)}): {webp_file.name}")
|
| 184 |
+
print(f"Extracting up to {frames_from_this_file} frames...")
|
| 185 |
+
|
| 186 |
+
files = extract_random_frames(webp_file, output_dir, frames_from_this_file)
|
| 187 |
+
extracted_files.extend(files)
|
| 188 |
+
total_extracted += len(files)
|
| 189 |
+
|
| 190 |
+
print(f"\n=== Summary ===")
|
| 191 |
+
print(f"Target frames: {args.k}")
|
| 192 |
+
print(f"Frames extracted: {total_extracted}")
|
| 193 |
+
print(f"Files processed: {len(selected_files)}")
|
| 194 |
+
print(f"Output directory: {output_dir}")
|
| 195 |
+
|
| 196 |
+
if total_extracted < args.k:
|
| 197 |
+
print(f"Warning: Only extracted {total_extracted} frames out of {args.k} requested")
|
| 198 |
+
|
| 199 |
+
print(f"\nExtracted files:")
|
| 200 |
+
for filename in extracted_files:
|
| 201 |
+
print(f" {filename}")
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
if __name__ == "__main__":
|
| 205 |
+
main()
|