Spaces:
Sleeping
Sleeping
Root endpoint / returning API info
Browse files
app.py
CHANGED
|
@@ -110,7 +110,7 @@ def _process_saree_core(base_image: Image.Image, pattern_image: Image.Image):
|
|
| 110 |
pattern_folded = np.clip(pattern_folded, 0, 1)
|
| 111 |
|
| 112 |
# ==========================================================
|
| 113 |
-
#
|
| 114 |
# ==========================================================
|
| 115 |
buf = BytesIO()
|
| 116 |
base_image.save(buf, format="PNG")
|
|
@@ -119,26 +119,19 @@ def _process_saree_core(base_image: Image.Image, pattern_image: Image.Image):
|
|
| 119 |
# Get RGBA from bgrem
|
| 120 |
result_no_bg = bgrem_remove(base_bytes)
|
| 121 |
mask_img = Image.open(BytesIO(result_no_bg)).convert("RGBA")
|
| 122 |
-
|
| 123 |
# Extract alpha and clean edges
|
| 124 |
mask_alpha = np.array(mask_img)[:, :, 3].astype(np.float32) / 255.0
|
| 125 |
-
|
| 126 |
-
#
|
| 127 |
-
# 1. Slightly shrink the mask to remove halos
|
| 128 |
kernel = np.ones((3, 3), np.uint8)
|
| 129 |
mask_binary = (mask_alpha > 0.05).astype(np.uint8) * 255
|
| 130 |
mask_eroded = cv2.erode(mask_binary, kernel, iterations=1)
|
| 131 |
-
|
| 132 |
-
# 2. Feather edges to smooth transition
|
| 133 |
-
mask_blurred = cv2.GaussianBlur(mask_eroded, (15, 15), sigmaX=3, sigmaY=3)
|
| 134 |
-
|
| 135 |
-
# 3. Re-normalize
|
| 136 |
-
mask_blurred = mask_blurred.astype(np.float32) / 255.0
|
| 137 |
|
|
|
|
|
|
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
mask_blurred = cv2.GaussianBlur((mask_alpha * 255).astype(np.uint8), (15, 15), sigmaX=5, sigmaY=5)
|
| 141 |
-
mask_blurred[mask_blurred < 25] = 0
|
| 142 |
mask_blurred = mask_blurred.astype(np.float32) / 255.0
|
| 143 |
|
| 144 |
# Final RGBA
|
|
@@ -189,6 +182,16 @@ gradio_iface = gr.Interface(
|
|
| 189 |
|
| 190 |
app = FastAPI()
|
| 191 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
# Mount Gradio at /gradio
|
| 193 |
app = gr.mount_gradio_app(app, gradio_iface, path="/gradio")
|
| 194 |
|
|
|
|
| 110 |
pattern_folded = np.clip(pattern_folded, 0, 1)
|
| 111 |
|
| 112 |
# ==========================================================
|
| 113 |
+
# Background removal with post-processing (no duplicate blur)
|
| 114 |
# ==========================================================
|
| 115 |
buf = BytesIO()
|
| 116 |
base_image.save(buf, format="PNG")
|
|
|
|
| 119 |
# Get RGBA from bgrem
|
| 120 |
result_no_bg = bgrem_remove(base_bytes)
|
| 121 |
mask_img = Image.open(BytesIO(result_no_bg)).convert("RGBA")
|
| 122 |
+
|
| 123 |
# Extract alpha and clean edges
|
| 124 |
mask_alpha = np.array(mask_img)[:, :, 3].astype(np.float32) / 255.0
|
| 125 |
+
|
| 126 |
+
# 1. Slightly shrink mask (erode)
|
|
|
|
| 127 |
kernel = np.ones((3, 3), np.uint8)
|
| 128 |
mask_binary = (mask_alpha > 0.05).astype(np.uint8) * 255
|
| 129 |
mask_eroded = cv2.erode(mask_binary, kernel, iterations=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
# 2. Feather edges (blur)
|
| 132 |
+
mask_blurred = cv2.GaussianBlur(mask_eroded, (15, 15), sigmaX=3, sigmaY=3)
|
| 133 |
|
| 134 |
+
# 3. Normalize
|
|
|
|
|
|
|
| 135 |
mask_blurred = mask_blurred.astype(np.float32) / 255.0
|
| 136 |
|
| 137 |
# Final RGBA
|
|
|
|
| 182 |
|
| 183 |
app = FastAPI()
|
| 184 |
|
| 185 |
+
# Root endpoint
|
| 186 |
+
@app.get("/")
|
| 187 |
+
async def root():
|
| 188 |
+
return JSONResponse(
|
| 189 |
+
content={
|
| 190 |
+
"message": "Saree Depth + Pattern Draping API",
|
| 191 |
+
"endpoints": ["/predict-saree", "/api/predict/", "/gradio"]
|
| 192 |
+
}
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
# Mount Gradio at /gradio
|
| 196 |
app = gr.mount_gradio_app(app, gradio_iface, path="/gradio")
|
| 197 |
|