"""FastAPI routes for CALY food image analysis.""" from __future__ import annotations import logging from fastapi import APIRouter, File, HTTPException, UploadFile from api.schemas import FoodAnalysisResponse from main_pipeline import analyze_food_image from utils.image_utils import decode_image_bytes LOGGER = logging.getLogger(__name__) router = APIRouter(tags=["camera"]) @router.post("/analyze-food", response_model=FoodAnalysisResponse) async def analyze_food(image: UploadFile = File(...)) -> FoodAnalysisResponse: """Accept a meal image and return food names, grams, and calibration method.""" if image.content_type and not image.content_type.startswith("image/"): raise HTTPException(status_code=415, detail="Uploaded file must be an image") try: content = await image.read() image_bgr = decode_image_bytes(content) result = analyze_food_image(image_bgr) except ValueError as exc: raise HTTPException(status_code=400, detail=str(exc)) from exc except Exception as exc: LOGGER.exception("Unhandled API analysis error") raise HTTPException(status_code=500, detail="Food analysis failed") from exc return FoodAnalysisResponse(**result)