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