-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathdecode.py
74 lines (60 loc) · 2.23 KB
/
decode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
########################################################################
# YouTube BoundingBox Decoder
########################################################################
#
# This script takes all of the videos within the YouTube BoundingBoxes
# dataset and parses them out into single frames for easy input to
# single-frame benchmarks. The videos must have been previously
# downloaded by the accompanying downloader script. Frames are decoded
# at 30 frames per second as per the dataset creators' recommendation.
#
# Author: Mark Buckler
#
########################################################################
#
# The data is placed into the following directory structure:
#
# /frames/d_set/class_id/clip_name/frame_idx.jpeg
#
# Note that clip name is defined as follows:
#
# clip_name = yt_id:class_id:object_id
#
########################################################################
from subprocess import check_call
from ffmpy import FFmpeg
import csv
import os
import sys
import glob
def decode(vid_dir='videos', frame_dir='frames'):
"""Using the videos downloaded in `vid_dir`, produce decoded frames in
`frame_dir`.
"""
# List the datasets
d_sets = [f.path for f in os.scandir(vid_dir) if f.is_dir() ]
# For each dataset
for d_set in d_sets:
# List the classes
classes = [f.path for f in os.scandir(d_set) if f.is_dir() ]
# For each class
for class_ in classes:
# List the clips
clips = [f.path for f in os.scandir(class_) ]
# For each clip, where clip is the path to the clip
for clip in clips:
clip_sub_dir = (clip.replace(vid_dir,''))[:-4]
clip_out_dir = frame_dir + clip_sub_dir
clip_name = clip.split('/')[-1][:-4]
# Change to the class directory (where the clip is)
os.chdir(class_)
# Decode the video into 30 fps frames with ffmpeg
check_call(['ffmpeg', '-i', 'file:'+clip_name+'.mp4', '-vf', 'fps=30',
'frame_%06d.jpg'])
# Create a directory for this clip
check_call(['mkdir', '-p', clip_out_dir])
# Move the results to the output directory
for jpg in glob.glob('*.jpg'):
check_call(['mv', jpg, clip_out_dir])
if __name__ == '__main__':
decode(*sys.argv[1:])