File size: 10,064 Bytes
2a8000d
83e2352
2d7a63f
2a8000d
 
 
 
 
83e2352
755cacc
af04a83
83e2352
9790229
919cbf7
2a8000d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1214ae6
2a8000d
414709f
edbbb69
f65e838
482673f
2aad4f2
2a8000d
f65e838
 
b165b05
987667a
c141fc2
 
2a8000d
 
 
 
 
 
 
 
 
 
142bff5
2a8000d
 
 
 
 
142bff5
b165b05
142bff5
 
2a8000d
1214ae6
142bff5
2a8000d
 
 
 
 
1885541
69e2a87
 
b165b05
69e2a87
 
 
 
 
 
 
b165b05
 
1dab45f
b165b05
1885541
b165b05
 
2a8000d
b165b05
 
 
 
 
 
69e2a87
b165b05
b0d222e
 
 
 
69e2a87
b0d222e
726e57b
b165b05
69e2a87
1214ae6
 
414709f
4a7957f
 
69e2a87
1214ae6
4682a47
69e2a87
b165b05
69e2a87
 
b165b05
 
 
 
 
 
 
 
2a8000d
726e57b
 
2a8000d
755cacc
af04a83
755cacc
414709f
755cacc
83e2352
755cacc
83e2352
 
755cacc
83e2352
 
 
 
 
755cacc
83e2352
 
 
 
 
 
 
755cacc
414709f
755cacc
2a8000d
2d7a63f
2a8000d
2d7a63f
2a8000d
755cacc
2a8000d
2d7a63f
 
2a8000d
 
2d7a63f
 
8db31c1
 
 
 
 
 
 
 
 
 
d496a3c
 
2d7a63f
414709f
d496a3c
414709f
2d7a63f
 
83e2352
 
 
 
 
414709f
 
2d7a63f
83e2352
 
 
 
 
 
 
 
 
414709f
2d7a63f
9790229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a8000d
2d7a63f
4682a47
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import gradio as gr
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import JSONResponse
import torch
import torch.nn as nn
import timm
import cv2
import numpy as np
from PIL import Image, UnidentifiedImageError
from io import BytesIO
import base64
import traceback
from starlette.exceptions import HTTPException as StarletteHTTPException
from rembg import remove as bgrem_remove

