kamyvision-api / app /routers /image.py
oyabun-dev's picture
deploy: 2026-04-02T00:05:48Z
55bcd2b
import io
from fastapi import APIRouter, File, HTTPException, UploadFile
from PIL import Image
from app.core.config import ALLOWED_IMAGE_MIMETYPES, MAX_FILE_SIZE
from app.pipelines import image as image_pipeline
try:
from pillow_heif import register_heif_opener
register_heif_opener()
except ImportError:
pass
Image.MAX_IMAGE_PIXELS = 100_000_000
router = APIRouter()
def _validate_and_read(file: UploadFile, contents: bytes) -> Image.Image:
content_type = getattr(file, "content_type", "")
if content_type not in ALLOWED_IMAGE_MIMETYPES:
raise HTTPException(
status_code=400,
detail=f"Format non supporté: {content_type}. Formats acceptés: JPEG, PNG, WEBP, HEIC.",
)
if len(contents) > MAX_FILE_SIZE:
raise HTTPException(status_code=413, detail="Fichier trop volumineux (max 20 Mo).")
try:
return Image.open(io.BytesIO(contents)).convert("RGB")
except Exception:
raise HTTPException(status_code=400, detail="Fichier image corrompu ou illisible.")
@router.post("/analyze/image")
async def analyze_image(file: UploadFile = File(...)):
contents = await file.read()
img = _validate_and_read(file, contents)
result = image_pipeline.run(img, contents)
return {"status": "success", **result}
@router.post("/analyze/image/fast")
async def analyze_image_fast(file: UploadFile = File(...)):
contents = await file.read()
img = _validate_and_read(file, contents)
result = image_pipeline.run_fast(img, contents)
return {"status": "success", **result}