Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
12x.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:682ce8dadee004dbe964950f1bf3eda451671815a6ed62db80b398916b9b7c6f
|
3 |
+
size 119322638
|
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, WebSocket
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from ultralytics import YOLO
|
5 |
+
import base64
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Initialize FastAPI app
|
9 |
+
app = FastAPI()
|
10 |
+
|
11 |
+
# Load YOLO model (Ensure 12x.pt exists)
|
12 |
+
model_path = "12x.pt"
|
13 |
+
if not os.path.exists(model_path):
|
14 |
+
raise FileNotFoundError(f"Model file '{model_path}' not found. Please place it in the project directory.")
|
15 |
+
|
16 |
+
model = YOLO(model_path)
|
17 |
+
|
18 |
+
|
19 |
+
def process_frame(frame):
|
20 |
+
results = model(frame)
|
21 |
+
predictions = []
|
22 |
+
object_count = {}
|
23 |
+
|
24 |
+
for result in results:
|
25 |
+
for box in result.boxes:
|
26 |
+
class_name = result.names[int(box.cls)]
|
27 |
+
predictions.append({
|
28 |
+
"class": class_name,
|
29 |
+
"confidence": float(box.conf),
|
30 |
+
"bbox": [float(x) for x in box.xyxy[0]]
|
31 |
+
})
|
32 |
+
|
33 |
+
# Count objects
|
34 |
+
object_count[class_name] = object_count.get(class_name, 0) + 1
|
35 |
+
|
36 |
+
return predictions, object_count
|
37 |
+
|
38 |
+
|
39 |
+
@app.websocket("/ws")
|
40 |
+
async def websocket_endpoint(websocket: WebSocket):
|
41 |
+
await websocket.accept()
|
42 |
+
|
43 |
+
cap = cv2.VideoCapture(0) # Open webcam
|
44 |
+
if not cap.isOpened():
|
45 |
+
await websocket.send_json({"error": "Could not open webcam"})
|
46 |
+
await websocket.close()
|
47 |
+
return
|
48 |
+
|
49 |
+
try:
|
50 |
+
while True:
|
51 |
+
ret, frame = cap.read()
|
52 |
+
if not ret:
|
53 |
+
break
|
54 |
+
|
55 |
+
predictions, object_count = process_frame(frame)
|
56 |
+
|
57 |
+
# Draw bounding boxes on the frame
|
58 |
+
for pred in predictions:
|
59 |
+
x1, y1, x2, y2 = map(int, pred["bbox"])
|
60 |
+
label = f"{pred['class']} ({pred['confidence']:.2f})"
|
61 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
62 |
+
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
63 |
+
|
64 |
+
# Encode the frame to send over WebSocket
|
65 |
+
_, buffer = cv2.imencode('.jpg', frame)
|
66 |
+
frame_base64 = base64.b64encode(buffer).decode('utf-8')
|
67 |
+
|
68 |
+
# Send frame and object count to frontend
|
69 |
+
await websocket.send_json({"frame": frame_base64, "object_count": object_count})
|
70 |
+
|
71 |
+
except Exception as e:
|
72 |
+
print(f"WebSocket Error: {e}")
|
73 |
+
await websocket.send_json({"error": str(e)})
|
74 |
+
finally:
|
75 |
+
cap.release()
|
76 |
+
await websocket.close()
|
77 |
+
|
78 |
+
|
79 |
+
@app.get("/")
|
80 |
+
def home():
|
81 |
+
return {"message": "Real-Time Object Detection API using 12x.pt"}
|