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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -28
app.py CHANGED
@@ -1,70 +1,78 @@
1
  import gradio as gr
2
- from PIL import Image
3
  import requests
4
  from io import BytesIO
5
- import numpy as np
6
- import cv2
7
 
8
  # AI model repo for design generation
9
  repo = "artificialguybr/TshirtDesignRedmond-V2"
10
 
 
11
  def generate_cloth(color_prompt):
12
  prompt = f"A plain {color_prompt} colored T-shirt hanging on a plain wall."
13
  api_url = f"https://api-inference.huggingface.co/models/{repo}"
14
  headers = {}
15
- payload = {"inputs": prompt, "parameters": {"num_inference_steps": 30}}
 
 
 
 
 
16
  response = requests.post(api_url, headers=headers, json=payload)
17
  if response.status_code == 200:
18
- return Image.open(BytesIO(response.content)).convert("RGB")
19
  else:
20
  raise Exception(f"Error generating cloth: {response.status_code}")
21
 
 
22
  def generate_design(design_prompt):
23
  prompt = f"A bold {design_prompt} design with vibrant colors, highly detailed."
24
  api_url = f"https://api-inference.huggingface.co/models/{repo}"
25
  headers = {}
26
- payload = {"inputs": prompt, "parameters": {"num_inference_steps": 30}}
 
 
 
 
 
27
  response = requests.post(api_url, headers=headers, json=payload)
28
  if response.status_code == 200:
29
- return Image.open(BytesIO(response.content)).convert("RGBA")
30
  else:
31
  raise Exception(f"Error generating design: {response.status_code}")
32
 
33
- def warp_design_to_tshirt(cloth_image, design_image, points):
34
- cloth_np = np.array(cloth_image)
35
- design_np = np.array(design_image)
36
- if design_np.shape[2] == 4:
37
- alpha_channel = design_np[:, :, 3] / 255.0
38
- design_rgb = design_np[:, :, :3]
39
- else:
40
- alpha_channel = np.ones((design_np.shape[0], design_np.shape[1]))
41
- design_rgb = design_np
42
 
43
- src_points = np.float32([[0, 0], [design_np.shape[1], 0], [design_np.shape[1], design_np.shape[0]], [0, design_np.shape[0]]])
44
- dest_points = np.float32(points)
45
- matrix = cv2.getPerspectiveTransform(src_points, dest_points)
46
- warped_design = cv2.warpPerspective(design_rgb, matrix, (cloth_np.shape[1], cloth_np.shape[0]))
47
- warped_alpha = cv2.warpPerspective(alpha_channel, matrix, (cloth_np.shape[1], cloth_np.shape[0]))
48
 
49
- for c in range(3):
50
- cloth_np[:, :, c] = (warped_alpha * warped_design[:, :, c] + (1 - warped_alpha) * cloth_np[:, :, c]).astype(np.uint8)
 
51
 
52
- return Image.fromarray(cloth_np)
53
 
 
54
  def design_tshirt(color_prompt, design_prompt, x, y, width, height):
55
  cloth_image = generate_cloth(color_prompt)
56
  design_image = generate_design(design_prompt)
57
- points = [[x, y], [x + width, y], [x + width - 20, y + height], [x + 20, y + height]]
58
- final_image = warp_design_to_tshirt(cloth_image, design_image, points)
59
  return final_image
60
 
 
61
  with gr.Blocks() as interface:
62
  gr.Markdown("# **AI Cloth Designer**")
 
 
63
  with gr.Row():
64
  with gr.Column(scale=1):
65
  color_prompt = gr.Textbox(label="Cloth Color", placeholder="E.g., Red, Blue")
66
  design_prompt = gr.Textbox(label="Design Details", placeholder="E.g., Abstract art, Nature patterns")
67
- x_coord = gr.Slider(label="X Coordinate", minimum=0, maximum=600, step=10, value=100)
68
  y_coord = gr.Slider(label="Y Coordinate", minimum=0, maximum=600, step=10, value=100)
69
  width_slider = gr.Slider(label="Design Width", minimum=100, maximum=500, step=10, value=200)
70
  height_slider = gr.Slider(label="Design Height", minimum=100, maximum=500, step=10, value=300)
@@ -78,4 +86,4 @@ with gr.Blocks() as interface:
78
  outputs=output_image,
79
  )
80
 
81
- interface.launch(debug=True)
 
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."
13
  api_url = f"https://api-inference.huggingface.co/models/{repo}"
14
  headers = {}
15
+ payload = {
16
+ "inputs": prompt,
17
+ "parameters": {
18
+ "num_inference_steps": 30,
19
+ },
20
+ }
21
  response = requests.post(api_url, headers=headers, json=payload)
22
  if response.status_code == 200:
23
+ return Image.open(BytesIO(response.content))
24
  else:
25
  raise Exception(f"Error generating cloth: {response.status_code}")
26
 
27
+ # Generate design based on user prompt
28
  def generate_design(design_prompt):
29
  prompt = f"A bold {design_prompt} design with vibrant colors, highly detailed."
30
  api_url = f"https://api-inference.huggingface.co/models/{repo}"
31
  headers = {}
32
+ payload = {
33
+ "inputs": prompt,
34
+ "parameters": {
35
+ "num_inference_steps": 30,
36
+ },
37
+ }
38
  response = requests.post(api_url, headers=headers, json=payload)
39
  if response.status_code == 200:
40
+ return Image.open(BytesIO(response.content))
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)
 
86
  outputs=output_image,
87
  )
88
 
89
+ interface.launch(debug=True)