-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathvideo.py
More file actions
113 lines (98 loc) · 3.74 KB
/
video.py
File metadata and controls
113 lines (98 loc) · 3.74 KB
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
104
105
106
107
108
109
110
# Copyright 2019 The Vearch Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# ==============================================================================
import json
import sys
import time
import os
import requests
import subprocess
import config
import exceptions
def installFFmpeg():
"""install ffmpeg method"""
p = subprocess.call('bash ../bin/centos_yum_install_ffmpeg.sh', shell=True)
if p == 0:
raise exceptions.InstallError('FFmpeg install failed!')
print('FFmpeg install success!')
def create_DB_space():
db = dict(name=config.video['db'])
space = dict(name=config.video['space'],
dynamic_schema='strict',
partition_num=2,
replica_num=1,
engine=dict(name='gamma',
metric_type='InnerProduct'),
properties=dict(url=dict(type='keyword',
index='true'),
feature=dict(type='vector',
dimension=512,
format='normalization'
)
)
)
headers = {'content-type':'application/json'}
ip = '%s/db/_create'%(config.video['ip'])
res = requests.put(ip, data=json.dumps(db), headers=headers)
ip = '%s/space/%s/_create' % (config.video['ip'], db['name'])
res = requests.put(ip, data=json.dumps(db), headers=headers)
return res.json()
def run():
main_p = subprocess.Popen(f'python main.py --model_name=face_retrieval', shell=True)
process_list.append(main_p)
# wait 10s for main_p load model
time.sleep(10)
res = create_DB_space()
if res['code'] in [200, 550, 561]:
print(res)
else:
raise exceptions.CreateDBAndSpaceError()
imgpath = config.video['imagepath']
isExists = os.path.exists(imgpath)
path = config.video['videopath']
if not isExists:
os.makedirs(imgpath)
ffmpeg_p = subprocess.Popen(
f"ffmpeg -i {path} -vf select='eq(pict_type\,I)' -vsync 2 -f image2 {imgpath} img-%d.jpg", shell=True)
process_list.append(ffmpeg_p)
count = 1
insert_ip = f'%s:%d/%s/%s'%(config.video['ip'], config.port, config.video['db'], config.video['space'])
insert_data = dict(url=None, feature=dict(feature=None))
headers = {'content-type':'application/json'}
while True:
if main_p.poll() is not None:
raise Exception('main process is killed!')
if ffmpeg_p.poll() is not None:
raise Exception('ffmpeg process is killed!')
imagefile = imgpath + f'/img-{count}.jpg'
if os.path.exists(imagefile):
insert_data['url'] = imagefile
insert_data['feature']['feature'] = imagefile
res = requests.post(insert_ip, data=json.dumps(insert_data), headers=headers)
count += 1
time.sleep(1)
def clear():
for p in process_list:
if p.poll() is None:
p.terminate()
sys.exit(1)
if __name__ == '__main__':
# judge if ffmpeg has been installed
p = subprocess.run('ffmpeg -version', shell=True)
if p.returncode != 0:
installFFmpeg()
process_list = []
try:
run()
except BaseException as err:
print(err)
clear()