gaur3009 commited on
Commit
5ed5e4c
·
verified ·
1 Parent(s): b8a08c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -48
app.py CHANGED
@@ -29,51 +29,35 @@ def segment_clothing(image):
29
  mask = cv2.resize(output_predictions, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_NEAREST)
30
  return mask
31
 
32
- def generate_displacement_map(image, mask):
33
- """Generate a displacement map from the clothing region."""
34
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
35
- blurred = cv2.GaussianBlur(gray, (15, 15), 0)
36
- displacement_map = cv2.normalize(blurred, None, 0, 255, cv2.NORM_MINMAX)
37
- displacement_map[mask != 15] = 0 # Apply mask (class 15 corresponds to 'person')
38
- return displacement_map
39
-
40
- def warp_text(image, text_overlay, displacement_map):
41
- """Warp the text overlay based on the displacement map."""
42
- text_overlay_array = np.array(text_overlay)
43
- displacement_map = cv2.GaussianBlur(displacement_map, (15, 15), 0)
44
-
45
- # Create an x, y distortion map
46
- h, w = displacement_map.shape
47
- x, y = np.meshgrid(np.arange(w), np.arange(h))
48
- x_displacement = x + displacement_map / 100.0
49
- y_displacement = y + displacement_map / 100.0
50
-
51
- # Warp text overlay using remap
52
- warped = cv2.remap(
53
- text_overlay_array,
54
- x_displacement.astype(np.float32),
55
- y_displacement.astype(np.float32),
56
- interpolation=cv2.INTER_LINEAR,
57
- borderMode=cv2.BORDER_CONSTANT
58
- )
59
- return Image.fromarray(warped)
60
 
61
  def overlay_text(image, text, font_path, font_size, color, mask, manual_coords=None):
62
- """Overlay text onto the detected clothing region."""
63
  pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).convert("RGBA")
64
-
65
  y_indices, x_indices = np.where(mask == 15)
66
  if len(x_indices) == 0 or len(y_indices) == 0:
67
  return None, "No clothing region detected."
68
-
69
  if manual_coords:
70
  text_x, text_y = manual_coords
71
  else:
72
  x_min, x_max = x_indices.min(), x_indices.max()
73
  y_min, y_max = y_indices.min(), y_indices.max()
74
  text_x, text_y = x_min, y_min
75
-
76
- # Load font and create transparent overlay for text
77
  font = ImageFont.truetype(font_path, font_size)
78
  text_overlay = Image.new("RGBA", pil_image.size, (255, 255, 255, 0))
79
  text_draw = ImageDraw.Draw(text_overlay)
@@ -82,28 +66,17 @@ def overlay_text(image, text, font_path, font_size, color, mask, manual_coords=N
82
 
83
  def process_image(image, text, font_size, color, font_path, placement_x, placement_y):
84
  try:
85
- # Segment clothing using DeepLabV3
86
  mask = segment_clothing(image)
87
  if mask.sum() == 0:
88
  return "No clothing detected. Try another image."
89
-
90
- # Generate displacement map
91
- displacement_map = generate_displacement_map(image, mask)
92
-
93
- # Overlay text with manual coordinates (if provided)
94
  manual_coords = (placement_x, placement_y)
95
  text_overlay, error = overlay_text(image, text, font_path, font_size, color, mask, manual_coords)
96
  if error:
97
  return error
98
-
99
- # Warp text using displacement map
100
- warped_text = warp_text(image, text_overlay, displacement_map)
101
-
102
- # Blend warped text with the original image
103
- pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).convert("RGBA")
104
- final_image = Image.alpha_composite(pil_image, warped_text).convert("RGB")
105
-
106
- return final_image
107
  except Exception as e:
108
  return f"Error: {str(e)}"
109
 
 
29
  mask = cv2.resize(output_predictions, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_NEAREST)
30
  return mask
31
 
32
+ def apply_displacement_map(text_img, clothing_img, strength=20):
33
+ gray = cv2.cvtColor(clothing_img, cv2.COLOR_BGR2GRAY)
34
+ grad_x = cv2.Sobel(gray, cv2.CV_32F, 1, 0, ksize=5)
35
+ grad_y = cv2.Sobel(gray, cv2.CV_32F, 0, 1, ksize=5)
36
+ grad_x = cv2.normalize(grad_x, None, 0, 1, cv2.NORM_MINMAX)
37
+ grad_y = cv2.normalize(grad_y, None, 0, 1, cv2.NORM_MINMAX)
38
+ displacement_map = np.zeros_like(clothing_img, dtype=np.float32)
39
+ displacement_map[:, :, 0] = grad_x * strength
40
+ displacement_map[:, :, 1] = grad_y * strength
41
+ text_warped = cv2.remap(text_img, displacement_map[:, :, 0].astype(np.float32), displacement_map[:, :, 1].astype(np.float32), interpolation=cv2.INTER_LINEAR)
42
+ return text_warped
43
+
44
+ def blend_images(foreground, background, alpha=0.7):
45
+ foreground = foreground.astype(float)
46
+ background = background.astype(float)
47
+ blended = cv2.addWeighted(foreground, alpha, background, 1 - alpha, 0)
48
+ return blended.astype(np.uint8)
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  def overlay_text(image, text, font_path, font_size, color, mask, manual_coords=None):
 
51
  pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).convert("RGBA")
 
52
  y_indices, x_indices = np.where(mask == 15)
53
  if len(x_indices) == 0 or len(y_indices) == 0:
54
  return None, "No clothing region detected."
 
55
  if manual_coords:
56
  text_x, text_y = manual_coords
57
  else:
58
  x_min, x_max = x_indices.min(), x_indices.max()
59
  y_min, y_max = y_indices.min(), y_indices.max()
60
  text_x, text_y = x_min, y_min
 
 
61
  font = ImageFont.truetype(font_path, font_size)
62
  text_overlay = Image.new("RGBA", pil_image.size, (255, 255, 255, 0))
63
  text_draw = ImageDraw.Draw(text_overlay)
 
66
 
67
  def process_image(image, text, font_size, color, font_path, placement_x, placement_y):
68
  try:
 
69
  mask = segment_clothing(image)
70
  if mask.sum() == 0:
71
  return "No clothing detected. Try another image."
 
 
 
 
 
72
  manual_coords = (placement_x, placement_y)
73
  text_overlay, error = overlay_text(image, text, font_path, font_size, color, mask, manual_coords)
74
  if error:
75
  return error
76
+ text_img = np.array(text_overlay.convert("RGB"))
77
+ text_warped = apply_displacement_map(text_img, image)
78
+ blended_image = blend_images(text_warped, image)
79
+ return Image.fromarray(blended_image)
 
 
 
 
 
80
  except Exception as e:
81
  return f"Error: {str(e)}"
82