# ===============================
# SIMPLE DPT MODEL (DEPTH ESTIMATION)
# ===============================
class SimpleDPT(nn.Module):
    def __init__(self, backbone_name='vit_base_patch16_384'):
        super(SimpleDPT, self).__init__()
        self.backbone = timm.create_model(backbone_name, pretrained=True, features_only=True)
        feature_info = self.backbone.feature_info
        channels = [f['num_chs'] for f in feature_info]

        self.decoder = nn.Sequential(
            nn.Conv2d(channels[-1], 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 128, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(128, 1, kernel_size=1)
        )

    def forward(self, x, target_size):
        features = self.backbone(x)
        x = features[-1]
        depth = self.decoder(x)
        depth = nn.functional.interpolate(depth, size=target_size, mode='bilinear', align_corners=False)
        return depth

# ===============================
# DEPTH → NORMAL MAP
# ===============================
def depth_to_normal(depth):
    dy, dx = np.gradient(depth)
    normal = np.dstack((-dx, -dy, np.ones_like(depth)))
    n = np.linalg.norm(normal, axis=2, keepdims=True)
    normal /= (n + 1e-8)
    normal = (normal + 1) / 2
    return normal

# ===============================
# CORE PROCESSING FUNCTION
# ===============================
def _process_saree_core(base_image: Image.Image, pattern_image: Image.Image, k: int = 5):
    # Auto-set k for BASE-BOTTOM images
    filename = getattr(base_image, "filename", "") or ""    
    if any(name in filename for name in ["BASE-BOTTOM-2.png"]):
        k = 12

    print(f"[DEBUG] base_image filename: {filename}")

    # Prepare tensor
    img_pil = base_image.convert("RGB")
    img_np = np.array(img_pil)

    img_resized = img_pil.resize((384, 384))
    img_tensor = torch.from_numpy(np.array(img_resized)).permute(2, 0, 1).unsqueeze(0).float() / 255.0
    mean = torch.as_tensor([0.5, 0.5, 0.5], device=img_tensor.device).view(1, 3, 1, 1)
    std = torch.as_tensor([0.5, 0.5, 0.5], device=img_tensor.device).view(1, 3, 1, 1)
    img_tensor = (img_tensor - mean) / std

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    model = SimpleDPT(backbone_name='vit_base_patch16_384').to(device)
    model.eval()

    # Depth inference
    with torch.no_grad():
        target_size = img_pil.size[::-1]
        depth_map = model(img_tensor.to(device), target_size=target_size)
        depth_map = depth_map.squeeze().cpu().numpy()

    # Normalize depth
    depth_vis = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())

    # Normal map
    normal_map = depth_to_normal(depth_vis)

    # Shading map (CLAHE)
    img_lab = cv2.cvtColor(img_np, cv2.COLOR_RGB2LAB)
    l_channel, _, _ = cv2.split(img_lab)
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    l_clahe = clahe.apply(l_channel)
    shading_map = l_clahe / 255.0

    # Tile pattern
    pattern_np = np.array(pattern_image.convert("RGB"))
    target_h, target_w = img_np.shape[:2]
    pattern_h, pattern_w = pattern_np.shape[:2]
    pattern_tiled = np.zeros((target_h, target_w, 3), dtype=np.uint8)
    for y in range(0, target_h, pattern_h):
        for x in range(0, target_w, pattern_w):
            end_y = min(y + pattern_h, target_h)
            end_x = min(x + pattern_w, target_w)
            pattern_tiled[y:end_y, x:end_x] = pattern_np[0:(end_y - y), 0:(end_x - x)]

    # Blend pattern
    normal_map_loaded = normal_map.astype(np.float32)
    shading_map_loaded = np.stack([shading_map] * 3, axis=-1)

    alpha = 0.7
    blended_shading = alpha * shading_map_loaded + (1 - alpha)

    pattern_folded = pattern_tiled.astype(np.float32) / 255.0 * blended_shading
    normal_boost = 0.5 + 0.5 * normal_map_loaded[..., 2:3]
    pattern_folded *= normal_boost
    pattern_folded = np.clip(pattern_folded, 0, 1)

    # ==========================================================
    # Background removal with post-processing (no duplicate blur)
    # ==========================================================
    buf = BytesIO()
    base_image.save(buf, format="PNG")
    base_bytes = buf.getvalue()

    # Get RGBA from bgrem
    result_no_bg = bgrem_remove(base_bytes)
    mask_img = Image.open(BytesIO(result_no_bg)).convert("RGBA")

    # Extract alpha and clean edges
    mask_alpha = np.array(mask_img)[:, :, 3].astype(np.float32) / 255.0

    # 1. Slightly stronger shrink (balanced)    
    kernel = np.ones((k, k), np.uint8)   # slightly larger kernel
    mask_binary = (mask_alpha > k/100).astype(np.uint8) * 255  # slightly stricter threshold
    mask_eroded = cv2.erode(mask_binary, kernel, iterations=3)  # balanced erosion


    # 2. Feather edges (blur)
    mask_blurred = cv2.GaussianBlur(mask_eroded, (15, 15), sigmaX=3, sigmaY=3)

    # 3. Normalize
    mask_blurred = mask_blurred.astype(np.float32) / 255.0

    # Final RGBA
    mask_stack = np.stack([mask_blurred] * 3, axis=-1)
    pattern_final = pattern_folded * mask_stack
    pattern_rgb = (pattern_final * 255).astype(np.uint8)
    alpha_channel = (mask_blurred * 255).astype(np.uint8)
    pattern_rgba = np.dstack((pattern_rgb, alpha_channel))

    # return Image.fromarray(pattern_rgba, mode="RGBA")
    return Image.fromarray(pattern_rgba)

