-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_video_cache.py
executable file
·103 lines (74 loc) · 2.66 KB
/
generate_video_cache.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
96
97
98
99
100
101
102
103
#!/usr/bin/python3
from facial_engine import FacialEngine
import argparse
import csv
import magic
import os
import re
def process_one_video(video_path, csv_policy):
_, filename = os.path.split(video_path)
print('process_one_video: %s' % (filename))
engine = FacialEngine(video_path)
if engine.init() == False:
print(' fail to init engine')
return False
ret = engine.configure(csv_policy = csv_policy)
if ret == False:
print(' fail to configure engine')
return False
ret = engine.process_video()
if ret == False:
print(' fail to process video')
return False
print(' success')
return True
def process_training_csv(csv_path):
_, filename = os.path.split(csv_path)
print('process_training_csv: %s' % (filename))
with open(csv_path, 'r', newline = '') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
# update the csv if found
ret = process_one_video(row['file_name'], 'update')
if ret == False:
return False
print(' success')
return True
def main():
parser = argparse.ArgumentParser()
parser.add_argument('path', help = 'path to a video file, a directory, or a training recipe file')
args = parser.parse_args()
input_video_path = args.path
print('Input path = %s' % (input_video_path))
_, ext = os.path.splitext(input_video_path)
if ext == '.csv':
# could be a training recipe
process_training_csv(input_video_path)
elif os.path.isfile(input_video_path) != False:
mime = magic.Magic(mime=True)
file_mine = mime.from_file(input_video_path)
if file_mine.find('video') == -1:
print(' not a video file')
return False
# update the csv if found
ret = process_one_video(input_video_path, 'update')
elif os.path.isdir(input_video_path):
# looking for any video file which:
# 1. file name ends with a 'A' or 'B' or 'D' character
# 2. in the SJCAM subdirectory
prog = re.compile(r'.*/SJCAM/.*[ABD]\..+')
mime = magic.Magic(mime=True)
for root, dirs, files in os.walk(input_video_path):
for file in files:
file_path = os.path.join(root, file)
file_mine = mime.from_file(file_path)
if file_mine.find('video') == -1:
continue
if prog.match(file_path) == None:
continue
ret = process_one_video(file_path, 'abort')
else:
print('Unrecognized path')
return
if __name__ == '__main__':
main()