Spaces:
Sleeping
Sleeping
accepting image blob inputs
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
import torch.nn as nn
|
|
@@ -6,7 +5,7 @@ import timm
|
|
| 6 |
import cv2
|
| 7 |
import numpy as np
|
| 8 |
from PIL import Image
|
| 9 |
-
|
| 10 |
import os
|
| 11 |
|
| 12 |
# ===============================
|
|
@@ -46,9 +45,9 @@ def depth_to_normal(depth):
|
|
| 46 |
return normal
|
| 47 |
|
| 48 |
# ===============================
|
| 49 |
-
#
|
| 50 |
# ===============================
|
| 51 |
-
def
|
| 52 |
# Convert base to numpy
|
| 53 |
img_pil = base_image.convert("RGB")
|
| 54 |
img_np = np.array(img_pil)
|
|
@@ -136,16 +135,39 @@ def process_saree(base_image: Image.Image, pattern_image: Image.Image):
|
|
| 136 |
|
| 137 |
return Image.fromarray(pattern_rgba, mode="RGBA")
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
# ===============================
|
| 140 |
# GRADIO INTERFACE
|
| 141 |
# ===============================
|
| 142 |
iface = gr.Interface(
|
| 143 |
fn=process_saree,
|
| 144 |
-
inputs=
|
| 145 |
-
gr.Image(type="pil", label="Pattern Image")],
|
| 146 |
outputs=gr.Image(type="pil", label="Final Saree Output"),
|
| 147 |
-
title="Saree Depth + Pattern Draping",
|
| 148 |
-
description="
|
| 149 |
)
|
| 150 |
|
| 151 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
|
|
|
| 5 |
import cv2
|
| 6 |
import numpy as np
|
| 7 |
from PIL import Image
|
| 8 |
+
from io import BytesIO
|
| 9 |
import os
|
| 10 |
|
| 11 |
# ===============================
|
|
|
|
| 45 |
return normal
|
| 46 |
|
| 47 |
# ===============================
|
| 48 |
+
# CORE PROCESSING FUNCTION
|
| 49 |
# ===============================
|
| 50 |
+
def _process_saree_core(base_image: Image.Image, pattern_image: Image.Image):
|
| 51 |
# Convert base to numpy
|
| 52 |
img_pil = base_image.convert("RGB")
|
| 53 |
img_np = np.array(img_pil)
|
|
|
|
| 135 |
|
| 136 |
return Image.fromarray(pattern_rgba, mode="RGBA")
|
| 137 |
|
| 138 |
+
# ===============================
|
| 139 |
+
# WRAPPER: ACCEPT BLOBS FROM DATA ARRAY
|
| 140 |
+
# ===============================
|
| 141 |
+
def process_saree(data):
|
| 142 |
+
"""
|
| 143 |
+
Accepts [base_blob, pattern_blob] as bytes (e.g., API POST with blobs)
|
| 144 |
+
"""
|
| 145 |
+
if not isinstance(data, (list, tuple)) or len(data) != 2:
|
| 146 |
+
raise ValueError("Expected an array with two elements: [base_blob, pattern_blob]")
|
| 147 |
+
|
| 148 |
+
base_blob, pattern_blob = data
|
| 149 |
+
|
| 150 |
+
if isinstance(base_blob, bytes):
|
| 151 |
+
base_image = Image.open(BytesIO(base_blob)).convert("RGBA")
|
| 152 |
+
else:
|
| 153 |
+
raise ValueError("Base image must be provided as bytes")
|
| 154 |
+
|
| 155 |
+
if isinstance(pattern_blob, bytes):
|
| 156 |
+
pattern_image = Image.open(BytesIO(pattern_blob)).convert("RGBA")
|
| 157 |
+
else:
|
| 158 |
+
raise ValueError("Pattern image must be provided as bytes")
|
| 159 |
+
|
| 160 |
+
return _process_saree_core(base_image, pattern_image)
|
| 161 |
+
|
| 162 |
# ===============================
|
| 163 |
# GRADIO INTERFACE
|
| 164 |
# ===============================
|
| 165 |
iface = gr.Interface(
|
| 166 |
fn=process_saree,
|
| 167 |
+
inputs=gr.Dataframe(headers=["Base Blob", "Pattern Blob"], type="array"),
|
|
|
|
| 168 |
outputs=gr.Image(type="pil", label="Final Saree Output"),
|
| 169 |
+
title="Saree Depth + Pattern Draping (Blob API Compatible)",
|
| 170 |
+
description="Send image blobs as array [base, pattern] or use Gradio UI for testing."
|
| 171 |
)
|
| 172 |
|
| 173 |
if __name__ == "__main__":
|