Spaces:
Runtime error
Runtime error
JustSkyDev commited on
Commit ·
7cabae2
1
Parent(s): 998fe30
Update process.py
Browse files- process.py +38 -1
process.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from iglovikov_helper_functions.utils.image_utils import load_rgb, pad, unpad
|
| 2 |
from iglovikov_helper_functions.dl.pytorch.utils import tensor_from_rgb_image
|
| 3 |
from cloths_segmentation.pre_trained_models import create_model
|
|
@@ -29,4 +30,40 @@ def Segment(image: Image.Image, color=[255, 255, 255]):
|
|
| 29 |
|
| 30 |
output_image = Image.fromarray(mask_rgb)
|
| 31 |
|
| 32 |
-
return output_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''
|
| 2 |
from iglovikov_helper_functions.utils.image_utils import load_rgb, pad, unpad
|
| 3 |
from iglovikov_helper_functions.dl.pytorch.utils import tensor_from_rgb_image
|
| 4 |
from cloths_segmentation.pre_trained_models import create_model
|
|
|
|
| 30 |
|
| 31 |
output_image = Image.fromarray(mask_rgb)
|
| 32 |
|
| 33 |
+
return output_image
|
| 34 |
+
'''
|
| 35 |
+
|
| 36 |
+
from PIL import Image
|
| 37 |
+
import numpy as np
|
| 38 |
+
import cv2
|
| 39 |
+
import torch
|
| 40 |
+
import albumentations as albu
|
| 41 |
+
from PIL import ImageOps
|
| 42 |
+
|
| 43 |
+
def Segment(image: Image.Image, color=[255, 255, 255]):
|
| 44 |
+
model = create_model("Unet_2020-10-30")
|
| 45 |
+
model.eval()
|
| 46 |
+
|
| 47 |
+
image = np.array(image)
|
| 48 |
+
transform = albu.Compose([albu.Normalize(p=1)], p=1)
|
| 49 |
+
padded_image, pads = pad(image, factor=32, border=cv2.BORDER_CONSTANT)
|
| 50 |
+
|
| 51 |
+
x = transform(image=padded_image)["image"]
|
| 52 |
+
x = torch.unsqueeze(tensor_from_rgb_image(x), 0)
|
| 53 |
+
|
| 54 |
+
with torch.no_grad():
|
| 55 |
+
prediction = model(x)[0][0]
|
| 56 |
+
|
| 57 |
+
mask = (prediction > 0).cpu().numpy().astype(np.uint8)
|
| 58 |
+
mask = unpad(mask, pads)
|
| 59 |
+
|
| 60 |
+
mask_rgb = np.ones((*mask.shape, 3), dtype=np.uint8) * 255
|
| 61 |
+
mask_rgb[mask == 1] = color
|
| 62 |
+
|
| 63 |
+
# Create an output image with transparent background
|
| 64 |
+
output_image = Image.new("RGBA", image.shape[:2][::-1], (255, 255, 255, 0))
|
| 65 |
+
output_image = ImageOps.invert(output_image) # Invert to black background
|
| 66 |
+
|
| 67 |
+
output_image.paste(Image.fromarray(mask_rgb), (0, 0), Image.fromarray(mask))
|
| 68 |
+
|
| 69 |
+
return output_image
|