Spaces:
Build error
Build error
File size: 1,501 Bytes
abf7f95 e6fd99c abf7f95 e6fd99c abf7f95 e6fd99c abf7f95 e6fd99c abf7f95 e6fd99c abf7f95 e6fd99c abf7f95 e6fd99c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
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))
|