from fastapi import APIRouter, HTTPException, UploadFile, File, Query from pydantic import BaseModel import io from PIL import Image from src.services.reconstruction import ReconstructionService router = APIRouter(prefix="/reconstruct", tags=["reconstruction"]) service = ReconstructionService() class ReconstructionResponse(BaseModel): obj_path: str mtl_path: str texture_path: str @router.post("/", response_model=ReconstructionResponse) async def reconstruct_model( file: UploadFile = File(..., description="Image file for 3D reconstruction"), remove_background: bool = Query(True, description="Remove background from the image"), foreground_ratio: float = Query(0.5, ge=0.0, le=1.0, description="Foreground scaling ratio"), mc_resolution: int = Query(256, ge=1, description="Marching cubes resolution") ): """ Upload an image and reconstruct a 3D model. Returns paths to the OBJ, MTL and texture files. """ try: content = await file.read() image = Image.open(io.BytesIO(content)) # preprocess and reconstruct image = service.preprocess_image(image, remove_background, foreground_ratio) obj_path, mtl_path, texture_path = service.generate_3d_model(image, mc_resolution) return ReconstructionResponse( obj_path=obj_path, mtl_path=mtl_path, texture_path=texture_path ) except Exception as e: raise HTTPException(status_code=500, detail=str(e))