forked from axinc-ai/ailia-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpspnet-hair-segmentation.py
215 lines (167 loc) · 5.86 KB
/
pspnet-hair-segmentation.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
import sys
import time
import numpy as np
import cv2
import ailia
# import original modules
sys.path.append('../../util')
from arg_utils import get_base_parser, update_parser, get_savepath # noqa: E402
from detector_utils import load_image # noqa: E402
from image_utils import normalize_image # noqa: E402
from model_utils import check_and_download_models # noqa: E402
import webcamera_utils # noqa: E402
# logger
from logging import getLogger # noqa: E402
logger = getLogger(__name__)
# ======================
# Parameters
# ======================
WEIGHT_RES_PATH = 'pspnet-hair-segmentation.onnx' #you can also use 'pspnet_resnet101.onnx'
MODEL_RES_PATH = WEIGHT_RES_PATH + '.prototxt'
WEIGHT_SQZ_PATH = 'pspnet_squeezenet.onnx'
MODEL_SQZ_PATH = WEIGHT_SQZ_PATH + '.prototxt'
REMOTE_PATH = \
'https://storage.googleapis.com/ailia-models/pspnet-hair-segmentation/'
IMAGE_PATH = 'test.jpg'
SAVE_IMAGE_PATH = 'output.png'
IMAGE_SIZE_RES = 512 #if you want to use 'pspnet_resnet101.onnx', please set 592
IMAGE_SIZE_SQZ = 592
# ======================
# Arguemnt Parser Config
# ======================
parser = get_base_parser(
'Real-time hair segmentation model', IMAGE_PATH, SAVE_IMAGE_PATH
)
parser.add_argument(
'-m', '--model', choices=('resnet101', 'squeezenet'),
default='resnet101', metavar='NAME',
help='name of neural network'
)
args = update_parser(parser)
# ======================
# Utils
# ======================
def sigmoid(x):
return 1.0 / (1.0 + np.exp(-x))
def img_resize(img, img_size):
h, w = img.shape[:2]
if img_size < max(h, w):
if h < w:
img = cv2.resize(img, (img_size, int(h * img_size / w + 0.5)), interpolation=cv2.INTER_AREA)
else:
img = cv2.resize(img, (int(w * img_size / h + 0.5), img_size), interpolation=cv2.INTER_AREA)
h, w = img.shape[:2]
y = (img_size - h) // 2
x = (img_size - w) // 2
pad_img = np.ones((img_size, img_size, 3), dtype=np.uint8) * 255
pad_img[y:y + h, x:x + w, ...] = img
return img, pad_img
def preprocess(img):
img = img.astype(np.float32)
img = normalize_image(img, normalize_type='ImageNet')
img = img.transpose((2, 0, 1)) # HWC -> CHW
img = np.expand_dims(img, axis=0)
return img
def postprocess(src_img, pred):
pred = sigmoid(pred)[0][0]
mask = pred >= 0.5
mask_n = np.zeros(src_img.shape)
mask_n[:, :, 0] = 255
mask_n[:, :, 0] *= mask
image_n = cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR)
image_n = image_n * 0.5 + mask_n * 0.5
return image_n
# ======================
# Main functions
# ======================
def recognize_from_image(net, img_size):
# input image loop
for image_path in args.input:
# prepare input data
logger.info(image_path)
img = load_image(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGB)
img, pad_img = img_resize(img, img_size)
input_data = preprocess(pad_img)
# inference
logger.info('Start inference...')
if args.benchmark:
logger.info('BENCHMARK mode')
for i in range(5):
start = int(round(time.time() * 1000))
pred = net.predict(input_data)
end = int(round(time.time() * 1000))
logger.info(f'\tailia processing time {end - start} ms')
else:
pred = net.predict(input_data)
# postprocessing
res_img = postprocess(pad_img, pred)
h, w = img.shape[:2]
y = (pad_img.shape[0] - h) // 2
x = (pad_img.shape[1] - w) // 2
res_img = res_img[y:y + h, x:x + w, ...]
savepath = get_savepath(args.savepath, image_path)
logger.info(f'saved at : {savepath}')
cv2.imwrite(savepath, res_img)
logger.info('Script finished successfully.')
def recognize_from_video(net, img_size):
capture = webcamera_utils.get_capture(args.video)
# create video writer if savepath is specified as video format
if args.savepath != SAVE_IMAGE_PATH:
logger.warning(
'currently, video results cannot be output correctly...'
)
writer = webcamera_utils.get_writer(
args.savepath, img_size, img_size
)
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
# prepare input data
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img, pad_img = img_resize(img, img_size)
input_data = preprocess(pad_img)
# inference
pred = net.predict(input_data)
# postprocessing
res_img = postprocess(pad_img, pred)
h, w = img.shape[:2]
y = (pad_img.shape[0] - h) // 2
x = (pad_img.shape[1] - w) // 2
res_img = res_img[y:y + h, x:x + w, ...]
cv2.imshow('frame', res_img / 255.0)
frame_shown = True
# # save results
# if writer is not None:
# writer.write(res_img)
capture.release()
cv2.destroyAllWindows()
if writer is not None:
writer.release()
logger.info('Script finished successfully.')
def main():
info = {
'resnet101': (
WEIGHT_RES_PATH, MODEL_RES_PATH, IMAGE_SIZE_RES),
'squeezenet': (
WEIGHT_SQZ_PATH, MODEL_SQZ_PATH, IMAGE_SIZE_SQZ),
}
weight_path, model_path, img_size = info[args.model]
# 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)
if args.video is not None:
# video mode
recognize_from_video(net, img_size)
else:
# image mode
recognize_from_image(net, img_size)
if __name__ == '__main__':
main()