-
Notifications
You must be signed in to change notification settings - Fork 260
/
main.py
68 lines (56 loc) · 1.83 KB
/
main.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
import os
import sys
import json
import subprocess
import numpy as np
import torch
from torch import nn
from opts import parse_opts
from model import generate_model
from mean import get_mean
from classify import classify_video
if __name__=="__main__":
opt = parse_opts()
opt.mean = get_mean()
opt.arch = '{}-{}'.format(opt.model_name, opt.model_depth)
opt.sample_size = 112
opt.sample_duration = 16
opt.n_classes = 400
model = generate_model(opt)
print('loading model {}'.format(opt.model))
model_data = torch.load(opt.model)
assert opt.arch == model_data['arch']
model.load_state_dict(model_data['state_dict'])
model.eval()
if opt.verbose:
print(model)
input_files = []
with open(opt.input, 'r') as f:
for row in f:
input_files.append(row[:-1])
class_names = []
with open('class_names_list') as f:
for row in f:
class_names.append(row[:-1])
ffmpeg_loglevel = 'quiet'
if opt.verbose:
ffmpeg_loglevel = 'info'
if os.path.exists('tmp'):
subprocess.call('rm -rf tmp', shell=True)
outputs = []
for input_file in input_files:
video_path = os.path.join(opt.video_root, input_file)
if os.path.exists(video_path):
print(video_path)
subprocess.call('mkdir tmp', shell=True)
subprocess.call('ffmpeg -i {} tmp/image_%05d.jpg'.format(video_path),
shell=True)
result = classify_video('tmp', input_file, class_names, model, opt)
outputs.append(result)
subprocess.call('rm -rf tmp', shell=True)
else:
print('{} does not exist'.format(input_file))
if os.path.exists('tmp'):
subprocess.call('rm -rf tmp', shell=True)
with open(opt.output, 'w') as f:
json.dump(outputs, f)