# ===============================
# WRAPPER: ACCEPT BYTES OR BASE64
# ===============================
def process_saree(data, k: int = 5):
    if not isinstance(data, (list, tuple)) or len(data) != 2:
        raise HTTPException(status_code=422, detail="Expected an array with two elements: [base_blob, pattern_blob]")

    try:
        base_blob, pattern_blob = data

        # Convert base64 to bytes if needed
        if isinstance(base_blob, str):
            base_blob = base64.b64decode(base_blob.split(",")[-1])
        if isinstance(pattern_blob, str):
            pattern_blob = base64.b64decode(pattern_blob.split(",")[-1])

        base_image = Image.open(BytesIO(base_blob)).convert("RGBA")
        pattern_image = Image.open(BytesIO(pattern_blob)).convert("RGBA")

    except (base64.binascii.Error, UnidentifiedImageError) as e:
        raise HTTPException(status_code=422, detail=f"Invalid image data: {str(e)}")
    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Error reading input images: {str(e)}")

    return _process_saree_core(base_image, pattern_image, k=k)

# ===============================
# GRADIO + FASTAPI APP
# ===============================
gradio_iface = gr.Interface(
    fn=process_saree,
    inputs=gr.Dataframe(headers=["Base Blob", "Pattern Blob"], type="array"),
    outputs=gr.Image(type="pil", label="Final Saree Output"),
    title="Saree Depth + Pattern Draping",
    description="Blob or base64 API compatible"
)

app = FastAPI()

# Root endpoint
@app.get("/")
async def root():
    return JSONResponse(
        content={
            "message": "Saree Depth + Pattern Draping API",
            "endpoints": ["/predict-saree", "/api/predict/", "/gradio"]
        }
    )

# Mount Gradio at /gradio
app = gr.mount_gradio_app(app, gradio_iface, path="/gradio")

# ===============================
# Custom API endpoint
# ===============================
@app.post("/predict-saree")
async def predict_saree(request: Request):
    try:
        body = await request.json()
        if "data" not in body:
            raise HTTPException(status_code=422, detail="Missing 'data' field in request body")

        k = body.get("k", 5)  # Default to 5 if not provided
        result_img = process_saree(body["data"], k=int(k))

        buf = BytesIO()
        result_img.save(buf, format="PNG")
        base64_img = base64.b64encode(buf.getvalue()).decode("utf-8")
        return JSONResponse(content={"image_base64": base64_img})

    except HTTPException as e:
        return JSONResponse(status_code=e.status_code, content={"error": "Input Error", "details": e.detail})
    except Exception as e:
        tb = traceback.format_exc()
        return JSONResponse(status_code=500, content={"error": "Processing Error", "details": str(e), "trace": tb})

# Alias for backward compatibility
@app.post("/api/predict/")
async def alias_predict(request: Request):
    return await predict_saree(request)

# ===============================
# GLOBAL ERROR HANDLERS
# ===============================
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
    if exc.status_code == 404:
        return JSONResponse(
            status_code=404,
            content={
                "error": "Endpoint Not Found",
                "details": f"The requested URL {request.url.path} does not exist. "
                           "Valid endpoints: /predict-saree or /api/predict/."
            }
        )
    elif exc.status_code == 405:
        return JSONResponse(
            status_code=405,
            content={
                "error": "Method Not Allowed",
                "details": f"Method {request.method} not allowed on {request.url.path}"
            }
        )
    return JSONResponse(
        status_code=exc.status_code,
        content={"error": exc.detail or "HTTP Error"}
    )

@app.exception_handler(Exception)
async def unhandled_exception_handler(request: Request, exc: Exception):
    return JSONResponse(
        status_code=500,
        content={
            "error": "Internal Server Error",
            "details": str(exc)
        }
    )

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)