Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -27,27 +27,25 @@ def segment_clothing(image):
|
|
27 |
|
28 |
# Scale back to original size
|
29 |
mask = cv2.resize(output_predictions, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_NEAREST)
|
30 |
-
print(f"Mask shape: {mask.shape}, unique values: {np.unique(mask)}") # Debugging
|
31 |
return mask
|
32 |
|
33 |
def generate_displacement_map(image, mask):
|
34 |
"""Generate a displacement map from the clothing region."""
|
35 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
36 |
-
blurred = cv2.GaussianBlur(gray, (
|
37 |
displacement_map = cv2.normalize(blurred, None, 0, 255, cv2.NORM_MINMAX)
|
38 |
displacement_map[mask != 15] = 0 # Apply mask (class 15 corresponds to 'person')
|
39 |
-
print(f"Displacement map stats - Min: {np.min(displacement_map)}, Max: {np.max(displacement_map)}") # Debugging
|
40 |
return displacement_map
|
41 |
|
42 |
def warp_text(image, text_overlay, displacement_map):
|
43 |
"""Warp the text overlay based on the displacement map."""
|
44 |
text_overlay_array = np.array(text_overlay)
|
45 |
-
displacement_map = cv2.GaussianBlur(displacement_map, (
|
46 |
|
47 |
# Create an x, y distortion map
|
48 |
h, w = displacement_map.shape
|
49 |
x, y = np.meshgrid(np.arange(w), np.arange(h))
|
50 |
-
x_displacement = x + displacement_map / 100.0
|
51 |
y_displacement = y + displacement_map / 100.0
|
52 |
|
53 |
# Warp text overlay using remap
|
@@ -60,57 +58,31 @@ def warp_text(image, text_overlay, displacement_map):
|
|
60 |
)
|
61 |
return Image.fromarray(warped)
|
62 |
|
63 |
-
def overlay_text(image, text, font_size, color, mask):
|
64 |
"""Overlay text onto the detected clothing region."""
|
65 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).convert("RGBA")
|
66 |
|
67 |
-
|
68 |
-
y_indices, x_indices = np.where(mask == 15) # Class 15 corresponds to 'person' in DeepLabV3
|
69 |
if len(x_indices) == 0 or len(y_indices) == 0:
|
70 |
return None, "No clothing region detected."
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
# Ensure the color is correctly formatted
|
79 |
-
color = color.lstrip('#')
|
80 |
-
color_tuple = tuple(int(color[i:i+2], 16) for i in (0, 2, 4))
|
81 |
-
|
82 |
-
# Load font and adjust size dynamically
|
83 |
-
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
|
84 |
-
if not os.path.exists(font_path):
|
85 |
-
return None, "Font file not found. Please provide a valid font path."
|
86 |
|
|
|
87 |
font = ImageFont.truetype(font_path, font_size)
|
88 |
-
text_width, text_height = font.getbbox(text)[2:]
|
89 |
-
while text_width > clothing_width or text_height > clothing_height:
|
90 |
-
font_size -= 1
|
91 |
-
if font_size <= 5:
|
92 |
-
return None, "Text too large to fit on the clothing. Try smaller text or font size."
|
93 |
-
font = ImageFont.truetype(font_path, font_size)
|
94 |
-
text_width, text_height = font.getbbox(text)[2:]
|
95 |
-
|
96 |
-
# Calculate position to center the text
|
97 |
-
text_x = x_min + (clothing_width - text_width) // 2
|
98 |
-
text_y = y_min + (clothing_height - text_height) // 2
|
99 |
-
|
100 |
-
# Draw the text on a transparent overlay
|
101 |
text_overlay = Image.new("RGBA", pil_image.size, (255, 255, 255, 0))
|
102 |
text_draw = ImageDraw.Draw(text_overlay)
|
103 |
-
|
104 |
-
rgba_color = color_tuple + (255,) # Add alpha channel
|
105 |
-
text_draw.text((text_x, text_y), text, font=font, fill=rgba_color)
|
106 |
-
except Exception as e:
|
107 |
-
return None, f"Error applying color: {str(e)}"
|
108 |
-
|
109 |
return text_overlay, None
|
110 |
|
111 |
-
def process_image(image, text, font_size, color):
|
112 |
try:
|
113 |
-
# Segment
|
114 |
mask = segment_clothing(image)
|
115 |
if mask.sum() == 0:
|
116 |
return "No clothing detected. Try another image."
|
@@ -118,33 +90,38 @@ def process_image(image, text, font_size, color):
|
|
118 |
# Generate displacement map
|
119 |
displacement_map = generate_displacement_map(image, mask)
|
120 |
|
121 |
-
# Overlay
|
122 |
-
|
|
|
123 |
if error:
|
124 |
return error
|
125 |
|
126 |
# Warp text using displacement map
|
127 |
warped_text = warp_text(image, text_overlay, displacement_map)
|
128 |
|
129 |
-
# Blend
|
130 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).convert("RGBA")
|
131 |
final_image = Image.alpha_composite(pil_image, warped_text).convert("RGB")
|
132 |
|
133 |
return final_image
|
134 |
except Exception as e:
|
135 |
-
print(f"Error processing image: {str(e)}")
|
136 |
return f"Error: {str(e)}"
|
137 |
|
138 |
# Gradio Interface
|
|
|
|
|
139 |
gr.Interface(
|
140 |
fn=process_image,
|
141 |
inputs=[
|
142 |
gr.Image(type="numpy", label="Upload Clothing Image"),
|
143 |
gr.Textbox(label="Enter Text"),
|
144 |
gr.Slider(10, 150, step=5, label="Font Size"),
|
145 |
-
gr.ColorPicker(label="Text Color", value="#000000")
|
|
|
|
|
|
|
146 |
],
|
147 |
outputs=gr.Image(type="pil", label="Final Image with Warped Text"),
|
148 |
title="Warped Text Overlay on Clothing",
|
149 |
-
description="Upload a clothing image and
|
150 |
).launch()
|
|
|
27 |
|
28 |
# Scale back to original size
|
29 |
mask = cv2.resize(output_predictions, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_NEAREST)
|
|
|
30 |
return mask
|
31 |
|
32 |
def generate_displacement_map(image, mask):
|
33 |
"""Generate a displacement map from the clothing region."""
|
34 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
35 |
+
blurred = cv2.GaussianBlur(gray, (15, 15), 0)
|
36 |
displacement_map = cv2.normalize(blurred, None, 0, 255, cv2.NORM_MINMAX)
|
37 |
displacement_map[mask != 15] = 0 # Apply mask (class 15 corresponds to 'person')
|
|
|
38 |
return displacement_map
|
39 |
|
40 |
def warp_text(image, text_overlay, displacement_map):
|
41 |
"""Warp the text overlay based on the displacement map."""
|
42 |
text_overlay_array = np.array(text_overlay)
|
43 |
+
displacement_map = cv2.GaussianBlur(displacement_map, (15, 15), 0)
|
44 |
|
45 |
# Create an x, y distortion map
|
46 |
h, w = displacement_map.shape
|
47 |
x, y = np.meshgrid(np.arange(w), np.arange(h))
|
48 |
+
x_displacement = x + displacement_map / 100.0
|
49 |
y_displacement = y + displacement_map / 100.0
|
50 |
|
51 |
# Warp text overlay using remap
|
|
|
58 |
)
|
59 |
return Image.fromarray(warped)
|
60 |
|
61 |
+
def overlay_text(image, text, font_path, font_size, color, mask, manual_coords=None):
|
62 |
"""Overlay text onto the detected clothing region."""
|
63 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).convert("RGBA")
|
64 |
|
65 |
+
y_indices, x_indices = np.where(mask == 15)
|
|
|
66 |
if len(x_indices) == 0 or len(y_indices) == 0:
|
67 |
return None, "No clothing region detected."
|
68 |
|
69 |
+
if manual_coords:
|
70 |
+
text_x, text_y = manual_coords
|
71 |
+
else:
|
72 |
+
x_min, x_max = x_indices.min(), x_indices.max()
|
73 |
+
y_min, y_max = y_indices.min(), y_indices.max()
|
74 |
+
text_x, text_y = x_min, y_min
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
+
# Load font and create transparent overlay for text
|
77 |
font = ImageFont.truetype(font_path, font_size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
text_overlay = Image.new("RGBA", pil_image.size, (255, 255, 255, 0))
|
79 |
text_draw = ImageDraw.Draw(text_overlay)
|
80 |
+
text_draw.text((text_x, text_y), text, font=font, fill=color)
|
|
|
|
|
|
|
|
|
|
|
81 |
return text_overlay, None
|
82 |
|
83 |
+
def process_image(image, text, font_size, color, font_path, placement_x, placement_y):
|
84 |
try:
|
85 |
+
# Segment clothing using DeepLabV3
|
86 |
mask = segment_clothing(image)
|
87 |
if mask.sum() == 0:
|
88 |
return "No clothing detected. Try another image."
|
|
|
90 |
# Generate displacement map
|
91 |
displacement_map = generate_displacement_map(image, mask)
|
92 |
|
93 |
+
# Overlay text with manual coordinates (if provided)
|
94 |
+
manual_coords = (placement_x, placement_y)
|
95 |
+
text_overlay, error = overlay_text(image, text, font_path, font_size, color, mask, manual_coords)
|
96 |
if error:
|
97 |
return error
|
98 |
|
99 |
# Warp text using displacement map
|
100 |
warped_text = warp_text(image, text_overlay, displacement_map)
|
101 |
|
102 |
+
# Blend warped text with the original image
|
103 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).convert("RGBA")
|
104 |
final_image = Image.alpha_composite(pil_image, warped_text).convert("RGB")
|
105 |
|
106 |
return final_image
|
107 |
except Exception as e:
|
|
|
108 |
return f"Error: {str(e)}"
|
109 |
|
110 |
# Gradio Interface
|
111 |
+
font_options = ["/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", "/path/to/another/font.ttf"]
|
112 |
+
|
113 |
gr.Interface(
|
114 |
fn=process_image,
|
115 |
inputs=[
|
116 |
gr.Image(type="numpy", label="Upload Clothing Image"),
|
117 |
gr.Textbox(label="Enter Text"),
|
118 |
gr.Slider(10, 150, step=5, label="Font Size"),
|
119 |
+
gr.ColorPicker(label="Text Color", value="#000000"),
|
120 |
+
gr.Dropdown(choices=font_options, label="Select Font", value=font_options[0]),
|
121 |
+
gr.Number(label="Text Placement X", value=0),
|
122 |
+
gr.Number(label="Text Placement Y", value=0),
|
123 |
],
|
124 |
outputs=gr.Image(type="pil", label="Final Image with Warped Text"),
|
125 |
title="Warped Text Overlay on Clothing",
|
126 |
+
description="Upload a clothing image and customize text placement, font, and style."
|
127 |
).launch()
|