forked from dluvizon/deephar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract-resize-videos.py
96 lines (69 loc) · 2.71 KB
/
extract-resize-videos.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python2
import os
import sys
import glob
import threading
import numpy as np
from PIL import Image
import cv2
resize_ratio = 1/2.
def mkdir(path):
if os.path.isdir(path) is False:
os.mkdir(path)
def extract_video(video_file, output_dir, frames, jpeg_quality=95):
try:
vidcap = cv2.VideoCapture(video_file)
except Exception as e:
sys.stderr.write(str(e) + '\n')
sys.stderr.write('Error loading file "{}"\n'.format(video_file))
return
mkdir(output_dir)
sys.stdout.write('Extracting video "{}"\n'.format(video_file))
sys.stdout.flush()
f = 0
while True:
success, image = vidcap.read()
f += 1
if not success:
break
if f not in frames:
continue # Skip if not in the frame list
ncols, nrows, rgb = image.shape
dsize = (int(resize_ratio*nrows), int(resize_ratio*ncols))
image = cv2.resize(image, dsize, interpolation=cv2.INTER_CUBIC)
frame_file = os.path.join(output_dir, '%05d.jpg' % f)
cv2.imwrite(frame_file, image,
[cv2.IMWRITE_JPEG_QUALITY, jpeg_quality])
vidcap.release()
def extract_resize_videos(input_path, output_path, annot_path, subjects,
num_C=3, num_P=40, num_R=2, num_A=60):
if os.path.isdir(input_path) is False:
raise Exception('Could not find the "{}" folder!'.format(input_path))
mkdir(output_path)
for s in subjects:
for c in range(1,num_C+1):
for p in range(1,num_P+1):
for r in range(1,num_R+1):
for a in range(1,num_A+1):
sequence_id = 'S%03dC%03dP%03dR%03dA%03d' \
% (s, c, p, r, a)
video_file = os.path.join(input_path,
sequence_id + '_rgb.avi')
annot_file = os.path.join(annot_path,
sequence_id + '.npy')
if not os.path.isfile(annot_file):
continue # Ignore missing annotation files
annot = np.load(annot_file)
# frames = annot[1::2,0] # Divide video fps by 2
frames = annot[:,0]
output_dir = os.path.join(output_path, sequence_id)
extract_video(video_file, output_dir, frames)
if __name__ == "__main__":
try:
assert len(sys.argv) == 2, \
'Expected 1 parameter, got {}'.format(sys.argv)
setlist = [int(sys.argv[-1])]
extract_resize_videos(
'nturgb+d_rgb', 'images-small', 'nturgb+d_numpy', setlist)
except Exception as e:
print (e)