Spaces:
Sleeping
Sleeping
File size: 2,031 Bytes
336490c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import JSONResponse
import cv2
import numpy as np
import io
from PIL import Image
from ultralytics import YOLO
app = FastAPI(title="YOLOv11 Detection API")
# Load the YOLO model
try:
model = YOLO('yolo11n.pt')
except Exception as e:
print(f"Error loading model: {e}")
model = None
@app.get("/")
async def root():
return {"message": "YOLOv11 Detection API is running. Go to /docs for API documentation."}
@app.get("/health")
async def health():
if model is not None:
return {"status": "healthy", "model": "yolo11n.pt"}
else:
raise HTTPException(status_code=503, detail="Model not loaded")
@app.post("/predict")
async def predict(file: UploadFile = File(...)):
if model is None:
raise HTTPException(status_code=503, detail="Model not loaded")
# Read the uploaded image
try:
contents = await file.read()
image = Image.open(io.BytesIO(contents)).convert("RGB")
img_array = np.array(image)
# Convert RGB to BGR for OpenCV/YOLO if needed
img_bgr = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
except Exception as e:
raise HTTPException(status_code=400, detail=f"Invalid image: {e}")
# Run inference
results = model(img_bgr, verbose=False)
detections = []
for box in results[0].boxes:
class_id = int(box.cls[0])
class_name = model.names[class_id]
confidence = float(box.conf[0])
x1, y1, x2, y2 = box.xyxy[0].tolist()
detections.append({
"class": class_name,
"confidence": confidence,
"bbox": [x1, y1, x2, y2]
})
return JSONResponse(content={
"filename": file.filename,
"detections": detections,
"count": len(detections)
})
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
|