-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
46 lines (37 loc) · 1.01 KB
/
app.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
from fastapi import FastAPI, WebSocket
from fastapi.responses import StreamingResponse
from stream import Camera
from model import Pipeline
app = FastAPI()
camera = Camera()
pipeline = Pipeline()
@app.get("/")
def root():
return {"Hello": "World"}
@app.get("/stream")
def stream():
return StreamingResponse(
camera.generate(),
headers={"Cache-Control": "no-cache, private", "Pragma": "no-cache"},
media_type="multipart/x-mixed-replace; boundary=frame",
)
@app.websocket("/detect")
async def detect(websocket: WebSocket):
await websocket.accept()
try:
while True:
res = camera.detect()
if res:
await websocket.send_text('1')
else:
await websocket.send_text('0')
except Exception as e:
print(e)
finally:
await websocket.close()
@app.get("/predict")
def predict():
img = camera.cam.capture_image()
img = img.convert("RGB")
res = pipeline.predict_image(img)
return res