Spaces:
Sleeping
Sleeping
ujoy007 commited on
Commit Β·
eece6a4
1
Parent(s): 60e7e74
all problems are now fixed. \detect should return predictions
Browse files
README.md
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
-
title: YOLO FastAPI Deploy
|
| 3 |
-
emoji: π€
|
| 4 |
-
colorFrom: blue
|
| 5 |
-
colorTo: green
|
| 6 |
-
sdk: docker
|
| 7 |
-
app_file: main.py
|
| 8 |
-
pinned: false
|
| 9 |
-
---
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
Use `/docs` to test endpoints, or send requests from Postman.
|
|
|
|
| 1 |
+
# YOLO FastAPI Deploy π
|
| 2 |
+
|
| 3 |
+
This Space deploys a YOLO object detection model with **FastAPI** on Hugging Face Spaces.
|
| 4 |
+
|
| 5 |
+
## π Live URL
|
| 6 |
+
|
| 7 |
+
π [https://ujoy-yolo-fast-api-deploy.hf.space](https://ujoy-yolo-fast-api-deploy.hf.space)
|
| 8 |
+
|
| 9 |
+
- Root health: `/`
|
| 10 |
+
- API docs (Swagger UI): `/docs`
|
| 11 |
+
- Detect endpoint: `/detect`
|
| 12 |
+
|
| 13 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
## π Usage
|
| 16 |
+
|
| 17 |
+
### 1. Upload an image for detection
|
| 18 |
|
| 19 |
+
**Endpoint**:
|
|
|
main.py
CHANGED
|
@@ -1,32 +1,33 @@
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File
|
| 2 |
-
from fastapi.responses import JSONResponse
|
| 3 |
from ultralytics import YOLO
|
| 4 |
import shutil
|
| 5 |
import uuid
|
| 6 |
import os
|
| 7 |
-
import base64
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
# Load YOLO model
|
| 12 |
model = YOLO("yolo11s.pt")
|
| 13 |
|
| 14 |
-
#
|
| 15 |
UPLOAD_DIR = "/tmp/uploads"
|
| 16 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 17 |
|
| 18 |
|
| 19 |
@app.get("/")
|
| 20 |
def home():
|
| 21 |
-
return {"
|
| 22 |
|
| 23 |
|
| 24 |
@app.post("/detect")
|
| 25 |
-
async def detect(file: UploadFile = File(...)
|
| 26 |
"""
|
| 27 |
Run YOLO detection.
|
| 28 |
-
|
| 29 |
-
-
|
|
|
|
|
|
|
| 30 |
"""
|
| 31 |
# Save uploaded file
|
| 32 |
file_id = str(uuid.uuid4())
|
|
@@ -37,19 +38,13 @@ async def detect(file: UploadFile = File(...), return_type: str = "json"):
|
|
| 37 |
# Run YOLO detection
|
| 38 |
results = model(file_path)
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
results[0].
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
else:
|
| 48 |
-
# Encode image as base64 string
|
| 49 |
-
with open(result_path, "rb") as img_file:
|
| 50 |
-
encoded = base64.b64encode(img_file.read()).decode("utf-8")
|
| 51 |
-
|
| 52 |
-
return JSONResponse(content={
|
| 53 |
-
"message": "Detection complete",
|
| 54 |
-
"image_base64": encoded
|
| 55 |
})
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
from ultralytics import YOLO
|
| 4 |
import shutil
|
| 5 |
import uuid
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
# Load YOLO model
|
| 11 |
model = YOLO("yolo11s.pt")
|
| 12 |
|
| 13 |
+
# Writable directory on Hugging Face
|
| 14 |
UPLOAD_DIR = "/tmp/uploads"
|
| 15 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 16 |
|
| 17 |
|
| 18 |
@app.get("/")
|
| 19 |
def home():
|
| 20 |
+
return {"status": "running", "docs": "/docs"}
|
| 21 |
|
| 22 |
|
| 23 |
@app.post("/detect")
|
| 24 |
+
async def detect(file: UploadFile = File(...)):
|
| 25 |
"""
|
| 26 |
Run YOLO detection.
|
| 27 |
+
Returns:
|
| 28 |
+
- bounding boxes
|
| 29 |
+
- class labels
|
| 30 |
+
- confidence scores
|
| 31 |
"""
|
| 32 |
# Save uploaded file
|
| 33 |
file_id = str(uuid.uuid4())
|
|
|
|
| 38 |
# Run YOLO detection
|
| 39 |
results = model(file_path)
|
| 40 |
|
| 41 |
+
# Extract predictions
|
| 42 |
+
predictions = []
|
| 43 |
+
for box in results[0].boxes:
|
| 44 |
+
predictions.append({
|
| 45 |
+
"class": results[0].names[int(box.cls)],
|
| 46 |
+
"confidence": float(box.conf),
|
| 47 |
+
"bbox": [float(x) for x in box.xyxy[0].tolist()] # [x1, y1, x2, y2]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
})
|
| 49 |
+
|
| 50 |
+
return JSONResponse(content={"predictions": predictions})
|