Spaces:
Running
Running
Upload 4 files
Browse files
DOCKER
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11
|
| 2 |
+
WORKDIR /app
|
| 3 |
+
COPY requirements.txt .
|
| 4 |
+
RUN pip install -r requirements.txt
|
| 5 |
+
COPY . .
|
| 6 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, Form
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
import numpy as np
|
| 6 |
+
import cv2
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# Allow your Vercel app to call this API
|
| 11 |
+
app.add_middleware(
|
| 12 |
+
CORSMiddleware,
|
| 13 |
+
allow_origins=["*"], # replace * with your vercel domain in production
|
| 14 |
+
allow_methods=["*"],
|
| 15 |
+
allow_headers=["*"],
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
model = YOLO("best.pt")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@app.post("/detect")
|
| 22 |
+
async def detect(
|
| 23 |
+
file: UploadFile = File(...),
|
| 24 |
+
confidence: float = Form(default=0.5), # 0.0 - 1.0, matches Roboflow slider default
|
| 25 |
+
overlap: float = Form(default=0.5), # NMS IoU threshold, same as Roboflow overlap slider
|
| 26 |
+
):
|
| 27 |
+
# Clamp values to valid range
|
| 28 |
+
confidence = max(0.01, min(1.0, confidence))
|
| 29 |
+
overlap = max(0.01, min(1.0, overlap))
|
| 30 |
+
|
| 31 |
+
contents = await file.read()
|
| 32 |
+
nparr = np.frombuffer(contents, np.uint8)
|
| 33 |
+
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 34 |
+
|
| 35 |
+
if img is None:
|
| 36 |
+
return JSONResponse({"error": "Could not decode image"}, status_code=400)
|
| 37 |
+
|
| 38 |
+
results = model(img, conf=confidence, iou=overlap)[0]
|
| 39 |
+
|
| 40 |
+
detections = []
|
| 41 |
+
for box in results.boxes:
|
| 42 |
+
detections.append({
|
| 43 |
+
"class": model.names[int(box.cls)],
|
| 44 |
+
"confidence": round(float(box.conf), 3),
|
| 45 |
+
"bbox": {
|
| 46 |
+
# Normalized coords (0-1), easy to draw on any canvas size
|
| 47 |
+
"x": round(float(box.xywhn[0][0]), 4),
|
| 48 |
+
"y": round(float(box.xywhn[0][1]), 4),
|
| 49 |
+
"w": round(float(box.xywhn[0][2]), 4),
|
| 50 |
+
"h": round(float(box.xywhn[0][3]), 4),
|
| 51 |
+
},
|
| 52 |
+
"bbox_pixels": {
|
| 53 |
+
# Absolute pixel coords in the original image
|
| 54 |
+
"x1": int(box.xyxy[0][0]),
|
| 55 |
+
"y1": int(box.xyxy[0][1]),
|
| 56 |
+
"x2": int(box.xyxy[0][2]),
|
| 57 |
+
"y2": int(box.xyxy[0][3]),
|
| 58 |
+
}
|
| 59 |
+
})
|
| 60 |
+
|
| 61 |
+
return JSONResponse({
|
| 62 |
+
"detections": detections,
|
| 63 |
+
"count": len(detections),
|
| 64 |
+
"image_shape": {
|
| 65 |
+
"width": img.shape[1],
|
| 66 |
+
"height": img.shape[0],
|
| 67 |
+
},
|
| 68 |
+
"settings": {
|
| 69 |
+
"confidence": confidence,
|
| 70 |
+
"overlap": overlap,
|
| 71 |
+
}
|
| 72 |
+
})
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
@app.get("/")
|
| 76 |
+
def root():
|
| 77 |
+
return {
|
| 78 |
+
"status": "ok",
|
| 79 |
+
"model": "floor-jaaps YOLOv8s",
|
| 80 |
+
"classes": list(model.names.values()),
|
| 81 |
+
"usage": "POST /detect with form-data: file (image), confidence (0-1), overlap (0-1)"
|
| 82 |
+
}
|
best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5b488a70f8c7b433589369e023e73e4fb10c80f0feb4970aa1b28e7493e5dcca
|
| 3 |
+
size 67128820
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
ultralytics
|
| 4 |
+
opencv-python-headless
|
| 5 |
+
python-multipart
|