File size: 912 Bytes
96285c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, UploadFile, File
from PIL import Image
from ultralytics import YOLO
import io

app = FastAPI()

# Loading model
model = YOLO("best.pt")
print("Model classes :", model.names)

# Health check
@app.get("/")
def home():
    return {"status": "ok", "classes": model.names}

@app.post("/predict")
def predict(file: UploadFile = File(...)):
    image = Image.open(io.BytesIO(file.file.read())).convert("RGB")

    results = model.predict(image)[0]

    detections = []
    # For each bounding box, 3 pieces of information are extracted
    for box in results.boxes:
        detections.append({
            # converte 0 and 1 into fire or smoke
            "classe": model.names[int(box.cls)],
            # confidence score
            "confidence": float(box.conf),
            # bbox coordinates
            "bbox": box.xyxy[0].tolist(),
        })

    return {"detections": detections}