-
Notifications
You must be signed in to change notification settings - Fork 417
/
vid2img_kinetics.py
executable file
·78 lines (64 loc) · 2.45 KB
/
vid2img_kinetics.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
# Code for "TSM: Temporal Shift Module for Efficient Video Understanding"
# arXiv:1811.08383
# Ji Lin*, Chuang Gan, Song Han
# {jilin, songhan}@mit.edu, [email protected]
from __future__ import print_function, division
import os
import sys
import subprocess
from multiprocessing import Pool
from tqdm import tqdm
n_thread = 100
def vid2jpg(file_name, class_path, dst_class_path):
if '.mp4' not in file_name:
return
name, ext = os.path.splitext(file_name)
dst_directory_path = os.path.join(dst_class_path, name)
video_file_path = os.path.join(class_path, file_name)
try:
if os.path.exists(dst_directory_path):
if not os.path.exists(os.path.join(dst_directory_path, 'img_00001.jpg')):
subprocess.call('rm -r \"{}\"'.format(dst_directory_path), shell=True)
print('remove {}'.format(dst_directory_path))
os.mkdir(dst_directory_path)
else:
print('*** convert has been done: {}'.format(dst_directory_path))
return
else:
os.mkdir(dst_directory_path)
except:
print(dst_directory_path)
return
cmd = 'ffmpeg -i \"{}\" -threads 1 -vf scale=-1:331 -q:v 0 \"{}/img_%05d.jpg\"'.format(video_file_path, dst_directory_path)
# print(cmd)
subprocess.call(cmd, shell=True,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
def class_process(dir_path, dst_dir_path, class_name):
print('*' * 20, class_name, '*'*20)
class_path = os.path.join(dir_path, class_name)
if not os.path.isdir(class_path):
print('*** is not a dir {}'.format(class_path))
return
dst_class_path = os.path.join(dst_dir_path, class_name)
if not os.path.exists(dst_class_path):
os.mkdir(dst_class_path)
vid_list = os.listdir(class_path)
vid_list.sort()
p = Pool(n_thread)
from functools import partial
worker = partial(vid2jpg, class_path=class_path, dst_class_path=dst_class_path)
for _ in tqdm(p.imap_unordered(worker, vid_list), total=len(vid_list)):
pass
# p.map(worker, vid_list)
p.close()
p.join()
print('\n')
if __name__ == "__main__":
dir_path = sys.argv[1]
dst_dir_path = sys.argv[2]
class_list = os.listdir(dir_path)
class_list.sort()
for class_name in class_list:
class_process(dir_path, dst_dir_path, class_name)
class_name = 'test'
class_process(dir_path, dst_dir_path, class_name)