from fastapi import FastAPI, File, UploadFile from fastapi.responses import JSONResponse from src.config import Config from src.inference import detector # Imports the loaded model instance app = FastAPI( title=Config.API_TITLE, description=Config.API_DESC, version=Config.API_VERSION ) @app.get("/health") def health_check(): return {"status": "operational", "model": Config.MODEL_PATH} @app.post("/predict") async def predict(file: UploadFile = File(...)): try: # 1. Read Image image_bytes = await file.read() img = detector.process_image(image_bytes) # 2. Inference raw_results = detector.run_inference(img) # 3. Formatting detections, base64_img = detector.format_results(raw_results) return JSONResponse(content={ "meta": { "count": len(detections), "model": "YOLOv8-X" }, "detections": detections, "annotated_image_base64": base64_img }) except Exception as e: return JSONResponse(content={"error": str(e)}, status_code=500)