gaur3009 commited on
Commit
ea949b2
·
verified ·
1 Parent(s): 1cea836

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -24
app.py CHANGED
@@ -1,12 +1,23 @@
1
  import gradio as gr
2
- from PIL import Image, ImageDraw
3
  import requests
4
  from io import BytesIO
5
- import os
 
 
6
 
7
  # AI model repo for design generation
8
  repo = "artificialguybr/TshirtDesignRedmond-V2"
9
 
 
 
 
 
 
 
 
 
 
10
  # Generate plain cloth image with specified color
11
  def generate_cloth(color_prompt):
12
  prompt = f"A plain {color_prompt} colored T-shirt hanging on a plain wall."
@@ -41,49 +52,44 @@ def generate_design(design_prompt):
41
  else:
42
  raise Exception(f"Error generating design: {response.status_code}")
43
 
44
- # Overlay design on cloth with adjustable width and height
45
- def overlay_design(cloth_image, design_image, x, y, width, height):
46
- # Ensure images are in RGBA mode
47
- cloth_image = cloth_image.convert("RGBA")
48
- design_image = design_image.convert("RGBA")
49
-
50
- # Resize design based on user inputs
51
- resized_design = design_image.resize((width, height))
52
-
53
- # Overlay the design at specified coordinates
54
- result = cloth_image.copy()
55
- result.paste(resized_design, (x, y), resized_design)
56
 
57
- return result
 
 
 
58
 
59
  # Full workflow: Generate cloth, design, and combine them
60
- def design_tshirt(color_prompt, design_prompt, x, y, width, height):
61
  cloth_image = generate_cloth(color_prompt)
62
  design_image = generate_design(design_prompt)
63
- final_image = overlay_design(cloth_image, design_image, x, y, width, height)
64
  return final_image
65
 
66
  # Gradio interface
67
  with gr.Blocks() as interface:
68
  gr.Markdown("# **AI Cloth Designer**")
69
- gr.Markdown("Generate custom T-shirts by specifying a color and adding a draggable design with adjustable size.")
70
 
71
  with gr.Row():
72
  with gr.Column(scale=1):
73
  color_prompt = gr.Textbox(label="Cloth Color", placeholder="E.g., Red, Blue")
74
  design_prompt = gr.Textbox(label="Design Details", placeholder="E.g., Abstract art, Nature patterns")
75
- x_coord = gr.Slider(label="X Coordinate", minimum=0, maximum=400, step=10, value=100)
76
- y_coord = gr.Slider(label="Y Coordinate", minimum=0, maximum=600, step=10, value=100)
77
- width_slider = gr.Slider(label="Design Width", minimum=100, maximum=500, step=10, value=200)
78
- height_slider = gr.Slider(label="Design Height", minimum=100, maximum=500, step=10, value=300)
79
  generate_button = gr.Button("Generate T-Shirt")
80
  with gr.Column(scale=1):
81
  output_image = gr.Image(label="Final T-Shirt Design")
82
 
83
  generate_button.click(
84
  design_tshirt,
85
- inputs=[color_prompt, design_prompt, x_coord, y_coord, width_slider, height_slider],
86
  outputs=output_image,
87
  )
88
 
89
- interface.launch(debug=True)
 
1
  import gradio as gr
2
+ from PIL import Image, ImageDraw, ImageOps
3
  import requests
4
  from io import BytesIO
5
+ import torch
6
+ from torchvision import transforms
7
+ from torchvision.models.segmentation import deeplabv3_resnet101
8
 
9
  # AI model repo for design generation
10
  repo = "artificialguybr/TshirtDesignRedmond-V2"
11
 
12
+ # Load a pretrained segmentation model for masking
13
+ segmentation_model = deeplabv3_resnet101(pretrained=True).eval()
14
+
15
+ # Transform for input image
16
+ transform = transforms.Compose([
17
+ transforms.Resize((512, 512)),
18
+ transforms.ToTensor(),
19
+ ])
20
+
21
  # Generate plain cloth image with specified color
22
  def generate_cloth(color_prompt):
23
  prompt = f"A plain {color_prompt} colored T-shirt hanging on a plain wall."
 
52
  else:
53
  raise Exception(f"Error generating design: {response.status_code}")
54
 
55
+ # Apply segmentation mask to fit design on T-shirt
56
+ def apply_mask(cloth_image, design_image):
57
+ input_tensor = transform(cloth_image).unsqueeze(0)
58
+ with torch.no_grad():
59
+ output = segmentation_model(input_tensor)['out'][0]
60
+ mask = output.argmax(0).byte().numpy()
61
+ mask_image = Image.fromarray((mask == 15).astype('uint8') * 255) # Class 15 for T-shirt
62
+ mask_resized = mask_image.resize(cloth_image.size)
 
 
 
 
63
 
64
+ # Fit the design inside the T-shirt mask
65
+ design_resized = design_image.resize(cloth_image.size)
66
+ design_masked = Image.composite(design_resized, cloth_image, mask_resized)
67
+ return design_masked
68
 
69
  # Full workflow: Generate cloth, design, and combine them
70
+ def design_tshirt(color_prompt, design_prompt):
71
  cloth_image = generate_cloth(color_prompt)
72
  design_image = generate_design(design_prompt)
73
+ final_image = apply_mask(cloth_image, design_image)
74
  return final_image
75
 
76
  # Gradio interface
77
  with gr.Blocks() as interface:
78
  gr.Markdown("# **AI Cloth Designer**")
79
+ gr.Markdown("Generate custom T-shirts by specifying a color and adding a perfectly fitted design.")
80
 
81
  with gr.Row():
82
  with gr.Column(scale=1):
83
  color_prompt = gr.Textbox(label="Cloth Color", placeholder="E.g., Red, Blue")
84
  design_prompt = gr.Textbox(label="Design Details", placeholder="E.g., Abstract art, Nature patterns")
 
 
 
 
85
  generate_button = gr.Button("Generate T-Shirt")
86
  with gr.Column(scale=1):
87
  output_image = gr.Image(label="Final T-Shirt Design")
88
 
89
  generate_button.click(
90
  design_tshirt,
91
+ inputs=[color_prompt, design_prompt],
92
  outputs=output_image,
93
  )
94
 
95
+ interface.launch(debug=True)