-
Notifications
You must be signed in to change notification settings - Fork 142
/
run_wsgi.py
65 lines (50 loc) · 1.36 KB
/
run_wsgi.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
import time
import uvicorn
from fastapi import FastAPI
from omdet.inference.det_engine import DetEngine
from pydantic import BaseModel
from typing import List, Dict, Union
class InfDetectBody(BaseModel):
model_id: str
data: List[str]
src_type: str = "url"
task: str
labels: List[str]
threshold: float = 0.1
nms_threshold: float = 0.5
class Object(BaseModel):
xmin: float
ymin: float
xmax: float
ymax: float
conf: float
label: str
class DetectionRes(BaseModel):
took: int
objects: List[List[Object]] = []
app = FastAPI()
@app.on_event("startup")
async def startup_event():
app.state.detector = DetEngine(model_dir="resources/", device="cuda", batch_size=10)
@app.post(
"/inf_predict",
response_model=DetectionRes,
name="Detect objects with Inf Possibilities",
)
async def detect_urls(
body: InfDetectBody = None,
) -> DetectionRes:
s_time = time.time()
out = app.state.detector.inf_predict(
body.model_id,
task=body.task,
labels=body.labels,
data=body.data,
src_type=body.src_type,
conf_threshold=body.threshold,
nms_threshold=body.nms_threshold,
)
resp = DetectionRes(took=int((time.time() - s_time) * 1000), objects=out)
return resp
if __name__ == "__main__":
uvicorn.run("run_wsgi:app", host="0.0.0.0", port=8000)