JustSkyDev commited on
Commit
d47850c
·
1 Parent(s): b58ed48

Update process.py

Browse files
Files changed (1) hide show
  1. process.py +8 -45
process.py CHANGED
@@ -1,4 +1,3 @@
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
@@ -8,7 +7,7 @@ import cv2
8
  import torch
9
  import albumentations as albu
10
 
11
- def Segment(image: Image.Image, color=[255, 255, 255]):
12
  model = create_model("Unet_2020-10-30")
13
  model.eval()
14
 
@@ -22,51 +21,15 @@ def Segment(image: Image.Image, color=[255, 255, 255]):
22
  with torch.no_grad():
23
  prediction = model(x)[0][0]
24
 
25
- mask = (prediction > 0).cpu().numpy().astype(np.uint8)
 
26
  mask = unpad(mask, pads)
27
 
28
- mask_rgb = np.ones((*mask.shape, 3), dtype=np.uint8) * 255
29
- mask_rgb[mask == 1] = color
30
 
31
- output_image = Image.fromarray(mask_rgb)
32
-
33
- return output_image
34
- '''
35
-
36
- from iglovikov_helper_functions.utils.image_utils import load_rgb, pad, unpad
37
- from iglovikov_helper_functions.dl.pytorch.utils import tensor_from_rgb_image
38
- from cloths_segmentation.pre_trained_models import create_model
39
- from PIL import Image
40
- import numpy as np
41
- import cv2
42
- import torch
43
- import albumentations as albu
44
- from PIL import ImageOps
45
-
46
- def Segment(image: Image.Image, color=[255, 255, 255]):
47
- model = create_model("Unet_2020-10-30")
48
- model.eval()
49
-
50
- image = np.array(image)
51
- transform = albu.Compose([albu.Normalize(p=1)], p=1)
52
- padded_image, pads = pad(image, factor=32, border=cv2.BORDER_CONSTANT)
53
-
54
- x = transform(image=padded_image)["image"]
55
- x = torch.unsqueeze(tensor_from_rgb_image(x), 0)
56
-
57
- with torch.no_grad():
58
- prediction = model(x)[0][0]
59
-
60
- mask = (prediction > 0).cpu().numpy().astype(np.uint8)
61
- mask = unpad(mask, pads)
62
-
63
- mask_rgb = np.ones((*mask.shape, 3), dtype=np.uint8) * 255
64
- mask_rgb[mask == 1] = color
65
-
66
- # Create an output image with transparent background
67
- output_image = Image.new("RGBA", image.shape[:2][::-1], (255, 255, 255, 0))
68
- output_image = ImageOps.invert(output_image) # Invert to black background
69
-
70
- output_image.paste(Image.fromarray(mask_rgb), (0, 0), Image.fromarray(mask))
71
 
72
  return output_image
 
 
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
 
7
  import torch
8
  import albumentations as albu
9
 
10
+ def Segment(image: Image.Image):
11
  model = create_model("Unet_2020-10-30")
12
  model.eval()
13
 
 
21
  with torch.no_grad():
22
  prediction = model(x)[0][0]
23
 
24
+ mask = (prediction > 0).cpu().numpy()
25
+ mask = np.where(mask==1, 255, 0).astype(np.uint8)
26
  mask = unpad(mask, pads)
27
 
28
+ alpha_channel = np.ones(mask.shape, dtype=np.uint8) * 255
29
+ alpha_channel[mask == 0] = 0
30
 
31
+ image_with_alpha = cv2.cvtColor(image, cv2.COLOR_RGB2RGBA)
32
+ image_with_alpha[:, :, 3] = alpha_channel
33
+ output_image = Image.fromarray(image_with_alpha)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  return output_image