-
Notifications
You must be signed in to change notification settings - Fork 356
Expand file tree
/
Copy pathhands_segmentation_pytorch.py
More file actions
160 lines (116 loc) · 4.27 KB
/
hands_segmentation_pytorch.py
File metadata and controls
160 lines (116 loc) · 4.27 KB
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
import sys
import time
import ailia
import cv2
import numpy as np
# import original modules
sys.path.append('../../util')
# logger
from logging import getLogger # noqa: E402
import webcamera_utils # 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
from scipy.special import softmax
logger = getLogger(__name__)
# ======================
# Parameters
# ======================
WEIGHT_PATH = "hands_segmentation_pytorch.onnx"
MODEL_PATH = WEIGHT_PATH + '.prototxt'
REMOTE_PATH = "https://storage.googleapis.com/ailia-models/hands_segmentation_pytorch/"
DEFAULT_INPUT_PATH = 'sample_image.jpg'
DEFAULT_SAVE_PATH = 'output.png'
# ======================
# Arguemnt Parser Config
# ======================
parser = get_base_parser(
'Hands Segmentation in PyTorch - A Plug and Play Model',
DEFAULT_INPUT_PATH, DEFAULT_SAVE_PATH
)
parser.add_argument(
'--height', type=int, default=256,
help='height of the image to run inference on '
)
parser.add_argument(
'--width', type=int, default=256,
help='width of the image to run inference on'
)
parser.add_argument(
'--overlay', action='store_true',
help='Visualize the mask overlayed on the image'
)
args = update_parser(parser)
# ======================
# Helper functions
# ======================
def preprocess(image, h=None, w=None, mean = np.array([0.485, 0.456, 0.406]), std = np.array([0.229, 0.224, 0.225])):
if h is not None and w is not None:
image = cv2.resize(image, (w, h))
image = (image - mean[None,None,:]) / std[None,None,:]
return image.transpose(2, 0, 1)
def postprocess(image, logits, h, w, overlay=False):
mask = softmax(logits[0], axis=0)[1]
mask = cv2.resize(mask, dsize=(w, h), interpolation=cv2.INTER_LINEAR)
if overlay:
mask = (np.where(mask[:,:,None] > 0.5, np.array([1., 0, 0]) * 0.5 + image * 0.5, image) * 255).astype('uint8')
else:
mask = (mask[:,:,None]> 0.5).astype('uint8') * 255
return mask
# ======================
# Main functions
# ======================
def recognize_from_image(model):
logger.info('Start inference...')
image_path = args.input[0]
# prepare input data
org_img = cv2.cvtColor(imread(image_path), cv2.COLOR_BGR2RGB) / 255.
image = preprocess(org_img, h = args.height, w = args.width)[None]
if args.benchmark and not (args.video is not None):
logger.info('BENCHMARK mode')
for i in range(5):
start = int(round(time.time() * 1000))
logits = model.predict(image)
end = int(round(time.time() * 1000))
logger.info(f'\tailia processing time {end - start} ms')
else:
logits = model.predict(image)
mask = postprocess(org_img, logits, org_img.shape[0], org_img.shape[1], args.overlay)
# visualize
logger.info(f'saving result to {args.savepath}')
mask = cv2.cvtColor(mask, cv2.COLOR_RGB2BGR)
cv2.imwrite(args.savepath, mask)
logger.info('Script finished successfully.')
def recognize_from_video(model):
# net initialize
capture = webcamera_utils.get_capture(args.video)
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
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) / 255.
# inference
image = preprocess(frame, h = args.height, w = args.width)[None]
logits = model.predict(image)
mask = postprocess(frame, logits, frame.shape[0], frame.shape[1], args.overlay)
# visualize
cv2.imshow("frame", mask)
frame_shown = True
capture.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
model = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id = args.env_id)
if args.video is not None:
# video mode
recognize_from_video(model)
else:
# image mode
recognize_from_image(model)
if __name__ == '__main__':
main()