Spaces:
Running
Running
fragger246
commited on
Commit
•
21e64dd
1
Parent(s):
9cb0f11
Update app.py
Browse files
app.py
CHANGED
@@ -1,110 +1,109 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import torch
|
3 |
-
from PIL import Image
|
4 |
-
import numpy as np
|
5 |
-
import cv2
|
6 |
-
from diffusers import StableDiffusionPipeline
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
model_id = "
|
14 |
-
pipe =
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
image
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
b, g, r
|
29 |
-
|
30 |
-
design_np
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
["
|
36 |
-
["
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
with gr.
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
demo.queue().launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
import cv2
|
6 |
+
from diffusers import StableDiffusionPipeline
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
# Setup the model
|
11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
+
model_id = "s3nh/artwork-arcane-stable-diffusion"
|
13 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16 if device == "cuda" else torch.float32, use_auth_token=True)
|
14 |
+
pipe = pipe.to(device)
|
15 |
+
|
16 |
+
# Generate T-shirt design function
|
17 |
+
def generate_tshirt_design(text):
|
18 |
+
prompt = f"{text}"
|
19 |
+
image = pipe(prompt).images[0]
|
20 |
+
return image
|
21 |
+
|
22 |
+
# Remove background from the generated design
|
23 |
+
def remove_background(design_image):
|
24 |
+
design_np = np.array(design_image)
|
25 |
+
gray = cv2.cvtColor(design_np, cv2.COLOR_BGR2GRAY)
|
26 |
+
_, alpha = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)
|
27 |
+
b, g, r = cv2.split(design_np)
|
28 |
+
rgba = [b, g, r, alpha]
|
29 |
+
design_np = cv2.merge(rgba, 4)
|
30 |
+
return design_np
|
31 |
+
|
32 |
+
# T-shirt mockup generator with Gradio interface
|
33 |
+
examples = [
|
34 |
+
["MyBrand"],
|
35 |
+
["Hello World"],
|
36 |
+
["Team logo"],
|
37 |
+
]
|
38 |
+
|
39 |
+
css = """
|
40 |
+
#col-container {
|
41 |
+
margin: 0 auto;
|
42 |
+
max-width: 520px;
|
43 |
+
}
|
44 |
+
"""
|
45 |
+
|
46 |
+
with gr.Blocks(css=css) as demo:
|
47 |
+
with gr.Column(elem_id="col-container"):
|
48 |
+
gr.Markdown("""
|
49 |
+
# T-shirt Design Generator with Stable Diffusion
|
50 |
+
""")
|
51 |
+
|
52 |
+
with gr.Row():
|
53 |
+
text = gr.Textbox(
|
54 |
+
label="Text",
|
55 |
+
placeholder="Enter text for the T-shirt design",
|
56 |
+
visible=True,
|
57 |
+
)
|
58 |
+
|
59 |
+
run_button = gr.Button("Generate Design", scale=0)
|
60 |
+
|
61 |
+
result = gr.Image(label="Design", show_label=False)
|
62 |
+
|
63 |
+
gr.Examples(
|
64 |
+
examples=examples,
|
65 |
+
inputs=[text]
|
66 |
+
)
|
67 |
+
|
68 |
+
def generate_tshirt_mockup(text):
|
69 |
+
# Generate T-shirt design
|
70 |
+
design_image = generate_tshirt_design(text)
|
71 |
+
|
72 |
+
# Remove background from design image
|
73 |
+
design_np = remove_background(design_image)
|
74 |
+
|
75 |
+
# Load blank T-shirt mockup template image
|
76 |
+
mockup_template = Image.open("/content/drive/MyDrive/unnamed.jpg")
|
77 |
+
|
78 |
+
# Convert mockup template to numpy array
|
79 |
+
mockup_np = np.array(mockup_template)
|
80 |
+
|
81 |
+
# Resize design image to fit mockup
|
82 |
+
design_resized = cv2.resize(design_np, (mockup_np.shape[1] // 4, mockup_np.shape[0] // 4)) # Adjust size as needed
|
83 |
+
|
84 |
+
# Center the design on the mockup
|
85 |
+
y_offset = (mockup_np.shape[0] - design_resized.shape[0]) // 2
|
86 |
+
x_offset = (mockup_np.shape[1] - design_resized.shape[1]) // 2
|
87 |
+
y1, y2 = y_offset, y_offset + design_resized.shape[0]
|
88 |
+
x1, x2 = x_offset, x_offset + design_resized.shape[1]
|
89 |
+
|
90 |
+
# Blend design with mockup using alpha channel
|
91 |
+
alpha_s = design_resized[:, :, 3] / 255.0 if design_resized.shape[2] == 4 else np.ones(design_resized.shape[:2])
|
92 |
+
alpha_l = 1.0 - alpha_s
|
93 |
+
|
94 |
+
for c in range(0, 3):
|
95 |
+
mockup_np[y1:y2, x1:x2, c] = (alpha_s * design_resized[:, :, c] +
|
96 |
+
alpha_l * mockup_np[y1:y2, x1:x2, c])
|
97 |
+
|
98 |
+
# Convert back to PIL image for Gradio output
|
99 |
+
result_image = Image.fromarray(mockup_np)
|
100 |
+
|
101 |
+
return result_image
|
102 |
+
|
103 |
+
run_button.click(
|
104 |
+
fn=generate_tshirt_mockup,
|
105 |
+
inputs=[text],
|
106 |
+
outputs=[result]
|
107 |
+
)
|
108 |
+
|
109 |
+
demo.queue().launch()
|
|