Spaces:
Sleeping
Sleeping
Create src/utilis.py
Browse files- src/utilis.py +106 -0
src/utilis.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import numpy as np
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import time
|
5 |
+
import os
|
6 |
+
from PIL import Image, ImageColor
|
7 |
+
from copy import deepcopy
|
8 |
+
|
9 |
+
import torch
|
10 |
+
import torch.nn as nn
|
11 |
+
import torch.nn.functional as F
|
12 |
+
import torchvision.transforms as transforms
|
13 |
+
|
14 |
+
from src.models.modnet import MODNet
|
15 |
+
|
16 |
+
|
17 |
+
# apply(st)
|
18 |
+
|
19 |
+
MODEL = "./assets/modnet_photographic_portrait_matting.ckpt"
|
20 |
+
|
21 |
+
|
22 |
+
def change_background(image, matte, background_alpha: float=1.0, background_hex: str="#000000"):
|
23 |
+
"""
|
24 |
+
image: PIL Image (RGBA)
|
25 |
+
matte: PIL Image (grayscale, if 255 it is foreground)
|
26 |
+
background_alpha: float
|
27 |
+
background_hex: string
|
28 |
+
"""
|
29 |
+
img = deepcopy(image)
|
30 |
+
if image.mode != "RGBA":
|
31 |
+
img = img.convert("RGBA")
|
32 |
+
|
33 |
+
background_color = ImageColor.getrgb(background_hex)
|
34 |
+
background_alpha = int(255 * background_alpha)
|
35 |
+
background = Image.new("RGBA", img.size, color=background_color + (background_alpha,))
|
36 |
+
background.paste(img, mask=matte)
|
37 |
+
return background
|
38 |
+
|
39 |
+
|
40 |
+
def matte(image):
|
41 |
+
# define hyper-parameters
|
42 |
+
ref_size = 512
|
43 |
+
|
44 |
+
# define image to tensor transform
|
45 |
+
im_transform = transforms.Compose(
|
46 |
+
[
|
47 |
+
transforms.ToTensor(),
|
48 |
+
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
|
49 |
+
]
|
50 |
+
)
|
51 |
+
|
52 |
+
# create MODNet and load the pre-trained ckpt
|
53 |
+
modnet = MODNet(backbone_pretrained=False)
|
54 |
+
modnet = nn.DataParallel(modnet)
|
55 |
+
|
56 |
+
if torch.cuda.is_available():
|
57 |
+
modnet = modnet.cuda()
|
58 |
+
weights = torch.load(MODEL)
|
59 |
+
else:
|
60 |
+
weights = torch.load(MODEL, map_location=torch.device('cpu'))
|
61 |
+
modnet.load_state_dict(weights)
|
62 |
+
modnet.eval()
|
63 |
+
|
64 |
+
# read image
|
65 |
+
im = deepcopy(image)
|
66 |
+
|
67 |
+
# unify image channels to 3
|
68 |
+
im = np.asarray(im)
|
69 |
+
if len(im.shape) == 2:
|
70 |
+
im = im[:, :, None]
|
71 |
+
if im.shape[2] == 1:
|
72 |
+
im = np.repeat(im, 3, axis=2)
|
73 |
+
elif im.shape[2] == 4:
|
74 |
+
im = im[:, :, 0:3]
|
75 |
+
|
76 |
+
# convert image to PyTorch tensor
|
77 |
+
im = Image.fromarray(im)
|
78 |
+
im = im_transform(im)
|
79 |
+
|
80 |
+
# add mini-batch dim
|
81 |
+
im = im[None, :, :, :]
|
82 |
+
|
83 |
+
# resize image for input
|
84 |
+
im_b, im_c, im_h, im_w = im.shape
|
85 |
+
if max(im_h, im_w) < ref_size or min(im_h, im_w) > ref_size:
|
86 |
+
if im_w >= im_h:
|
87 |
+
im_rh = ref_size
|
88 |
+
im_rw = int(im_w / im_h * ref_size)
|
89 |
+
elif im_w < im_h:
|
90 |
+
im_rw = ref_size
|
91 |
+
im_rh = int(im_h / im_w * ref_size)
|
92 |
+
else:
|
93 |
+
im_rh = im_h
|
94 |
+
im_rw = im_w
|
95 |
+
|
96 |
+
im_rw = im_rw - im_rw % 32
|
97 |
+
im_rh = im_rh - im_rh % 32
|
98 |
+
im = F.interpolate(im, size=(im_rh, im_rw), mode='area')
|
99 |
+
|
100 |
+
# inference
|
101 |
+
_, _, matte = modnet(im.cuda() if torch.cuda.is_available() else im, True)
|
102 |
+
|
103 |
+
# resize and save matte
|
104 |
+
matte = F.interpolate(matte, size=(im_h, im_w), mode='area')
|
105 |
+
matte = matte[0][0].data.cpu().numpy()
|
106 |
+
return Image.fromarray(((matte * 255).astype('uint8')), mode='L')
|