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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -47
app.py CHANGED
@@ -7,85 +7,70 @@ 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
 
@@ -94,15 +79,13 @@ 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
 
7
  from skimage.filters import sobel
8
  from skimage.restoration import denoise_tv_chambolle
9
  from scipy.interpolate import Rbf
 
10
 
11
 
12
+ # Function to estimate a normal map from cloth texture
13
  def estimate_normal_map(image):
14
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
15
  sobel_x = sobel(gray)
16
  sobel_y = sobel(gray)
17
+
18
  normal_map = np.stack([sobel_x, sobel_y, np.ones_like(sobel_x)], axis=-1)
19
+ normal_map /= np.linalg.norm(normal_map, axis=-1, keepdims=True)
20
+
21
  return (normal_map * 255).astype(np.uint8)
22
 
23
 
24
  # Function to apply Thin Plate Spline (TPS) warping
25
+ def apply_tps_warping(design, normal_map):
26
+ h, w = design.shape[:2]
27
  x, y = np.meshgrid(np.arange(w), np.arange(h))
28
 
29
+ # Generate warp offsets from normal map
30
  control_x = x + (normal_map[:, :, 0] - 128) * 0.5
31
  control_y = y + (normal_map[:, :, 1] - 128) * 0.5
32
 
33
+ # Apply Radial Basis Function (RBF) interpolation
34
  rbf_x = Rbf(x.flatten(), y.flatten(), control_x.flatten(), function='thin_plate')
35
  rbf_y = Rbf(x.flatten(), y.flatten(), control_y.flatten(), function='thin_plate')
36
 
37
  warped_x = rbf_x(x, y).astype(np.float32)
38
  warped_y = rbf_y(x, y).astype(np.float32)
39
 
40
+ # Warp the design
41
+ warped_design = cv2.remap(design, warped_x, warped_y, interpolation=cv2.INTER_LINEAR)
 
 
42
 
43
+ return warped_design
44
 
45
+
46
+ # Function to blend design onto the cloth using Poisson Editing
47
+ def blend_design_cloth(cloth, design, x=50, y=50):
48
  cloth_bgr = np.array(cloth)
49
+ design_bgr = np.array(design)
50
 
51
  normal_map = estimate_normal_map(cloth_bgr)
52
 
53
+ # Resize design to fit the center of the cloth
54
+ design_resized = cv2.resize(design_bgr, (cloth_bgr.shape[1] // 2, cloth_bgr.shape[0] // 5))
55
 
56
  # Convert to grayscale and create a mask
57
+ design_gray = cv2.cvtColor(design_resized, cv2.COLOR_BGR2GRAY)
58
+ _, mask = cv2.threshold(design_gray, 1, 255, cv2.THRESH_BINARY)
59
 
60
+ # Warp design using normal map
61
+ warped_design = apply_tps_warping(design_resized, normal_map)
62
 
63
+ # Blend using Poisson seamless cloning
64
+ center = (x + design_resized.shape[1] // 2, y + design_resized.shape[0] // 2)
65
+ blended = cv2.seamlessClone(warped_design, cloth_bgr, mask, center, cv2.MIXED_CLONE)
66
 
67
  return Image.fromarray(blended)
68
 
69
 
70
  # Gradio function
71
+ def process_image(cloth_image, design_image, x=50, y=50):
72
+ # Blend design onto cloth
73
+ result = blend_design_cloth(cloth_image, design_image, x, y)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  return result
75
 
76
 
 
79
  fn=process_image,
80
  inputs=[
81
  gr.Image(type="pil", label="Upload Cloth Image"),
82
+ gr.Image(type="pil", label="Upload Design"),
 
 
83
  gr.Slider(0, 1000, step=10, label="X Coordinate", value=50),
84
  gr.Slider(0, 1000, step=10, label="Y Coordinate", value=50),
85
  ],
86
  outputs=gr.Image(type="pil", label="Blended Output"),
87
+ title="Advanced Cloth Design Blending",
88
+ description="Upload a cloth image and a design to blend them naturally using advanced warping & Poisson blending.",
89
  )
90
 
91
  # Launch the app