Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,12 +2,12 @@ import gradio as gr
|
|
2 |
from PIL import Image, ImageDraw, ImageFilter
|
3 |
import requests
|
4 |
from io import BytesIO
|
|
|
5 |
import torch
|
6 |
import torchvision.transforms as T
|
7 |
from torchvision import models
|
8 |
-
import numpy as np
|
9 |
-
import cv2
|
10 |
|
|
|
11 |
repo = "artificialguybr/TshirtDesignRedmond-V2"
|
12 |
|
13 |
def generate_cloth(color_prompt):
|
@@ -32,92 +32,40 @@ def generate_design(design_prompt):
|
|
32 |
else:
|
33 |
raise Exception(f"Error generating design: {response.status_code}")
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
def get_tshirt_mask(image):
|
38 |
-
image = image.convert("RGB") # Ensure 3 channels
|
39 |
-
preprocess = T.Compose([
|
40 |
-
T.Resize((520, 520)), # Resize to avoid distortion
|
41 |
-
T.ToTensor(),
|
42 |
-
T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
43 |
-
])
|
44 |
-
input_tensor = preprocess(image).unsqueeze(0)
|
45 |
-
with torch.no_grad():
|
46 |
-
output = segmentation_model(input_tensor)["out"][0]
|
47 |
-
|
48 |
-
mask = output.argmax(0).byte().cpu().numpy()
|
49 |
-
raw_mask = Image.fromarray((mask == 15).astype("uint8") * 255) # Binary mask
|
50 |
-
processed_mask = post_process_mask(raw_mask) # Apply post-processing
|
51 |
-
return processed_mask.resize(image.size)
|
52 |
-
|
53 |
-
def post_process_mask(mask):
|
54 |
-
mask_np = np.array(mask)
|
55 |
-
|
56 |
-
kernel = np.ones((5, 5), np.uint8)
|
57 |
-
mask_np = cv2.dilate(mask_np, kernel, iterations=2) # Expand mask
|
58 |
-
mask_np = cv2.erode(mask_np, kernel, iterations=1) # Remove noise
|
59 |
-
|
60 |
-
processed_mask = Image.fromarray(mask_np).filter(ImageFilter.GaussianBlur(3))
|
61 |
-
return processed_mask
|
62 |
-
|
63 |
-
def get_bounding_box(mask):
|
64 |
-
mask_np = np.array(mask)
|
65 |
-
coords = np.column_stack(np.where(mask_np > 0))
|
66 |
-
if coords.size == 0:
|
67 |
-
raise Exception("No T-shirt detected in the image.")
|
68 |
-
x_min, y_min = coords.min(axis=0)
|
69 |
-
x_max, y_max = coords.max(axis=0)
|
70 |
-
return (x_min, y_min, x_max, y_max)
|
71 |
-
|
72 |
-
def visualize_mask(image, mask):
|
73 |
-
overlay = image.copy().convert("RGBA")
|
74 |
-
draw = ImageDraw.Draw(overlay)
|
75 |
-
bbox = get_bounding_box(mask)
|
76 |
-
draw.rectangle(bbox, outline="red", width=3) # Draw bounding box
|
77 |
-
blended = Image.blend(image.convert("RGBA"), overlay, alpha=0.5) # Overlay mask
|
78 |
-
blended.save("debug_visualization.png") # Save debug image
|
79 |
-
return blended
|
80 |
-
|
81 |
-
def overlay_design(cloth_image, design_image):
|
82 |
cloth_image = cloth_image.convert("RGBA")
|
83 |
design_image = design_image.convert("RGBA")
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
bbox = get_bounding_box(mask)
|
88 |
-
tshirt_width = bbox[2] - bbox[0]
|
89 |
-
tshirt_height = bbox[3] - bbox[1]
|
90 |
-
|
91 |
scale_factor = 0.7
|
92 |
-
|
93 |
-
|
|
|
94 |
design_image = design_image.resize((design_width, design_height), Image.Resampling.LANCZOS)
|
95 |
|
96 |
-
|
97 |
-
|
|
|
98 |
|
|
|
99 |
transparent_layer = Image.new("RGBA", cloth_image.size, (0, 0, 0, 0))
|
100 |
transparent_layer.paste(design_image, (x_offset, y_offset), design_image)
|
101 |
|
|
|
102 |
final_image = Image.alpha_composite(cloth_image, transparent_layer)
|
103 |
return final_image
|
104 |
|
105 |
-
def debug_intermediate_outputs(cloth_image, mask):
|
106 |
-
cloth_image.save("debug_cloth_image.png")
|
107 |
-
mask.save("debug_tshirt_mask.png")
|
108 |
-
|
109 |
def design_tshirt(color_prompt, design_prompt):
|
110 |
-
cloth_image = generate_cloth(color_prompt)
|
111 |
-
design_image = generate_design(design_prompt)
|
112 |
try:
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
final_image = overlay_design(cloth_image, design_image)
|
117 |
return final_image
|
118 |
except Exception as e:
|
119 |
raise Exception(f"Error in design process: {str(e)}")
|
120 |
|
|
|
121 |
with gr.Blocks() as interface:
|
122 |
gr.Markdown("# **AI Cloth Designer**")
|
123 |
gr.Markdown("Generate custom T-shirts by specifying a color and adding a design that perfectly fits the T-shirt.")
|
@@ -135,4 +83,4 @@ with gr.Blocks() as interface:
|
|
135 |
outputs=output_image,
|
136 |
)
|
137 |
|
138 |
-
interface.launch(debug=True)
|
|
|
2 |
from PIL import Image, ImageDraw, ImageFilter
|
3 |
import requests
|
4 |
from io import BytesIO
|
5 |
+
import numpy as np
|
6 |
import torch
|
7 |
import torchvision.transforms as T
|
8 |
from torchvision import models
|
|
|
|
|
9 |
|
10 |
+
# AI model repo for design generation
|
11 |
repo = "artificialguybr/TshirtDesignRedmond-V2"
|
12 |
|
13 |
def generate_cloth(color_prompt):
|
|
|
32 |
else:
|
33 |
raise Exception(f"Error generating design: {response.status_code}")
|
34 |
|
35 |
+
def overlay_design_on_tshirt(cloth_image, design_image):
|
36 |
+
# Ensure images are in RGBA mode
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
cloth_image = cloth_image.convert("RGBA")
|
38 |
design_image = design_image.convert("RGBA")
|
39 |
|
40 |
+
# Resize the design to fit within 70% of the T-shirt's dimensions
|
|
|
|
|
|
|
|
|
|
|
41 |
scale_factor = 0.7
|
42 |
+
width, height = cloth_image.size
|
43 |
+
design_width = int(width * scale_factor)
|
44 |
+
design_height = int(height * scale_factor)
|
45 |
design_image = design_image.resize((design_width, design_height), Image.Resampling.LANCZOS)
|
46 |
|
47 |
+
# Calculate position to center the design on the T-shirt
|
48 |
+
x_offset = (width - design_width) // 2
|
49 |
+
y_offset = (height - design_height) // 2
|
50 |
|
51 |
+
# Create a transparent layer for the design
|
52 |
transparent_layer = Image.new("RGBA", cloth_image.size, (0, 0, 0, 0))
|
53 |
transparent_layer.paste(design_image, (x_offset, y_offset), design_image)
|
54 |
|
55 |
+
# Blend the T-shirt with the design
|
56 |
final_image = Image.alpha_composite(cloth_image, transparent_layer)
|
57 |
return final_image
|
58 |
|
|
|
|
|
|
|
|
|
59 |
def design_tshirt(color_prompt, design_prompt):
|
|
|
|
|
60 |
try:
|
61 |
+
cloth_image = generate_cloth(color_prompt)
|
62 |
+
design_image = generate_design(design_prompt)
|
63 |
+
final_image = overlay_design_on_tshirt(cloth_image, design_image)
|
|
|
64 |
return final_image
|
65 |
except Exception as e:
|
66 |
raise Exception(f"Error in design process: {str(e)}")
|
67 |
|
68 |
+
# Gradio UI
|
69 |
with gr.Blocks() as interface:
|
70 |
gr.Markdown("# **AI Cloth Designer**")
|
71 |
gr.Markdown("Generate custom T-shirts by specifying a color and adding a design that perfectly fits the T-shirt.")
|
|
|
83 |
outputs=output_image,
|
84 |
)
|
85 |
|
86 |
+
interface.launch(debug=True)
|