MengHsir commited on
Commit
c988771
·
verified ·
1 Parent(s): 0136328

Upload Action_Genome_data/dump_frames.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. Action_Genome_data/dump_frames.py +61 -0
Action_Genome_data/dump_frames.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import argparse
3
+ import warnings
4
+ from tqdm import tqdm
5
+
6
+
7
+ def dump_frames(args):
8
+ video_dir = args.video_dir
9
+ frame_dir = args.frame_dir
10
+ annotation_dir = args.annotation_dir
11
+ all_frames = args.all_frames
12
+
13
+ # Load the list of annotated frames
14
+ frame_list = []
15
+ with open(os.path.join(annotation_dir, 'frame_list.txt'), 'r') as f:
16
+ for frame in f:
17
+ frame_list.append(frame.rstrip('\n'))
18
+
19
+ # Create video to frames mapping
20
+ video2frames = {}
21
+ for path in frame_list:
22
+ video, frame = path.split('/')
23
+ if video not in video2frames:
24
+ video2frames[video] = []
25
+ video2frames[video].append(frame)
26
+
27
+ # For each video, dump frames.
28
+ for v in tqdm(video2frames):
29
+ curr_frame_dir = os.path.join(frame_dir, v)
30
+ if not os.path.exists(curr_frame_dir):
31
+ os.makedirs(curr_frame_dir)
32
+ # Use ffmpeg to extract frames. Different versions of ffmpeg may generate slightly different frames.
33
+ # We used ffmpeg 2.8.15 to dump our frames.
34
+ # Note that the frames are extracted according to their original video FPS, which is not always 24.
35
+ # Therefore, our frame indices are different from Charades extracted frames' indices.
36
+ os.system('ffmpeg -loglevel panic -i %s/%s %s/%%06d.png' % (video_dir, v, curr_frame_dir))
37
+
38
+ # if not keeping all frames, only keep the annotated frames included in frame_list.txt
39
+ if not all_frames:
40
+ keep_frames = video2frames[v]
41
+ frames_to_delete = set(os.listdir(curr_frame_dir)) - set(keep_frames)
42
+ for frame in frames_to_delete:
43
+ os.remove(os.path.join(curr_frame_dir, frame))
44
+ else:
45
+ warnings.warn('Frame directory %s already exists. Skipping dumping into this directory.' % curr_frame_dir,
46
+ RuntimeWarning)
47
+
48
+
49
+ if __name__ == "__main__":
50
+ parser = argparse.ArgumentParser(description="Dump frames")
51
+ parser.add_argument("--video_dir", default="./videos",
52
+ help="Folder containing Charades videos.")
53
+ parser.add_argument("--frame_dir", default="./frames",
54
+ help="Root folder containing frames to be dumped.")
55
+ parser.add_argument("--annotation_dir", default="./annotations",
56
+ help=("Folder containing annotation files, including object_bbox_and_relationship.pkl, "
57
+ "person_bbox.pkl and frame_list.txt."))
58
+ parser.add_argument("--all_frames", action="store_true",
59
+ help="Set if you want to dump all frames, rather than the frames listed in frame_list.txt")
60
+ args = parser.parse_args()
61
+ dump_frames(args)