File size: 1,217 Bytes
826ab94
 
 
 
 
 
 
 
 
 
151f2d9
826ab94
 
 
 
 
396fc06
826ab94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse
from PIL import Image
import io
from huggingface_hub import hf_hub_download
from ultralytics import YOLO

app = FastAPI()

# Download YOLOv8 model from Hugging Face
YOLO_MODEL_PATH = hf_hub_download(repo_id="sharktide/RDiCC", filename="runs/detect/train/weights/best.pt")

# Load YOLOv8 model
yolo_model = YOLO(YOLO_MODEL_PATH)

@app.get("/working")
def working():
    return JSONResponse(content={"status": "working"})

@app.post("/detect")
async def detect(file: UploadFile = File(...)):
    # Read and convert image
    image = Image.open(io.BytesIO(await file.read())).convert("RGB")

    # Run YOLO inference
    results = yolo_model(image)

    # Parse results
    detections = []
    for r in results:
        for box in r.boxes:
            cls_id = int(box.cls)
            label = yolo_model.names[cls_id]
            conf = float(box.conf)
            xyxy = box.xyxy[0].tolist()
            detections.append({
                "label": label,
                "confidence": round(conf, 3),
                "box": [round(x, 2) for x in xyxy]
            })

    return JSONResponse(content={"detections": detections})