gaur3009 commited on
Commit
4b2925c
·
verified ·
1 Parent(s): 5a421d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -115
app.py CHANGED
@@ -1,118 +1,48 @@
1
- import gradio as gr
2
- from PIL import Image, ImageDraw, ImageFilter
3
  import torch
4
- from diffusers import StableDiffusionXLImg2ImgPipeline, StableDiffusionXLInpaintPipeline
5
- import torchvision.transforms as T
6
- from torchvision import models
7
- import numpy as np
8
  import cv2
 
9
 
10
- # Load the SDXL pipelines
11
- device = "cuda" if torch.cuda.is_available() else "cpu"
12
- pipe_tshirt = StableDiffusionXLImg2ImgPipeline.from_pretrained(
13
- "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float32
14
- ).to(device)
15
-
16
- pipe_design = StableDiffusionXLImg2ImgPipeline.from_pretrained(
17
- "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float32
18
- ).to(device)
19
-
20
- # Generate T-shirt image
21
- def generate_cloth(color_prompt):
22
- prompt = f"A plain {color_prompt} T-shirt hanging on a plain wall, realistic, high-quality photo."
23
- image = pipe_tshirt(
24
- prompt=prompt, strength=0.8, guidance_scale=7.5, num_inference_steps=30
25
- ).images[0]
26
- return image
27
-
28
- # Generate design image
29
- def generate_design(design_prompt):
30
- prompt = f"A bold {design_prompt} design with vibrant colors, highly detailed."
31
- image = pipe_design(
32
- prompt=prompt, strength=0.8, guidance_scale=7.5, num_inference_steps=30
33
- ).images[0]
34
- return image.convert("RGBA")
35
-
36
- # Load pretrained DeepLabV3 model for T-shirt segmentation
37
- segmentation_model = models.segmentation.deeplabv3_resnet101(pretrained=True).eval()
38
-
39
- # Apply segmentation to extract T-shirt mask
40
- def get_tshirt_mask(image):
41
- image = image.convert("RGB") # Ensure 3 channels
42
- preprocess = T.Compose([
43
- T.Resize((520, 520)), # Resize to avoid distortion
44
- T.ToTensor(),
45
- T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
46
- ])
47
- input_tensor = preprocess(image).unsqueeze(0)
48
- with torch.no_grad():
49
- output = segmentation_model(input_tensor)["out"][0]
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()