-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract_frames.py
64 lines (50 loc) · 2.13 KB
/
extract_frames.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
'''
For HMDB51 and UCF101 datasets:
Code extracts frames from video at a rate of 25fps and scaling the
larger dimension of the frame is scaled to 256 pixels.
After extraction of all frames write a "done" file to signify proper completion
of frame extraction.
Usage:
python extract_frames.py video_dir frame_dir
video_dir => path of video files
frame_dir => path of extracted jpg frames
'''
import sys, os, pdb
import numpy as np
import subprocess
from tqdm import tqdm
def extract(vid_dir, frame_dir, redo=False):
class_list = sorted(os.listdir(vid_dir))
print("Classes =", class_list)
for ic,cls in enumerate(class_list):
vlist = sorted(os.listdir(vid_dir + cls))
print("")
print(ic+1, len(class_list), cls, len(vlist))
print("")
for v in tqdm(vlist):
outdir = os.path.join(frame_dir, cls, v[:-4])
# Checking if frames already extracted
if os.path.isfile( os.path.join(outdir, 'done') ) and not redo: continue
try:
os.system('mkdir -p "%s"'%(outdir))
# check if horizontal or vertical scaling factor
o = subprocess.check_output('ffprobe -v error -show_entries stream=width,height -of default=noprint_wrappers=1 "%s"'%(os.path.join(vid_dir, cls, v)), shell=True).decode('utf-8')
lines = o.splitlines()
width = int(lines[0].split('=')[1])
height = int(lines[1].split('=')[1])
resize_str = '-1:256' if width>height else '256:-1'
# extract frames
os.system('ffmpeg -i "%s" -r 10 -q:v 2 -vf "scale=%s" "%s" > /dev/null 2>&1'%( os.path.join(vid_dir, cls, v), resize_str, os.path.join(outdir, '%05d.jpg')))
nframes = len([ fname for fname in os.listdir(outdir) if fname.endswith('.jpg') and len(fname)==9])
if nframes==0: raise Exception
os.system('touch "%s"'%(os.path.join(outdir, 'done') ))
except:
print("ERROR", cls, v)
if __name__ == '__main__':
# vid_dir = sys.argv[1]
# frame_dir = sys.argv[2]
# start = int(sys.argv[3])
# end = int(sys.argv[4])
vid_dir = './mice_2020-03-20/'
frame_dir = './mice_0320-1f/'
extract(vid_dir, frame_dir, redo=True)