gaur3009 commited on
Commit
b938ae3
·
verified ·
1 Parent(s): 109a86d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -18
app.py CHANGED
@@ -31,21 +31,22 @@ def warp_design(cloth_img, design_img):
31
  cloth_np = np.array(cloth_img)
32
  design_np = np.array(design_img)
33
 
34
- # Estimate depth for fold detection
35
- depth_map = estimate_depth(cloth_img)
36
- depth_map = cv2.resize(depth_map, (cloth_np.shape[1], cloth_np.shape[0])) # Ensure matching dimensions
37
-
38
- # Resize design to fit centrally on clothing
39
- design_h, design_w = cloth_np.shape[:2]
40
- center_x, center_y = design_w // 2, design_h // 2
41
- resized_design = cv2.resize(design_np, (design_w // 2, design_h // 2))
42
 
43
- # Place design at the center of the clothing image
44
- y_offset = center_y - resized_design.shape[0] // 2
45
- x_offset = center_x - resized_design.shape[1] // 2
 
 
46
 
47
- blended_design = cloth_np.copy()
48
- blended_design[y_offset:y_offset + resized_design.shape[0], x_offset:x_offset + resized_design.shape[1]] = resized_design
 
49
 
50
  # Generate displacement map based on depth
51
  displacement_x = cv2.Sobel(depth_map, cv2.CV_32F, 1, 0, ksize=5)
@@ -56,14 +57,13 @@ def warp_design(cloth_img, design_img):
56
  displacement_y = cv2.normalize(displacement_y, None, -5, 5, cv2.NORM_MINMAX)
57
 
58
  # Warp design using displacement map
59
- h, w, _ = cloth_np.shape
60
  map_x, map_y = np.meshgrid(np.arange(w), np.arange(h))
61
  map_x = np.clip(np.float32(map_x + displacement_x), 0, w - 1)
62
  map_y = np.clip(np.float32(map_y + displacement_y), 0, h - 1)
63
- warped_design = cv2.remap(blended_design, map_x, map_y, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT)
64
 
65
- # Blend images
66
- blended = cv2.addWeighted(cloth_np, 0.6, warped_design, 0.4, 0)
67
  return Image.fromarray(blended)
68
 
69
  def main(cloth, design):
@@ -74,7 +74,7 @@ iface = gr.Interface(
74
  inputs=[gr.Image(type="pil"), gr.Image(type="pil")],
75
  outputs=gr.Image(type="pil"),
76
  title="AI Cloth Design Warping",
77
- description="Upload a clothing image and a design to blend it naturally at the center, considering fabric folds."
78
  )
79
 
80
  if __name__ == "__main__":
 
31
  cloth_np = np.array(cloth_img)
32
  design_np = np.array(design_img)
33
 
34
+ # Ensure design fits within the center of the clothing
35
+ h, w, _ = cloth_np.shape
36
+ dh, dw, _ = design_np.shape
37
+ scale_factor = min(w / dw, h / dh) * 0.8 # Scale to 80% of the available space
38
+ new_w, new_h = int(dw * scale_factor), int(dh * scale_factor)
39
+ design_np = cv2.resize(design_np, (new_w, new_h))
 
 
40
 
41
+ # Create a blank canvas and paste the resized design at the center
42
+ design_canvas = np.zeros_like(cloth_np)
43
+ x_offset = (w - new_w) // 2
44
+ y_offset = (h - new_h) // 2
45
+ design_canvas[y_offset:y_offset+new_h, x_offset:x_offset+new_w] = design_np
46
 
47
+ # Estimate depth for fold detection
48
+ depth_map = estimate_depth(cloth_img)
49
+ depth_map = cv2.resize(depth_map, (w, h))
50
 
51
  # Generate displacement map based on depth
52
  displacement_x = cv2.Sobel(depth_map, cv2.CV_32F, 1, 0, ksize=5)
 
57
  displacement_y = cv2.normalize(displacement_y, None, -5, 5, cv2.NORM_MINMAX)
58
 
59
  # Warp design using displacement map
 
60
  map_x, map_y = np.meshgrid(np.arange(w), np.arange(h))
61
  map_x = np.clip(np.float32(map_x + displacement_x), 0, w - 1)
62
  map_y = np.clip(np.float32(map_y + displacement_y), 0, h - 1)
63
+ warped_design = cv2.remap(design_canvas, map_x, map_y, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT)
64
 
65
+ # Blend images without excessive transparency
66
+ blended = cv2.addWeighted(cloth_np, 0.7, warped_design, 0.7, 0)
67
  return Image.fromarray(blended)
68
 
69
  def main(cloth, design):
 
74
  inputs=[gr.Image(type="pil"), gr.Image(type="pil")],
75
  outputs=gr.Image(type="pil"),
76
  title="AI Cloth Design Warping",
77
+ description="Upload a clothing image and a design to blend it naturally, ensuring it stays centered and follows fabric folds."
78
  )
79
 
80
  if __name__ == "__main__":