Spaces:
Runtime error
Runtime error
Commit ·
a601816
1
Parent(s): 8c7592a
add negative padding
Browse files- processing/mask.py +5 -1
- requirements.txt +1 -0
processing/mask.py
CHANGED
|
@@ -3,6 +3,7 @@ import numpy as np
|
|
| 3 |
import torch
|
| 4 |
from PIL import Image, ImageFilter
|
| 5 |
from rembg import remove
|
|
|
|
| 6 |
from torchvision.transforms import Compose
|
| 7 |
|
| 8 |
from DPT.dpt.transforms import PrepareForNet, NormalizeImage, Resize
|
|
@@ -13,7 +14,10 @@ def create_mask(image, blur=0, padding=0):
|
|
| 13 |
rm_bg = Image.fromarray((rm_bg * 255).astype(np.uint8))
|
| 14 |
rm_bg = rm_bg.resize(image.size, resample=Image.BILINEAR)
|
| 15 |
# Create a padding of 5 pixels around the object in the mask
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Convert mask back to uint8 for PIL compatibility
|
| 19 |
pil_mask = Image.fromarray((padded_mask * 255).astype(np.uint8))
|
|
|
|
| 3 |
import torch
|
| 4 |
from PIL import Image, ImageFilter
|
| 5 |
from rembg import remove
|
| 6 |
+
from scipy.ndimage import binary_dilation
|
| 7 |
from torchvision.transforms import Compose
|
| 8 |
|
| 9 |
from DPT.dpt.transforms import PrepareForNet, NormalizeImage, Resize
|
|
|
|
| 14 |
rm_bg = Image.fromarray((rm_bg * 255).astype(np.uint8))
|
| 15 |
rm_bg = rm_bg.resize(image.size, resample=Image.BILINEAR)
|
| 16 |
# Create a padding of 5 pixels around the object in the mask
|
| 17 |
+
if padding > 0:
|
| 18 |
+
padded_mask = np.pad(rm_bg, pad_width=padding, mode='constant', constant_values=0)
|
| 19 |
+
else:
|
| 20 |
+
padded_mask = binary_dilation(np.array(rm_bg), iterations=-padding)
|
| 21 |
|
| 22 |
# Convert mask back to uint8 for PIL compatibility
|
| 23 |
pil_mask = Image.fromarray((padded_mask * 255).astype(np.uint8))
|
requirements.txt
CHANGED
|
@@ -19,4 +19,5 @@ matplotlib
|
|
| 19 |
setuptools
|
| 20 |
safetensors
|
| 21 |
huggingface-hub
|
|
|
|
| 22 |
ultralytics~=8.2.48
|
|
|
|
| 19 |
setuptools
|
| 20 |
safetensors
|
| 21 |
huggingface-hub
|
| 22 |
+
scipy
|
| 23 |
ultralytics~=8.2.48
|