Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import Request
|
| 2 |
+
|
| 3 |
+
@app.get("/video_feed")
|
| 4 |
+
def video_feed(request: Request):
|
| 5 |
+
conf = float(request.query_params.get("conf", 0.25))
|
| 6 |
+
iou = float(request.query_params.get("iou", 0.45))
|
| 7 |
+
cam_id = int(request.query_params.get("camera", 0))
|
| 8 |
+
|
| 9 |
+
cap = cv2.VideoCapture(cam_id)
|
| 10 |
+
|
| 11 |
+
def gen_frames():
|
| 12 |
+
while True:
|
| 13 |
+
success, frame = cap.read()
|
| 14 |
+
if not success:
|
| 15 |
+
break
|
| 16 |
+
results = model.track(frame, conf=conf, iou=iou, persist=True, tracker="botsort.yaml")
|
| 17 |
+
annotated = results[0].plot()
|
| 18 |
+
_, buffer = cv2.imencode(".jpg", annotated)
|
| 19 |
+
yield (b"--frame\r\n"
|
| 20 |
+
b"Content-Type: image/jpeg\r\n\r\n" + buffer.tobytes() + b"\r\n")
|
| 21 |
+
|
| 22 |
+
return StreamingResponse(gen_frames(), media_type="multipart/x-mixed-replace; boundary=frame")
|