-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathu2net-portrait-matting.py
216 lines (166 loc) · 5.72 KB
/
u2net-portrait-matting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import sys
import time
import ailia
import cv2
import numpy as np
from PIL import Image
# import original modules
sys.path.append('../../util')
# logger
from logging import getLogger # noqa: E402
import webcamera_utils # noqa: E402
from detector_utils import load_image # noqa: E402
from image_utils import imread # noqa: E402
from model_utils import check_and_download_models # noqa: E402
from arg_utils import get_base_parser, get_savepath, update_parser # noqa: E402
logger = getLogger(__name__)
# ======================
# Parameters
# ======================
WEIGHT_PATH = 'u2net-portrait-matting.onnx'
MODEL_PATH = 'u2net-portrait-matting.onnx.prototxt'
REMOTE_PATH = 'https://storage.googleapis.com/ailia-models/u2net-portrait-matting/'
IMAGE_PATH = 'input.png'
SAVE_IMAGE_PATH = 'output.png'
IMAGE_SIZE = 448
# ======================
# Arguemnt Parser Config
# ======================
parser = get_base_parser('U^2-Net - Portrait matting', IMAGE_PATH, SAVE_IMAGE_PATH)
parser.add_argument(
'-c', '--composite',
action='store_true',
help='Composite input image and predicted alpha value'
)
parser.add_argument(
'-w', '--width',
default=IMAGE_SIZE, type=int,
help='The segmentation width and height for u2net.'
)
parser.add_argument(
'-h', '--height',
default=IMAGE_SIZE, type=int,
help='The segmentation height and height for u2net.'
)
args = update_parser(parser)
# ======================
# Main functions
# ======================
def preprocess(img):
mean = np.array((0.5, 0.5, 0.5))
std = np.array((0.5, 0.5, 0.5))
h, w = img.shape[:2]
max_wh = max(h, w)
hp = (max_wh - w) // 2
vp = (max_wh - h) // 2
h, w = img.shape[:2]
img_pad = np.zeros((max_wh, max_wh, 3), dtype=np.uint8)
img_pad[vp:vp + h, hp:hp + w, ...] = img
img = img_pad
img = np.array(Image.fromarray(img).resize(
(args.width, args.height),
resample=Image.LANCZOS))
img = img / 255
img = (img - mean) / std
img = img.transpose(2, 0, 1) # HWC -> CHW
img = np.expand_dims(img, axis=0)
return img.astype(np.float32), img_pad
def postprocess(pred, img_0, img_pad):
h0, w0 = img_0.shape[:2]
h1, w1 = img_pad.shape[:2]
pred = cv2.resize(pred, (w1, h1), cv2.INTER_LINEAR)
vp = (h1 - h0) // 2
hp = (w1 - w0) // 2
pred = pred[vp:vp + h0, hp:hp + w0]
pred = np.clip(pred, 0, 1)
return pred
def recognize_from_image(net):
# input image loop
for image_path in args.input:
# prepare input data
logger.info(image_path)
# prepare input data
img = img_0 = load_image(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGB)
img, img_pad = preprocess(img)
# inference
logger.info('Start inference...')
if args.benchmark:
logger.info('BENCHMARK mode')
for i in range(5):
start = int(round(time.time() * 1000))
output = net.predict({'img': img})
end = int(round(time.time() * 1000))
logger.info(f'\tailia processing time {end - start} ms')
else:
output = net.predict({'img': img})
# postprocessing
pred = output[0][0]
pred = postprocess(pred, img_0, img_pad)
if not args.composite:
res_img = pred * 255
else:
# composite
h, w = img_0.shape[:2]
image = imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2BGRA)
image[:, :, 3] = cv2.resize(pred, (w, h)) * 255
res_img = image
savepath = get_savepath(args.savepath, image_path, ext='.png')
logger.info(f'saved at : {savepath}')
cv2.imwrite(savepath, res_img)
logger.info('Script finished successfully.')
def recognize_from_video(net):
capture = webcamera_utils.get_capture(args.video)
# create video writer if savepath is specified as video format
f_h = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
f_w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
if args.savepath != SAVE_IMAGE_PATH:
logger.warning(
'currently, video results cannot be output correctly...'
)
writer = webcamera_utils.get_writer(args.savepath, f_h, f_w)
else:
writer = None
frame_shown = False
while True:
ret, frame = capture.read()
if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret:
break
if frame_shown and cv2.getWindowProperty('frame', cv2.WND_PROP_VISIBLE) == 0:
break
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img, img_pad = preprocess(img)
# inference
output = net.predict({'img': img})
# postprocessing
pred = output[0][0]
pred = postprocess(pred, frame, img_pad)
# force composite
frame[:, :, 0] = frame[:, :, 0] * pred + 64 * (1 - pred)
frame[:, :, 1] = frame[:, :, 1] * pred + 177 * (1 - pred)
frame[:, :, 2] = frame[:, :, 2] * pred
cv2.imshow('frame', frame)
frame_shown = True
# save results
if writer is not None:
writer.write(frame.astype(np.uint8))
capture.release()
cv2.destroyAllWindows()
if writer is not None:
writer.release()
logger.info('Script finished successfully.')
def main():
# model files check and download
check_and_download_models(WEIGHT_PATH, MODEL_PATH, REMOTE_PATH)
# net initialize
net = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=args.env_id)
net.set_input_shape((1, 3, args.height, args.width)) # dynamic axis
if args.video is not None:
# video mode
recognize_from_video(net)
else:
# image mode
recognize_from_image(net)
if __name__ == '__main__':
main()