from fastapi import FastAPI, File, UploadFile
from fastapi.responses import HTMLResponse
from ultralytics import YOLO
import uvicorn
import io
from PIL import Image
import numpy as np
app = FastAPI(title="Fire Detection API", description="API for detecting fire and smoke using YOLOv26")
# Load model
model = YOLO("best.pt")
@app.get("/", response_class=HTMLResponse)
def read_root():
return """
Fire Detection API
"""
@app.post("/predict")
async def predict(file: UploadFile = File(...)):
# Read image
contents = await file.read()
image = Image.open(io.BytesIO(contents)).convert("RGB")
# Run inference
results = model.predict(image, conf=0.25)
detections = []
for result in results:
for box in result.boxes:
detection = {
"class": model.names[int(box.cls[0])],
"confidence": float(box.conf[0]),
"bbox": [float(x) for x in box.xyxy[0]] # [x1, y1, x2, y2]
}
detections.append(detection)
return {"detections": detections}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)