Update cropped image functionality and revert back inference
Browse files
app.py
CHANGED
|
@@ -204,7 +204,7 @@ hypar["model"] = ISNetDIS()
|
|
| 204 |
# Build Model
|
| 205 |
net = build_model(hypar, device)
|
| 206 |
|
| 207 |
-
|
| 208 |
def inference(image):
|
| 209 |
image_path = image
|
| 210 |
|
|
@@ -213,10 +213,14 @@ def inference(image):
|
|
| 213 |
|
| 214 |
# Process the original mask with smoothing and denoising
|
| 215 |
processed_mask = smooth_and_denoise(original_mask)
|
|
|
|
| 216 |
|
| 217 |
# Convert processed mask to PIL image
|
| 218 |
pil_processed_mask = Image.fromarray((processed_mask * 255).astype(np.uint8)).convert('L')
|
| 219 |
pil_original_mask = Image.fromarray(original_mask).convert('L')
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
im_rgb = Image.open(image).convert("RGB")
|
| 222 |
im_dark = Image.new('RGB', im_rgb.size, (0, 0, 0))
|
|
@@ -227,43 +231,7 @@ def inference(image):
|
|
| 227 |
im_rgba.putalpha(pil_original_mask)
|
| 228 |
im_dark.putalpha(pil_processed_mask)
|
| 229 |
|
| 230 |
-
return [cropped_signature_image, processed_mask,
|
| 231 |
-
'''
|
| 232 |
-
|
| 233 |
-
def inference(image):
|
| 234 |
-
image_path = image
|
| 235 |
-
|
| 236 |
-
# Load and predict the mask for the original image
|
| 237 |
-
image_tensor, orig_size = load_image(image_path, hypar)
|
| 238 |
-
original_mask = predict(net, image_tensor, orig_size, hypar, device)
|
| 239 |
-
|
| 240 |
-
# Crop the signature from the original image
|
| 241 |
-
cropped_signature_image = crop_signature(image_path, original_mask, 64)
|
| 242 |
-
|
| 243 |
-
# Resize the cropped image to the expected input size for the model
|
| 244 |
-
resized_cropped_image = cropped_signature_image.resize(hypar["input_size"], Image.BILINEAR)
|
| 245 |
-
|
| 246 |
-
# Convert the resized cropped image to a tensor for model input
|
| 247 |
-
resized_cropped_image_tensor, _ = load_image(resized_cropped_image, hypar)
|
| 248 |
-
|
| 249 |
-
# Predict the mask for the resized cropped signature image
|
| 250 |
-
cropped_mask = predict(net, resized_cropped_image_tensor, orig_size, hypar, device)
|
| 251 |
-
|
| 252 |
-
# Resize the mask to match the resized cropped signature image dimensions
|
| 253 |
-
pil_cropped_mask = Image.fromarray(cropped_mask).convert('L')
|
| 254 |
-
pil_cropped_mask = pil_cropped_mask.resize(resized_cropped_image.size, Image.BILINEAR)
|
| 255 |
-
|
| 256 |
-
# Apply the refined mask to the resized cropped signature image
|
| 257 |
-
im_rgba_cropped = resized_cropped_image.copy()
|
| 258 |
-
im_rgba_cropped.putalpha(pil_cropped_mask)
|
| 259 |
-
|
| 260 |
-
# Create a dark image with the same size as the resized cropped signature image
|
| 261 |
-
im_dark_cropped = Image.new('RGB', im_rgba_cropped.size, (0, 0, 0))
|
| 262 |
-
im_dark_cropped.putalpha(pil_cropped_mask)
|
| 263 |
-
|
| 264 |
-
# The outputs are now based on the resized cropped image
|
| 265 |
-
return [im_rgba_cropped, pil_cropped_mask, im_dark_cropped]
|
| 266 |
-
|
| 267 |
|
| 268 |
title = "Mysign.id - Signature Background removal based on DIS"
|
| 269 |
description = "ML Model based on ECCV2022/dis-background-removal specifically made for removing background from signatures."
|
|
|
|
| 204 |
# Build Model
|
| 205 |
net = build_model(hypar, device)
|
| 206 |
|
| 207 |
+
|
| 208 |
def inference(image):
|
| 209 |
image_path = image
|
| 210 |
|
|
|
|
| 213 |
|
| 214 |
# Process the original mask with smoothing and denoising
|
| 215 |
processed_mask = smooth_and_denoise(original_mask)
|
| 216 |
+
cropped_processed_mask = crop_signature(image_path, processed_mask, 64)
|
| 217 |
|
| 218 |
# Convert processed mask to PIL image
|
| 219 |
pil_processed_mask = Image.fromarray((processed_mask * 255).astype(np.uint8)).convert('L')
|
| 220 |
pil_original_mask = Image.fromarray(original_mask).convert('L')
|
| 221 |
+
|
| 222 |
+
im_dark_cropped = Image.new('RGB', cropped_signature_image.size, (0, 0, 0))
|
| 223 |
+
im_dark_cropped.putalpha(cropped_processed_mask)
|
| 224 |
|
| 225 |
im_rgb = Image.open(image).convert("RGB")
|
| 226 |
im_dark = Image.new('RGB', im_rgb.size, (0, 0, 0))
|
|
|
|
| 231 |
im_rgba.putalpha(pil_original_mask)
|
| 232 |
im_dark.putalpha(pil_processed_mask)
|
| 233 |
|
| 234 |
+
return [cropped_signature_image, processed_mask, im_dark_cropped]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
title = "Mysign.id - Signature Background removal based on DIS"
|
| 237 |
description = "ML Model based on ECCV2022/dis-background-removal specifically made for removing background from signatures."
|