Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,118 +1,48 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from PIL import Image, ImageDraw, ImageFilter
|
3 |
import torch
|
4 |
-
|
5 |
-
import
|
6 |
-
from
|
7 |
-
import numpy as np
|
8 |
import cv2
|
|
|
9 |
|
10 |
-
# Load
|
11 |
-
|
12 |
-
|
13 |
-
"
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
image =
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
# Extract T-shirt mask (class 15 in COCO dataset)
|
52 |
-
mask = output.argmax(0).byte().cpu().numpy()
|
53 |
-
raw_mask = Image.fromarray((mask == 15).astype("uint8") * 255) # Binary mask
|
54 |
-
processed_mask = post_process_mask(raw_mask) # Apply post-processing
|
55 |
-
return processed_mask.resize(image.size)
|
56 |
-
|
57 |
-
# Post-process mask to improve quality
|
58 |
-
def post_process_mask(mask):
|
59 |
-
# Convert mask to NumPy array
|
60 |
-
mask_np = np.array(mask)
|
61 |
-
|
62 |
-
# Morphological operations to refine mask
|
63 |
-
kernel = np.ones((5, 5), np.uint8)
|
64 |
-
mask_np = cv2.dilate(mask_np, kernel, iterations=2) # Expand mask
|
65 |
-
mask_np = cv2.erode(mask_np, kernel, iterations=1) # Remove noise
|
66 |
-
|
67 |
-
# Convert back to PIL image and smooth
|
68 |
-
processed_mask = Image.fromarray(mask_np).filter(ImageFilter.GaussianBlur(3))
|
69 |
-
return processed_mask
|
70 |
-
|
71 |
-
# Blend design seamlessly with T-shirt
|
72 |
-
def blend_design_with_tshirt(cloth_image, design_image, mask):
|
73 |
-
# Resize the design to match T-shirt bounding box
|
74 |
-
mask_np = np.array(mask) / 255.0
|
75 |
-
cloth_np = np.array(cloth_image)
|
76 |
-
design_np = np.array(design_image.resize(cloth_image.size))
|
77 |
-
|
78 |
-
# Perform alpha blending with mask
|
79 |
-
blended = (design_np[..., :3] * mask_np[..., None] + cloth_np * (1 - mask_np[..., None])).astype(np.uint8)
|
80 |
-
|
81 |
-
return Image.fromarray(blended)
|
82 |
-
|
83 |
-
# Main function to process T-shirt and design
|
84 |
-
def design_tshirt(color_prompt, design_prompt):
|
85 |
-
try:
|
86 |
-
# Generate cloth and design images
|
87 |
-
cloth_image = generate_cloth(color_prompt)
|
88 |
-
design_image = generate_design(design_prompt)
|
89 |
-
|
90 |
-
# Get T-shirt mask
|
91 |
-
mask = get_tshirt_mask(cloth_image)
|
92 |
-
|
93 |
-
# Blend the design with the T-shirt
|
94 |
-
final_image = blend_design_with_tshirt(cloth_image, design_image, mask)
|
95 |
-
return final_image
|
96 |
-
|
97 |
-
except Exception as e:
|
98 |
-
return f"An error occurred: {str(e)}. Please try again with different inputs."
|
99 |
-
|
100 |
-
# Gradio UI
|
101 |
-
with gr.Blocks() as interface:
|
102 |
-
gr.Markdown("# **AI T-Shirt Designer**")
|
103 |
-
gr.Markdown("Generate custom T-shirts by specifying a color and adding a design that blends perfectly with the T-shirt.")
|
104 |
-
with gr.Row():
|
105 |
-
with gr.Column():
|
106 |
-
color_prompt = gr.Textbox(label="T-Shirt Color", placeholder="E.g., Red, Blue")
|
107 |
-
design_prompt = gr.Textbox(label="Design Details", placeholder="E.g., Abstract art, Nature patterns")
|
108 |
-
generate_button = gr.Button("Generate T-Shirt")
|
109 |
-
with gr.Column():
|
110 |
-
output_image = gr.Image(label="Final T-Shirt Design")
|
111 |
-
|
112 |
-
generate_button.click(
|
113 |
-
design_tshirt,
|
114 |
-
inputs=[color_prompt, design_prompt],
|
115 |
-
outputs=output_image,
|
116 |
-
)
|
117 |
-
|
118 |
-
interface.launch(debug=True)
|
|
|
|
|
|
|
1 |
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
4 |
+
from PIL import Image
|
|
|
5 |
import cv2
|
6 |
+
import numpy as np
|
7 |
|
8 |
+
# Load ControlNet pre-trained model for depth-based warping
|
9 |
+
controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11f1p_sd15_depth", torch_dtype=torch.float16)
|
10 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
11 |
+
"runwayml/stable-diffusion-v1-5",
|
12 |
+
controlnet=controlnet,
|
13 |
+
torch_dtype=torch.float16
|
14 |
+
).to("cuda")
|
15 |
+
|
16 |
+
# Function to generate depth map of the cloth
|
17 |
+
def generate_depth_map(image_path):
|
18 |
+
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
|
19 |
+
depth_map = cv2.Laplacian(image, cv2.CV_64F)
|
20 |
+
depth_map = cv2.normalize(depth_map, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
|
21 |
+
return Image.fromarray(depth_map)
|
22 |
+
|
23 |
+
# Function to blend design onto fabric using ControlNet
|
24 |
+
def blend_design_on_cloth(fabric_image, design_image, prompt="T-shirt with embedded design"):
|
25 |
+
# Generate depth map for fabric
|
26 |
+
depth_map = generate_depth_map(fabric_image)
|
27 |
+
|
28 |
+
# Generate realistic blended output
|
29 |
+
result = pipe(prompt=prompt, image=fabric_image, control_image=depth_map, num_inference_steps=30).images[0]
|
30 |
+
return result
|
31 |
+
|
32 |
+
# Gradio interface
|
33 |
+
def process_image(fabric_image, design_image):
|
34 |
+
result = blend_design_on_cloth(fabric_image, design_image)
|
35 |
+
return result
|
36 |
+
|
37 |
+
interface = gr.Interface(
|
38 |
+
fn=process_image,
|
39 |
+
inputs=[gr.Image(type="pil", label="Upload Cloth Image"),
|
40 |
+
gr.Image(type="pil", label="Upload Design/Text Image")],
|
41 |
+
outputs=gr.Image(type="pil", label="Blended Output"),
|
42 |
+
title="AI-Powered Text & Design Blending on Cloth",
|
43 |
+
description="Upload a T-shirt or fabric image and a design. AI will blend it naturally into the fabric!",
|
44 |
+
)
|
45 |
+
|
46 |
+
# Launch app for Hugging Face Spaces
|
47 |
+
if __name__ == "__main__":
|
48 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|