Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,6 @@ import torch
|
|
7 |
from torchvision import transforms
|
8 |
from torchvision.models.segmentation import deeplabv3_resnet101
|
9 |
|
10 |
-
# Load Pretrained DeepLabV3 Model
|
11 |
model = deeplabv3_resnet101(pretrained=True)
|
12 |
model.eval()
|
13 |
|
@@ -25,40 +24,33 @@ def segment_clothing(image):
|
|
25 |
output = model(input_tensor)['out'][0]
|
26 |
output_predictions = output.argmax(0).byte().cpu().numpy()
|
27 |
|
28 |
-
# Scale back to original size
|
29 |
mask = cv2.resize(output_predictions, (image.shape[1], image.shape[0]))
|
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
|
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 / 50.0
|
49 |
y_displacement = y + displacement_map / 50.0
|
50 |
|
51 |
-
# Warp text overlay using remap
|
52 |
warped = cv2.remap(text_overlay_array, x_displacement.astype(np.float32), y_displacement.astype(np.float32), interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
|
53 |
return Image.fromarray(warped)
|
54 |
|
55 |
def overlay_text(image, text, font_size, color, mask):
|
56 |
-
"""Overlay text onto the detected clothing region."""
|
57 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).convert("RGBA")
|
58 |
draw = ImageDraw.Draw(pil_image)
|
59 |
|
60 |
-
|
61 |
-
y_indices, x_indices = np.where(mask == 15) # Class 15 corresponds to 'person' in DeepLabV3
|
62 |
if len(x_indices) == 0 or len(y_indices) == 0:
|
63 |
return None, "No clothing region detected."
|
64 |
|
@@ -68,7 +60,6 @@ def overlay_text(image, text, font_size, color, mask):
|
|
68 |
clothing_width = x_max - x_min
|
69 |
clothing_height = y_max - y_min
|
70 |
|
71 |
-
# Load font and adjust size dynamically
|
72 |
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
|
73 |
if not os.path.exists(font_path):
|
74 |
return None, "Font file not found. Please provide a valid font path."
|
@@ -82,36 +73,34 @@ def overlay_text(image, text, font_size, color, mask):
|
|
82 |
font = ImageFont.truetype(font_path, font_size)
|
83 |
text_width, text_height = font.getbbox(text)[2:]
|
84 |
|
85 |
-
# Calculate position to center the text
|
86 |
text_x = x_min + (clothing_width - text_width) // 2
|
87 |
text_y = y_min + (clothing_height - text_height) // 2
|
88 |
|
89 |
-
# Draw the text on a transparent overlay
|
90 |
text_overlay = Image.new("RGBA", pil_image.size, (255, 255, 255, 0))
|
91 |
text_draw = ImageDraw.Draw(text_overlay)
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
return text_overlay, None
|
95 |
|
96 |
def process_image(image, text, font_size, color):
|
97 |
try:
|
98 |
-
# Segment the clothing using DeepLabV3
|
99 |
mask = segment_clothing(image)
|
100 |
if mask.sum() == 0:
|
101 |
return "No clothing detected. Try another image."
|
102 |
|
103 |
-
# Generate displacement map
|
104 |
displacement_map = generate_displacement_map(image, mask)
|
105 |
|
106 |
-
# Overlay the text
|
107 |
text_overlay, error = overlay_text(image, text, font_size, color, mask)
|
108 |
if error:
|
109 |
return error
|
110 |
|
111 |
-
# Warp text using displacement map
|
112 |
warped_text = warp_text(image, text_overlay, displacement_map)
|
113 |
|
114 |
-
# Blend the warped text back onto the original image
|
115 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).convert("RGBA")
|
116 |
final_image = Image.alpha_composite(pil_image, warped_text)
|
117 |
|
@@ -120,14 +109,13 @@ def process_image(image, text, font_size, color):
|
|
120 |
print(f"Error processing image: {str(e)}")
|
121 |
return f"Error: {str(e)}"
|
122 |
|
123 |
-
# Gradio Interface
|
124 |
gr.Interface(
|
125 |
fn=process_image,
|
126 |
inputs=[
|
127 |
gr.Image(type="numpy", label="Upload Clothing Image"),
|
128 |
gr.Textbox(label="Enter Text"),
|
129 |
gr.Slider(10, 150, step=5, label="Font Size"),
|
130 |
-
gr.ColorPicker(label="Text Color", value="#000000")
|
131 |
],
|
132 |
outputs=gr.Image(type="pil", label="Final Image with Warped Text"),
|
133 |
title="Warped Text Overlay on Clothing",
|
|
|
7 |
from torchvision import transforms
|
8 |
from torchvision.models.segmentation import deeplabv3_resnet101
|
9 |
|
|
|
10 |
model = deeplabv3_resnet101(pretrained=True)
|
11 |
model.eval()
|
12 |
|
|
|
24 |
output = model(input_tensor)['out'][0]
|
25 |
output_predictions = output.argmax(0).byte().cpu().numpy()
|
26 |
|
|
|
27 |
mask = cv2.resize(output_predictions, (image.shape[1], image.shape[0]))
|
28 |
return mask
|
29 |
|
30 |
def generate_displacement_map(image, mask):
|
|
|
31 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
32 |
blurred = cv2.GaussianBlur(gray, (15, 15), 0)
|
33 |
displacement_map = cv2.normalize(blurred, None, 0, 255, cv2.NORM_MINMAX)
|
34 |
+
displacement_map[mask != 15] = 0
|
35 |
return displacement_map
|
36 |
|
37 |
def warp_text(image, text_overlay, displacement_map):
|
|
|
38 |
text_overlay_array = np.array(text_overlay)
|
39 |
displacement_map = cv2.GaussianBlur(displacement_map, (15, 15), 0)
|
40 |
|
|
|
41 |
h, w = displacement_map.shape
|
42 |
x, y = np.meshgrid(np.arange(w), np.arange(h))
|
43 |
+
x_displacement = x + displacement_map / 50.0
|
44 |
y_displacement = y + displacement_map / 50.0
|
45 |
|
|
|
46 |
warped = cv2.remap(text_overlay_array, x_displacement.astype(np.float32), y_displacement.astype(np.float32), interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
|
47 |
return Image.fromarray(warped)
|
48 |
|
49 |
def overlay_text(image, text, font_size, color, mask):
|
|
|
50 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).convert("RGBA")
|
51 |
draw = ImageDraw.Draw(pil_image)
|
52 |
|
53 |
+
y_indices, x_indices = np.where(mask == 15)
|
|
|
54 |
if len(x_indices) == 0 or len(y_indices) == 0:
|
55 |
return None, "No clothing region detected."
|
56 |
|
|
|
60 |
clothing_width = x_max - x_min
|
61 |
clothing_height = y_max - y_min
|
62 |
|
|
|
63 |
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
|
64 |
if not os.path.exists(font_path):
|
65 |
return None, "Font file not found. Please provide a valid font path."
|
|
|
73 |
font = ImageFont.truetype(font_path, font_size)
|
74 |
text_width, text_height = font.getbbox(text)[2:]
|
75 |
|
|
|
76 |
text_x = x_min + (clothing_width - text_width) // 2
|
77 |
text_y = y_min + (clothing_height - text_height) // 2
|
78 |
|
|
|
79 |
text_overlay = Image.new("RGBA", pil_image.size, (255, 255, 255, 0))
|
80 |
text_draw = ImageDraw.Draw(text_overlay)
|
81 |
+
|
82 |
+
try:
|
83 |
+
rgba_color = tuple(color) + (255,)
|
84 |
+
text_draw.text((text_x, text_y), text, font=font, fill=rgba_color)
|
85 |
+
except Exception as e:
|
86 |
+
return None, f"Error applying color: {str(e)}"
|
87 |
|
88 |
return text_overlay, None
|
89 |
|
90 |
def process_image(image, text, font_size, color):
|
91 |
try:
|
|
|
92 |
mask = segment_clothing(image)
|
93 |
if mask.sum() == 0:
|
94 |
return "No clothing detected. Try another image."
|
95 |
|
|
|
96 |
displacement_map = generate_displacement_map(image, mask)
|
97 |
|
|
|
98 |
text_overlay, error = overlay_text(image, text, font_size, color, mask)
|
99 |
if error:
|
100 |
return error
|
101 |
|
|
|
102 |
warped_text = warp_text(image, text_overlay, displacement_map)
|
103 |
|
|
|
104 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).convert("RGBA")
|
105 |
final_image = Image.alpha_composite(pil_image, warped_text)
|
106 |
|
|
|
109 |
print(f"Error processing image: {str(e)}")
|
110 |
return f"Error: {str(e)}"
|
111 |
|
|
|
112 |
gr.Interface(
|
113 |
fn=process_image,
|
114 |
inputs=[
|
115 |
gr.Image(type="numpy", label="Upload Clothing Image"),
|
116 |
gr.Textbox(label="Enter Text"),
|
117 |
gr.Slider(10, 150, step=5, label="Font Size"),
|
118 |
+
gr.ColorPicker(label="Text Color", value="#000000")
|
119 |
],
|
120 |
outputs=gr.Image(type="pil", label="Final Image with Warped Text"),
|
121 |
title="Warped Text Overlay on Clothing",
|