|
| 1 | +""" |
| 2 | +Helper script for extracting frames from the UCF-101 dataset |
| 3 | +""" |
| 4 | + |
| 5 | +import av |
| 6 | +import glob |
| 7 | +import os |
| 8 | +import time |
| 9 | +import tqdm |
| 10 | +import datetime |
| 11 | +import argparse |
| 12 | + |
| 13 | + |
| 14 | +def extract_frames(video_path): |
| 15 | + frames = [] |
| 16 | + video = av.open(video_path) |
| 17 | + for frame in video.decode(0): |
| 18 | + yield frame.to_image() |
| 19 | + |
| 20 | + |
| 21 | +prev_time = time.time() |
| 22 | +if __name__ == "__main__": |
| 23 | + parser = argparse.ArgumentParser() |
| 24 | + parser.add_argument("--dataset_path", type=str, default="UCF-101", help="Path to UCF-101 dataset") |
| 25 | + opt = parser.parse_args() |
| 26 | + print(opt) |
| 27 | + |
| 28 | + time_left = 0 |
| 29 | + video_paths = glob.glob(os.path.join(opt.dataset_path, "*", "*.avi")) |
| 30 | + for i, video_path in enumerate(video_paths): |
| 31 | + sequence_type, sequence_name = video_path.split(".avi")[0].split("/")[-2:] |
| 32 | + sequence_path = os.path.join(f"{opt.dataset_path}-frames", sequence_type, sequence_name) |
| 33 | + |
| 34 | + if os.path.exists(sequence_path): |
| 35 | + continue |
| 36 | + |
| 37 | + os.makedirs(sequence_path, exist_ok=True) |
| 38 | + |
| 39 | + # Extract frames |
| 40 | + for j, frame in enumerate( |
| 41 | + tqdm.tqdm( |
| 42 | + extract_frames(video_path), |
| 43 | + desc=f"[{i}/{len(video_paths)}] {sequence_name} : ETA {time_left}", |
| 44 | + ) |
| 45 | + ): |
| 46 | + frame.save(os.path.join(sequence_path, f"{j}.jpg")) |
| 47 | + |
| 48 | + # Determine approximate time left |
| 49 | + videos_left = len(video_paths) - (i + 1) |
| 50 | + time_left = datetime.timedelta(seconds=videos_left * (time.time() - prev_time)) |
| 51 | + prev_time = time.time() |
0 commit comments