-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
186 lines (159 loc) · 5.81 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import copy
import time
import traceback
import uvicorn
from pydantic import BaseModel
from fastapi import FastAPI, Form
import util
from main_poster import *
from model.boundary.detect import BoundaryDetector
from model.object_detection.detect import ObjectDetector
from model.car.recognize import CarRecognizer
from model.asr.recogize import SpeechRecognizer, speechRecognizer
from model.asr.TextToSpeech import TTS
# from model.asr.realtime import run_server
from model.face.rocognize import FaceHandler
from model.image_process.process import ImageProcesser
from model.scene.detect import SceneRecognizer
import warnings
warnings.filterwarnings("ignore")
# 全局变量
controller = {}
app = FastAPI()
# 视频分析接口
@app.post('/startModel')
def startModel(analysisType=Form(None),
businessId=Form(None),
url=Form(None),
file=Form(None),
modelType=Form(None),
position=Form(None),
modelStopCode=Form(None)
):
response = copy.deepcopy(startModelReturn)
request = StartModelRequest(analysisType=analysisType,
businessId=businessId,
url=url,
file=file,
modelType=modelType,
position=position,
modelStopCode=modelStopCode)
if request.position:
request.position = json.loads(request.position)
print(request.position)
modelPostHelper = ModelPostHelper(request)
# 模型运行控制器
global controller
controller[request.modelStopCode] = Controller()
try:
if request.modelType == '1': # 周界检测
task = BoundaryDetector(modelPostHelper, controller[request.modelStopCode])
elif request.modelType == '2' or request.modelType == '4': # 目标检测
task = ObjectDetector(modelPostHelper, controller[request.modelStopCode])
elif request.modelType == '3': # 车辆识别
task = CarRecognizer(modelPostHelper, controller[request.modelStopCode])
elif request.modelType == '5': # 人脸/人物识别
task = FaceHandler(modelPostHelper, controller[request.modelStopCode])
elif request.modelType == '6': # 场景识别
task = SceneRecognizer(modelPostHelper, controller[request.modelStopCode])
else:
raise Exception("Called unimplemented model type.")
task_thread = threading.Thread(target=task.run)
task_thread.start()
except Exception as e:
tb = traceback.format_exc()
response["message"] = "模型启动失败"
response["code"] = "1"
response["tail"] = str(tb)
print(tb)
else:
response["message"] = "模型启动成功"
response["code"] = "0"
response["timestamp"] = time.time()
return response
@app.post('/stopModel')
def stopModel(modelStopCode=Form(None)):
response = copy.deepcopy(stopModelReturn)
try:
global controller
controller[modelStopCode].stop()
controller.pop(modelStopCode)
except Exception as e:
tb = traceback.format_exc()
response["message"] = "关闭模型失败"
response["code"] = 1
response["tail"] = str(tb)
print(tb)
else:
response["message"] = "关闭模型成功"
response["code"] = 0
response["timestamp"] = time.time()
return response
# 语音识别接口
@app.post('/speech/recognize')
def speechRecognize(data=Form(None)):
print(data)
data = json.loads(data)
response = copy.deepcopy(start_asr_return)
try:
global speechRecognizer
if not isinstance(speechRecognizer, SpeechRecognizer):
speechRecognizer = SpeechRecognizer()
detect_thread = threading.Thread(target=speechRecognizer.run, args=[data])
detect_thread.start()
except Exception as e:
tb = traceback.format_exc()
response["message"] = "语音识别模型启动失败"
response["code"] = 1
response["tail"] = str(tb)
print(tb)
else:
response["message"] = "语音识别模型启动成功"
response["code"] = 0
response["timestamp"] = time.time()
return response
# 语音合成接口
@app.post('/speech/systhesis')
def speechRecognize(rate=Form(None),
volumeLevel=Form(None),
text=Form(None)):
response = copy.deepcopy(start_asr_return)
try:
save_path = './tmp/ss.mp3'
TTS.run(text, save_path, rate, volumeLevel)
except Exception as e:
tb = traceback.format_exc()
response["message"] = "语音合成模型启动失败"
response["code"] = 1
response["tail"] = str(tb)
print(tb)
else:
response["message"] = "语音合成模型启动成功"
response["code"] = 0
response["timestamp"] = time.time()
return response
# 图像处理接口
@app.post('/image/process')
def speechRecognize(file=Form(None),
modelType=Form(None)):
print("Image-process modelType:", modelType)
response = copy.deepcopy(image_process_return)
try:
response["data"] = ImageProcesser.run(file, modelType)
except Exception as e:
tb = traceback.format_exc()
response["message"] = "图像处理失败"
response["code"] = 1
response["tail"] = str(tb)
print(tb)
else:
response["message"] = "图像处理成功"
response["code"] = 0
print("Finished Image-process modelType:", modelType)
response["timestamp"] = time.time()
return response
if __name__ == '__main__':
uvicorn.run(app=app,
host="0.0.0.0",
port=8080,
workers=1)