gaur3009 commited on
Commit
21fbf68
·
verified ·
1 Parent(s): c9b0cde

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -36
app.py CHANGED
@@ -1,48 +1,110 @@
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()
 
 
 
 
 
1
  import cv2
2
  import numpy as np
3
+ from PIL import Image, ImageDraw, ImageFont
4
+ import gradio as gr
5
+ import torch
6
+ import torchvision.transforms as transforms
7
+ from skimage.filters import sobel
8
+ from skimage.restoration import denoise_tv_chambolle
9
+ from scipy.interpolate import Rbf
10
+ from scipy.ndimage import map_coordinates
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ # Function to estimate a normal map from the cloth texture
14
+ def estimate_normal_map(image):
15
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
16
+ sobel_x = sobel(gray)
17
+ sobel_y = sobel(gray)
18
+ normal_map = np.stack([sobel_x, sobel_y, np.ones_like(sobel_x)], axis=-1)
19
+ normal_map = normal_map / np.linalg.norm(normal_map, axis=-1, keepdims=True)
20
+ return (normal_map * 255).astype(np.uint8)
21
+
22
+
23
+ # Function to apply Thin Plate Spline (TPS) warping
24
+ def apply_tps_warping(text_image, normal_map):
25
+ h, w = text_image.shape[:2]
26
+ x, y = np.meshgrid(np.arange(w), np.arange(h))
27
+
28
+ # Generate control points from the normal map
29
+ control_x = x + (normal_map[:, :, 0] - 128) * 0.5
30
+ control_y = y + (normal_map[:, :, 1] - 128) * 0.5
31
+
32
+ # Interpolate using Radial Basis Function (RBF)
33
+ rbf_x = Rbf(x.flatten(), y.flatten(), control_x.flatten(), function='thin_plate')
34
+ rbf_y = Rbf(x.flatten(), y.flatten(), control_y.flatten(), function='thin_plate')
35
+
36
+ warped_x = rbf_x(x, y).astype(np.float32)
37
+ warped_y = rbf_y(x, y).astype(np.float32)
38
+
39
+ # Apply warping
40
+ warped_text = cv2.remap(text_image, warped_x, warped_y, interpolation=cv2.INTER_LINEAR)
41
+
42
+ return warped_text
43
+
44
+
45
+ # Function to blend text onto cloth using Poisson editing
46
+ def blend_text_cloth(cloth, text, x=50, y=50):
47
+ cloth_bgr = np.array(cloth)
48
+ text_bgr = np.array(text)
49
+
50
+ normal_map = estimate_normal_map(cloth_bgr)
51
+
52
+ # Resize text to fit on cloth
53
+ text_resized = cv2.resize(text_bgr, (cloth_bgr.shape[1] // 2, cloth_bgr.shape[0] // 5))
54
+
55
+ # Convert to grayscale and create a mask
56
+ text_gray = cv2.cvtColor(text_resized, cv2.COLOR_BGR2GRAY)
57
+ _, mask = cv2.threshold(text_gray, 1, 255, cv2.THRESH_BINARY)
58
+
59
+ # Warp text using normal map
60
+ warped_text = apply_tps_warping(text_resized, normal_map)
61
+
62
+ # Blend the text using Poisson editing
63
+ center = (x + text_resized.shape[1] // 2, y + text_resized.shape[0] // 2)
64
+ blended = cv2.seamlessClone(warped_text, cloth_bgr, mask, center, cv2.MIXED_CLONE)
65
+
66
+ return Image.fromarray(blended)
67
+
68
+
69
+ # Gradio function
70
+ def process_image(cloth_image, text, font_size=32, font_color=(255, 0, 0), x=50, y=50):
71
+ # Convert font color input
72
+ font_color = tuple(map(int, font_color.strip("()").split(",")))
73
+
74
+ # Create a blank image with text
75
+ text_img = Image.new('RGB', (400, 200), (0, 0, 0, 0))
76
+ draw = ImageDraw.Draw(text_img)
77
+
78
+ try:
79
+ font = ImageFont.truetype("arial.ttf", font_size)
80
+ except:
81
+ font = ImageFont.load_default()
82
+
83
+ draw.text((50, 50), text, font=font, fill=font_color)
84
+ text_img = np.array(text_img)
85
+
86
+ # Blend text onto cloth
87
+ result = blend_text_cloth(cloth_image, text_img, x, y)
88
+
89
  return result
90
 
91
+
92
+ # Gradio Interface
93
  interface = gr.Interface(
94
  fn=process_image,
95
+ inputs=[
96
+ gr.Image(type="pil", label="Upload Cloth Image"),
97
+ gr.Textbox(label="Text to Blend", value="Sample Text"),
98
+ gr.Slider(10, 100, step=2, label="Font Size", value=32),
99
+ gr.Textbox(label="Font Color (RGB)", value="(255, 0, 0)"),
100
+ gr.Slider(0, 1000, step=10, label="X Coordinate", value=50),
101
+ gr.Slider(0, 1000, step=10, label="Y Coordinate", value=50),
102
+ ],
103
  outputs=gr.Image(type="pil", label="Blended Output"),
104
+ title="Advanced Text-Cloth Blending",
105
+ description="Upload a cloth image and blend text naturally using advanced warping & blending techniques.",
106
  )
107
 
108
+ # Launch the app
109
  if __name__ == "__main__":
110
+ interface.launch(server_name="0.0.0.0", server_port=7860)