Spaces:
Running
Running
Upload main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, WebSocket
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
import cv2
|
| 4 |
+
import base64
|
| 5 |
+
import asyncio
|
| 6 |
+
from inference_sdk import InferenceHTTPClient
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
app.add_middleware(
|
| 11 |
+
CORSMiddleware,
|
| 12 |
+
allow_origins=["*"],
|
| 13 |
+
allow_methods=["*"],
|
| 14 |
+
allow_headers=["*"],
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
CLIENT = InferenceHTTPClient(
|
| 18 |
+
api_url="https://serverless.roboflow.com",
|
| 19 |
+
api_key="Wr4ertX4ONwPGai4nBFc"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
MODEL_ID = "dental-caries-i8vaj-9x6hc/1"
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# ----------------------------------------------------
|
| 26 |
+
# Beautiful fast rounded box
|
| 27 |
+
# ----------------------------------------------------
|
| 28 |
+
def draw_box(img, x1, y1, x2, y2, label):
|
| 29 |
+
color = (0, 220, 0)
|
| 30 |
+
thickness = 2
|
| 31 |
+
|
| 32 |
+
# Box
|
| 33 |
+
cv2.rectangle(img, (x1, y1), (x2, y2), color, thickness, cv2.LINE_AA)
|
| 34 |
+
|
| 35 |
+
# Label background
|
| 36 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
| 37 |
+
scale = 0.6
|
| 38 |
+
thick = 2
|
| 39 |
+
(tw, th), _ = cv2.getTextSize(label, font, scale, thick)
|
| 40 |
+
|
| 41 |
+
label_x1 = x1
|
| 42 |
+
label_y1 = y1 - th - 8
|
| 43 |
+
label_x2 = x1 + tw + 10
|
| 44 |
+
label_y2 = y1
|
| 45 |
+
|
| 46 |
+
overlay = img.copy()
|
| 47 |
+
cv2.rectangle(overlay, (label_x1, label_y1), (label_x2, label_y2), color, -1)
|
| 48 |
+
cv2.addWeighted(overlay, 0.35, img, 0.65, 0, img)
|
| 49 |
+
|
| 50 |
+
cv2.putText(img, label, (x1 + 5, y1 - 5), font, scale,
|
| 51 |
+
(255, 255, 255), thick, cv2.LINE_AA)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# ----------------------------------------------------
|
| 55 |
+
# WebSocket Stream
|
| 56 |
+
# ----------------------------------------------------
|
| 57 |
+
@app.websocket("/ws")
|
| 58 |
+
async def video_stream(websocket: WebSocket):
|
| 59 |
+
await websocket.accept()
|
| 60 |
+
cap = cv2.VideoCapture(0)
|
| 61 |
+
|
| 62 |
+
frame_count = 0
|
| 63 |
+
last_prediction = [] # cached detections
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
while True:
|
| 67 |
+
ret, frame = cap.read()
|
| 68 |
+
if not ret:
|
| 69 |
+
break
|
| 70 |
+
|
| 71 |
+
# ------------------------------
|
| 72 |
+
# Reduce resolution (much faster)
|
| 73 |
+
# ------------------------------
|
| 74 |
+
small = cv2.resize(frame, (640, 480))
|
| 75 |
+
|
| 76 |
+
# ------------------------------
|
| 77 |
+
# Run inference only every 2 frames
|
| 78 |
+
# ------------------------------
|
| 79 |
+
if frame_count % 2 == 0:
|
| 80 |
+
_, buf = cv2.imencode(".jpg", small, [cv2.IMWRITE_JPEG_QUALITY, 70])
|
| 81 |
+
img_b64 = base64.b64encode(buf).decode("utf-8")
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
result = CLIENT.infer(
|
| 85 |
+
img_b64,
|
| 86 |
+
model_id=MODEL_ID
|
| 87 |
+
)
|
| 88 |
+
last_prediction = result["predictions"]
|
| 89 |
+
except:
|
| 90 |
+
pass
|
| 91 |
+
|
| 92 |
+
frame_count += 1
|
| 93 |
+
|
| 94 |
+
# ------------------------------
|
| 95 |
+
# Draw cached predictions (smooth FPS)
|
| 96 |
+
# ------------------------------
|
| 97 |
+
for det in last_prediction:
|
| 98 |
+
x1 = int(det["x"] - det["width"] / 2)
|
| 99 |
+
y1 = int(det["y"] - det["height"] / 2)
|
| 100 |
+
x2 = int(det["x"] + det["width"] / 2)
|
| 101 |
+
y2 = int(det["y"] + det["height"] / 2)
|
| 102 |
+
|
| 103 |
+
label = f"{det['class']} {det['confidence']:.2f}"
|
| 104 |
+
draw_box(frame, x1, y1, x2, y2, label)
|
| 105 |
+
|
| 106 |
+
# ------------------------------
|
| 107 |
+
# Send back to frontend
|
| 108 |
+
# ------------------------------
|
| 109 |
+
_, final_buf = cv2.imencode(".jpg", frame, [cv2.IMWRITE_JPEG_QUALITY, 70])
|
| 110 |
+
encoded = base64.b64encode(final_buf).decode("utf-8")
|
| 111 |
+
|
| 112 |
+
await websocket.send_text(encoded)
|
| 113 |
+
|
| 114 |
+
await asyncio.sleep(0.02) # ~50 FPS output
|
| 115 |
+
|
| 116 |
+
except Exception as e:
|
| 117 |
+
print("Connection closed:", e)
|
| 118 |
+
|
| 119 |
+
finally:
|
| 120 |
+
cap.release()
|
| 121 |
+
await websocket.close()
|