File size: 4,534 Bytes
5a36fd8
 
 
 
c411a0f
 
112243b
5a36fd8
 
 
112243b
c411a0f
e859ee2
c411a0f
 
 
5a36fd8
e859ee2
 
 
 
c411a0f
 
 
6dcdb1c
c411a0f
 
112243b
e859ee2
c411a0f
 
 
 
5a36fd8
 
c411a0f
 
 
 
5a36fd8
c411a0f
 
5a36fd8
e859ee2
112243b
6dcdb1c
3a44146
c411a0f
5a36fd8
112243b
6dcdb1c
112243b
 
6dcdb1c
112243b
5a36fd8
 
 
e859ee2
 
 
 
 
c411a0f
5a36fd8
6dcdb1c
 
5a36fd8
e859ee2
 
c411a0f
e859ee2
5a36fd8
e859ee2
 
 
5a36fd8
 
 
e859ee2
 
5a36fd8
e859ee2
 
 
c411a0f
5a36fd8
c411a0f
5a36fd8
e859ee2
5a36fd8
c411a0f
 
 
6dcdb1c
5a36fd8
 
e859ee2
 
5a36fd8
6dcdb1c
 
5a36fd8
 
e859ee2
5a36fd8
 
 
e859ee2
5a36fd8
 
 
c411a0f
5a36fd8
6dcdb1c
5a36fd8
 
 
 
6dcdb1c
 
c411a0f
5a36fd8
 
 
e859ee2
5a36fd8
6dcdb1c
e859ee2
 
 
6dcdb1c
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from fastapi import FastAPI, File, HTTPException, UploadFile, Form
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from PIL import Image
import os
import shutil
import io
import time
import sys
from pathlib import Path
import requests as http_requests

# --- Path Setup ---
root_dir = Path(__file__).resolve().parent.parent.parent
sys.path.append(str(root_dir))

# Import your core engine
try:
    from core import processor
except ImportError:
    print("Error: 'core.processor' nahi mila. Check karein ke 'core' folder sahi jagah hai.")

app = FastAPI()

# --- CORS Fix ---
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

def get_dirs():
    base_dir = Path("images")
    input_dir = base_dir / "input"
    output_dir = base_dir / "output"
    input_dir.mkdir(parents=True, exist_ok=True)
    output_dir.mkdir(parents=True, exist_ok=True)
    return input_dir, output_dir

@app.get("/")
def health_check():
    return {"status": "active", "service": "CarVite AI Backend"}

# --- Main Processing Endpoint (For Background Removal or URL Replacement) ---
@app.post("/process")
@app.post("/upload-image")
async def process_car_image(
    image: UploadFile = File(...),
    action: str = Form("remove"),
    bg_color: str = Form(None),
    bg_url: str = Form(None),
    model_name: str = Form("isnet-general-use") # Added model_name support
):
    input_dir, output_dir = get_dirs()
    file_id = int(time.time() * 1000)
    
    ext = Path(image.filename).suffix or ".jpg"
    input_path = input_dir / f"in_{file_id}{ext}"
    
    with input_path.open("wb") as buffer:
        shutil.copyfileobj(image.file, buffer)

    try:
        # 1. Background Remove
        processed_img, _ = processor.process_image(str(input_path), model_name=model_name)
        processed_img = processed_img.convert("RGBA")
        
        output_path = output_dir / f"out_{file_id}.png"

        if action == "remove" and not bg_url and not bg_color:
            processed_img.save(output_path, "PNG")
        else:
            final_img = processed_img
            if bg_url:
                bg_res = http_requests.get(bg_url, timeout=10)
                bg_img = Image.open(io.BytesIO(bg_res.content)).convert("RGBA")
                bg_img = bg_img.resize(processed_img.size, Image.LANCZOS)
                final_img = Image.alpha_composite(bg_img, processed_img)
            elif bg_color:
                bg_img = Image.new("RGBA", processed_img.size, bg_color)
                final_img = Image.alpha_composite(bg_img, processed_img)
            
            final_img.save(output_path, "PNG")

        return FileResponse(str(output_path), media_type="image/png")

    except Exception as e:
        print(f"AI Error: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Processing failed: {str(e)}")
    finally:
        image.file.close()

# --- Custom Background Upload Endpoint (Fixed for missing model_name) ---
@app.post("/replace-background")
async def replace_custom_bg(
    image: UploadFile = File(..., description="Car Image"),
    background: UploadFile = File(..., description="Custom BG Image"),
    car_size: float = Form(0.65),
    smart_placement: bool = Form(True),
    model_name: str = Form("isnet-general-use") # <--- Yahan ye zaroori hai!
):
    input_dir, output_dir = get_dirs()
    file_id = int(time.time() * 1000)
    
    fg_path = input_dir / f"fg_{file_id}_{image.filename}"
    bg_path = input_dir / f"bg_{file_id}_{background.filename}"
    output_path = output_dir / f"final_{file_id}.png"
    
    with fg_path.open("wb") as f: shutil.copyfileobj(image.file, f)
    with bg_path.open("wb") as b: shutil.copyfileobj(background.file, b)

    try:
        # Added model_name argument to fix the "missing positional argument" error
        result_img = processor.replace_background(
            foreground_input=str(fg_path),
            background_input=str(bg_path),
            target_car_ratio=car_size,
            smart_placement=smart_placement,
            model_name=model_name # <--- Ye argument pass hona chahiye
        )
        
        result_img.save(output_path, "PNG")
        return FileResponse(str(output_path), media_type="image/png")

    except Exception as e:
        print(f"Replacement Error: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        image.file.close()
        background.file.close()