Spaces:
Runtime error
Runtime error
JustSkyDev commited on
Commit ·
c29048b
1
Parent(s): 39ba66a
Create process.py
Browse files- process.py +30 -0
process.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import cv2
|
| 7 |
+
import torch
|
| 8 |
+
import albumentations as albu
|
| 9 |
+
|
| 10 |
+
def Segment(image: Image.Image, color=[255, 255, 255]):
|
| 11 |
+
model = create_model("Unet_2020-10-30")
|
| 12 |
+
model.eval()
|
| 13 |
+
|
| 14 |
+
image = np.array(image)
|
| 15 |
+
transform = albu.Compose([albu.Normalize(p=1)], p=1)
|
| 16 |
+
padded_image, pads = pad(image, factor=32, border=cv2.BORDER_CONSTANT)
|
| 17 |
+
|
| 18 |
+
x = transform(image=padded_image)["image"]
|
| 19 |
+
x = torch.unsqueeze(tensor_from_rgb_image(x), 0)
|
| 20 |
+
|
| 21 |
+
with torch.no_grad():
|
| 22 |
+
prediction = model(x)[0][0]
|
| 23 |
+
|
| 24 |
+
mask = (prediction > 0).cpu().numpy().astype(np.uint8)
|
| 25 |
+
mask = unpad(mask, pads)
|
| 26 |
+
|
| 27 |
+
mask_rgb = np.ones((*mask.shape, 3), dtype=np.uint8) * 255
|
| 28 |
+
mask_rgb[mask == 1] = color
|
| 29 |
+
|
| 30 |
+
return mask_rgb
|