File size: 1,483 Bytes
c411a0f | 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 41 42 43 44 45 46 47 48 49 50 51 52 53 | from pathlib import Path
from fastapi import HTTPException, UploadFile
from src.constants import ALLOWED_EXTENSIONS
from core import processor
def validate_uploaded_image(file: UploadFile) -> Path:
if not (file.content_type and file.content_type.startswith("image/")):
raise HTTPException(
status_code=400,
detail=f"File {file.filename} is not a valid image."
)
filename = Path(file.filename or "upload")
if filename.suffix.lower() not in ALLOWED_EXTENSIONS:
raise HTTPException(
status_code=400,
detail=f"Invalid file extension for {filename.name}."
)
return filename
def process_model_replacement(
model_name: str,
fg_path: Path,
bg_path: Path,
output_dir: Path,
car_size_decimal: float,
smart_placement: bool,
):
output_name = f"{fg_path.stem}_{model_name}_replaced.png"
output_path = output_dir / output_name
result_img = processor.replace_background(
foreground_input=str(fg_path),
background_input=str(bg_path),
model_name=model_name,
normalize=True,
target_car_ratio=car_size_decimal,
smart_placement=smart_placement,
)
result_img.save(output_path, format="PNG")
return {
"model": model_name,
"status": "success",
"output_filename": output_path.name,
"output_path": str(output_path),
"output_url": f"/output/{output_path.name}",
} |