itsyogesh commited on
Commit
91be27b
·
verified ·
1 Parent(s): 802830a

Update crop function

Browse files
Files changed (1) hide show
  1. app.py +49 -24
app.py CHANGED
@@ -70,31 +70,42 @@ def build_model(hypar,device):
70
  return net
71
 
72
 
73
- def crop_to_signature(mask, padding=32):
74
  """
75
- Crop the signature area based on the mask and add padding.
 
 
76
  :param mask: The binary mask of the signature.
77
- :param padding: Padding around the cropped signature.
78
- :return: Cropped mask of the signature with padding.
79
  """
80
- contours, _ = cv2.findContours(
81
- mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
 
 
 
 
 
 
 
 
82
  if contours:
83
  # Assume the largest contour is the signature
84
  x, y, w, h = cv2.boundingRect(max(contours, key=cv2.contourArea))
85
 
86
  # Add padding to the bounding box
87
- x = max(x - padding, 0)
88
- y = max(y - padding, 0)
89
- w = min(w + 2 * padding, mask.shape[1] - x)
90
- h = min(h + 2 * padding, mask.shape[0] - y)
91
-
92
- # Crop the mask
93
- cropped_mask = mask[y:y+h, x:x+w]
94
- return cropped_mask
95
- else:
96
- # Return the original mask if no contours are found
97
- return mask
 
98
 
99
 
100
  def smooth_and_denoise(mask):
@@ -107,11 +118,11 @@ def smooth_and_denoise(mask):
107
  smoothed_mask = cv2.GaussianBlur(mask, (5, 5), 0)
108
 
109
  # Estimate noise standard deviation from the image
110
- sigma_est = np.mean(estimate_sigma(smoothed_mask, channel_axis=None))
111
 
112
  # Apply Non-Local Means Denoising
113
  denoised_mask = denoise_nl_means(smoothed_mask, h=1.15 * sigma_est, fast_mode=True,
114
- patch_size=5, patch_distance=3, channel_axis=None)
115
  return denoised_mask
116
 
117
 
@@ -166,28 +177,42 @@ hypar["model"] = ISNetDIS()
166
  # Build Model
167
  net = build_model(hypar, device)
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
170
  def inference(image):
171
  image_path = image
172
 
173
  image_tensor, orig_size = load_image(image_path, hypar)
174
  original_mask = predict(net, image_tensor, orig_size, hypar, device)
175
-
176
  # Process the original mask with smoothing and denoising
177
  processed_mask = smooth_and_denoise(original_mask)
178
 
179
  # Convert processed mask to PIL image
180
- pil_processed_mask = Image.fromarray(processed_mask).convert('L')
 
181
  im_rgb = Image.open(image).convert("RGB")
182
-
183
  im_dark = Image.new('RGB', im_rgb.size, (0, 0, 0))
 
184
 
185
  # Apply processed mask to images
186
  im_rgba = im_rgb.copy()
187
- im_rgba.putalpha(pil_processed_mask)
188
  im_dark.putalpha(pil_processed_mask)
189
 
190
- return [im_rgba, pil_processed_mask, im_dark]
191
 
192
 
193
  title = "Mysign.id - Signature Background removal based on DIS"
 
70
  return net
71
 
72
 
73
+ def crop_signature(original_image_path, mask, padding=32):
74
  """
75
+ Crop the signature from the original image using the provided mask.
76
+
77
+ :param original_image_path: The file path of the original image.
78
  :param mask: The binary mask of the signature.
79
+ :param padding: Padding to add around the bounding box of the signature.
80
+ :return: Cropped image containing the signature.
81
  """
82
+ # Convert the mask to a binary image
83
+ _, binary_mask = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)
84
+
85
+ # Find contours from the binary mask
86
+ contours, _ = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
87
+
88
+ # Open the original image
89
+ original_image = Image.open(original_image_path).convert("RGB")
90
+
91
+ # If contours are found, proceed to crop
92
  if contours:
93
  # Assume the largest contour is the signature
94
  x, y, w, h = cv2.boundingRect(max(contours, key=cv2.contourArea))
95
 
96
  # Add padding to the bounding box
97
+ x_padded = max(x - padding, 0)
98
+ y_padded = max(y - padding, 0)
99
+ w_padded = min(w + 2 * padding, original_image.width - x_padded)
100
+ h_padded = min(h + 2 * padding, original_image.height - y_padded)
101
+
102
+ # Crop the original image and mask using the bounding box with padding
103
+ cropped_image = original_image.crop((x_padded, y_padded, x_padded + w_padded, y_padded + h_padded))
104
+
105
+ return cropped_image
106
+
107
+ # If no contours are found, return the original image
108
+ return original_image
109
 
110
 
111
  def smooth_and_denoise(mask):
 
118
  smoothed_mask = cv2.GaussianBlur(mask, (5, 5), 0)
119
 
120
  # Estimate noise standard deviation from the image
121
+ sigma_est = np.mean(estimate_sigma(smoothed_mask, channel_axis=-1))
122
 
123
  # Apply Non-Local Means Denoising
124
  denoised_mask = denoise_nl_means(smoothed_mask, h=1.15 * sigma_est, fast_mode=True,
125
+ patch_size=5, patch_distance=3, channel_axis=-1)
126
  return denoised_mask
127
 
128
 
 
177
  # Build Model
178
  net = build_model(hypar, device)
179
 
180
+ def inference(image):
181
+ image_path = image
182
+
183
+ image_tensor, orig_size = load_image(image_path, hypar)
184
+ mask = predict(net, image_tensor, orig_size, hypar, device)
185
+
186
+ cropped_mask = crop_to_signature(mask)
187
+ processed_mask = smooth_and_denoise(cropped_mask)
188
+
189
+ # Convert to PIL image for output
190
+ pil_mask = Image.fromarray((processed_mask * 255).astype(np.uint8)).convert('L')
191
+ return pil_mask
192
+
193
 
194
  def inference(image):
195
  image_path = image
196
 
197
  image_tensor, orig_size = load_image(image_path, hypar)
198
  original_mask = predict(net, image_tensor, orig_size, hypar, device)
199
+
200
  # Process the original mask with smoothing and denoising
201
  processed_mask = smooth_and_denoise(original_mask)
202
 
203
  # Convert processed mask to PIL image
204
+ pil_processed_mask = Image.fromarray((processed_mask * 255).astype(np.uint8)).convert('L')
205
+
206
  im_rgb = Image.open(image).convert("RGB")
 
207
  im_dark = Image.new('RGB', im_rgb.size, (0, 0, 0))
208
+ cropped_signature_image = crop_signature(image_path, mask)
209
 
210
  # Apply processed mask to images
211
  im_rgba = im_rgb.copy()
212
+ im_rgba.putalpha(original_mask)
213
  im_dark.putalpha(pil_processed_mask)
214
 
215
+ return [cropped_signature_image, processed_mask, im_dark]
216
 
217
 
218
  title = "Mysign.id - Signature Background removal based on DIS"