Spaces:
No application file
No application file
Upload 2 files
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
model = YOLO("best.pt") # Ensure this file is in your project folder
|
| 9 |
+
|
| 10 |
+
@app.post("/detect")
|
| 11 |
+
async def detect(file: UploadFile = File(...)):
|
| 12 |
+
contents = await file.read()
|
| 13 |
+
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 14 |
+
results = model(image)
|
| 15 |
+
|
| 16 |
+
detections = []
|
| 17 |
+
for r in results:
|
| 18 |
+
for box in r.boxes:
|
| 19 |
+
cls_id = int(box.cls)
|
| 20 |
+
conf = float(box.conf)
|
| 21 |
+
label = model.names[cls_id]
|
| 22 |
+
x1, y1, x2, y2 = box.xyxy[0]
|
| 23 |
+
detections.append({
|
| 24 |
+
"label": label,
|
| 25 |
+
"confidence": round(conf, 2),
|
| 26 |
+
"box": [
|
| 27 |
+
round(float(x1), 2),
|
| 28 |
+
round(float(y1), 2),
|
| 29 |
+
round(float(x2 - x1), 2),
|
| 30 |
+
round(float(y2 - y1), 2)
|
| 31 |
+
]
|
| 32 |
+
})
|
| 33 |
+
|
| 34 |
+
return JSONResponse(content={"detections": detections})
|
best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:05b9debfd24b0e21dd95ed1e92237bdea28e97ecb841818a32628beba27405a0
|
| 3 |
+
size 6306147
|