Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,85 +7,70 @@ import torchvision.transforms as transforms
|
|
7 |
from skimage.filters import sobel
|
8 |
from skimage.restoration import denoise_tv_chambolle
|
9 |
from scipy.interpolate import Rbf
|
10 |
-
from scipy.ndimage import map_coordinates
|
11 |
|
12 |
|
13 |
-
# Function to estimate a normal map from
|
14 |
def estimate_normal_map(image):
|
15 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
16 |
sobel_x = sobel(gray)
|
17 |
sobel_y = sobel(gray)
|
|
|
18 |
normal_map = np.stack([sobel_x, sobel_y, np.ones_like(sobel_x)], axis=-1)
|
19 |
-
normal_map
|
|
|
20 |
return (normal_map * 255).astype(np.uint8)
|
21 |
|
22 |
|
23 |
# Function to apply Thin Plate Spline (TPS) warping
|
24 |
-
def apply_tps_warping(
|
25 |
-
h, w =
|
26 |
x, y = np.meshgrid(np.arange(w), np.arange(h))
|
27 |
|
28 |
-
# Generate
|
29 |
control_x = x + (normal_map[:, :, 0] - 128) * 0.5
|
30 |
control_y = y + (normal_map[:, :, 1] - 128) * 0.5
|
31 |
|
32 |
-
#
|
33 |
rbf_x = Rbf(x.flatten(), y.flatten(), control_x.flatten(), function='thin_plate')
|
34 |
rbf_y = Rbf(x.flatten(), y.flatten(), control_y.flatten(), function='thin_plate')
|
35 |
|
36 |
warped_x = rbf_x(x, y).astype(np.float32)
|
37 |
warped_y = rbf_y(x, y).astype(np.float32)
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
return warped_text
|
43 |
|
|
|
44 |
|
45 |
-
|
46 |
-
|
|
|
47 |
cloth_bgr = np.array(cloth)
|
48 |
-
|
49 |
|
50 |
normal_map = estimate_normal_map(cloth_bgr)
|
51 |
|
52 |
-
# Resize
|
53 |
-
|
54 |
|
55 |
# Convert to grayscale and create a mask
|
56 |
-
|
57 |
-
_, mask = cv2.threshold(
|
58 |
|
59 |
-
# Warp
|
60 |
-
|
61 |
|
62 |
-
# Blend
|
63 |
-
center = (x +
|
64 |
-
blended = cv2.seamlessClone(
|
65 |
|
66 |
return Image.fromarray(blended)
|
67 |
|
68 |
|
69 |
# Gradio function
|
70 |
-
def process_image(cloth_image,
|
71 |
-
#
|
72 |
-
|
73 |
-
|
74 |
-
# Create a blank image with text
|
75 |
-
text_img = Image.new('RGB', (400, 200), (0, 0, 0, 0))
|
76 |
-
draw = ImageDraw.Draw(text_img)
|
77 |
-
|
78 |
-
try:
|
79 |
-
font = ImageFont.truetype("arial.ttf", font_size)
|
80 |
-
except:
|
81 |
-
font = ImageFont.load_default()
|
82 |
-
|
83 |
-
draw.text((50, 50), text, font=font, fill=font_color)
|
84 |
-
text_img = np.array(text_img)
|
85 |
-
|
86 |
-
# Blend text onto cloth
|
87 |
-
result = blend_text_cloth(cloth_image, text_img, x, y)
|
88 |
-
|
89 |
return result
|
90 |
|
91 |
|
@@ -94,15 +79,13 @@ interface = gr.Interface(
|
|
94 |
fn=process_image,
|
95 |
inputs=[
|
96 |
gr.Image(type="pil", label="Upload Cloth Image"),
|
97 |
-
gr.
|
98 |
-
gr.Slider(10, 100, step=2, label="Font Size", value=32),
|
99 |
-
gr.Textbox(label="Font Color (RGB)", value="(255, 0, 0)"),
|
100 |
gr.Slider(0, 1000, step=10, label="X Coordinate", value=50),
|
101 |
gr.Slider(0, 1000, step=10, label="Y Coordinate", value=50),
|
102 |
],
|
103 |
outputs=gr.Image(type="pil", label="Blended Output"),
|
104 |
-
title="Advanced
|
105 |
-
description="Upload a cloth image and blend
|
106 |
)
|
107 |
|
108 |
# Launch the app
|
|
|
7 |
from skimage.filters import sobel
|
8 |
from skimage.restoration import denoise_tv_chambolle
|
9 |
from scipy.interpolate import Rbf
|
|
|
10 |
|
11 |
|
12 |
+
# Function to estimate a normal map from cloth texture
|
13 |
def estimate_normal_map(image):
|
14 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
15 |
sobel_x = sobel(gray)
|
16 |
sobel_y = sobel(gray)
|
17 |
+
|
18 |
normal_map = np.stack([sobel_x, sobel_y, np.ones_like(sobel_x)], axis=-1)
|
19 |
+
normal_map /= np.linalg.norm(normal_map, axis=-1, keepdims=True)
|
20 |
+
|
21 |
return (normal_map * 255).astype(np.uint8)
|
22 |
|
23 |
|
24 |
# Function to apply Thin Plate Spline (TPS) warping
|
25 |
+
def apply_tps_warping(design, normal_map):
|
26 |
+
h, w = design.shape[:2]
|
27 |
x, y = np.meshgrid(np.arange(w), np.arange(h))
|
28 |
|
29 |
+
# Generate warp offsets from normal map
|
30 |
control_x = x + (normal_map[:, :, 0] - 128) * 0.5
|
31 |
control_y = y + (normal_map[:, :, 1] - 128) * 0.5
|
32 |
|
33 |
+
# Apply Radial Basis Function (RBF) interpolation
|
34 |
rbf_x = Rbf(x.flatten(), y.flatten(), control_x.flatten(), function='thin_plate')
|
35 |
rbf_y = Rbf(x.flatten(), y.flatten(), control_y.flatten(), function='thin_plate')
|
36 |
|
37 |
warped_x = rbf_x(x, y).astype(np.float32)
|
38 |
warped_y = rbf_y(x, y).astype(np.float32)
|
39 |
|
40 |
+
# Warp the design
|
41 |
+
warped_design = cv2.remap(design, warped_x, warped_y, interpolation=cv2.INTER_LINEAR)
|
|
|
|
|
42 |
|
43 |
+
return warped_design
|
44 |
|
45 |
+
|
46 |
+
# Function to blend design onto the cloth using Poisson Editing
|
47 |
+
def blend_design_cloth(cloth, design, x=50, y=50):
|
48 |
cloth_bgr = np.array(cloth)
|
49 |
+
design_bgr = np.array(design)
|
50 |
|
51 |
normal_map = estimate_normal_map(cloth_bgr)
|
52 |
|
53 |
+
# Resize design to fit the center of the cloth
|
54 |
+
design_resized = cv2.resize(design_bgr, (cloth_bgr.shape[1] // 2, cloth_bgr.shape[0] // 5))
|
55 |
|
56 |
# Convert to grayscale and create a mask
|
57 |
+
design_gray = cv2.cvtColor(design_resized, cv2.COLOR_BGR2GRAY)
|
58 |
+
_, mask = cv2.threshold(design_gray, 1, 255, cv2.THRESH_BINARY)
|
59 |
|
60 |
+
# Warp design using normal map
|
61 |
+
warped_design = apply_tps_warping(design_resized, normal_map)
|
62 |
|
63 |
+
# Blend using Poisson seamless cloning
|
64 |
+
center = (x + design_resized.shape[1] // 2, y + design_resized.shape[0] // 2)
|
65 |
+
blended = cv2.seamlessClone(warped_design, cloth_bgr, mask, center, cv2.MIXED_CLONE)
|
66 |
|
67 |
return Image.fromarray(blended)
|
68 |
|
69 |
|
70 |
# Gradio function
|
71 |
+
def process_image(cloth_image, design_image, x=50, y=50):
|
72 |
+
# Blend design onto cloth
|
73 |
+
result = blend_design_cloth(cloth_image, design_image, x, y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
return result
|
75 |
|
76 |
|
|
|
79 |
fn=process_image,
|
80 |
inputs=[
|
81 |
gr.Image(type="pil", label="Upload Cloth Image"),
|
82 |
+
gr.Image(type="pil", label="Upload Design"),
|
|
|
|
|
83 |
gr.Slider(0, 1000, step=10, label="X Coordinate", value=50),
|
84 |
gr.Slider(0, 1000, step=10, label="Y Coordinate", value=50),
|
85 |
],
|
86 |
outputs=gr.Image(type="pil", label="Blended Output"),
|
87 |
+
title="Advanced Cloth Design Blending",
|
88 |
+
description="Upload a cloth image and a design to blend them naturally using advanced warping & Poisson blending.",
|
89 |
)
|
90 |
|
91 |
# Launch the app
|