forked from axinc-ai/ailia-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhitnet.py
199 lines (154 loc) · 5.54 KB
/
hitnet.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
import glob
import sys
import time
import ailia
import cv2
import numpy as np
import onnxruntime
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
logger = getLogger(__name__)
from arg_utils_hitnet import CameraConfig, draw_depth, draw_disparity
camera_config = CameraConfig(0.546, 1000)
max_distance = 30
# ======================
# Parameters 1
# ======================
LEFT_IMAGE_PATH = "./cones/im2.ppm"
RIGHT_IMAGE_PATH = "./cones/im6.ppm"
STEREO_DATA_DIR ="./stereo_data"
SAVE_IMAGE_PATH = 'output.png'
WEIGHT_PATH = 'hitnet.onnx'
MODEL_PATH = 'hitnet.onnx.prototxt'
REMOTE_PATH = 'https://storage.googleapis.com/ailia-models/hitnet/'
# ======================
# Argument Parser Config
# ======================
parser = get_base_parser(
'hitnet',
LEFT_IMAGE_PATH,
RIGHT_IMAGE_PATH,
SAVE_IMAGE_PATH,
)
parser.add_argument(
'-l','--left', type=str,
default=LEFT_IMAGE_PATH,
help='The input image for left image.'
)
parser.add_argument(
'-r', '--right', type=str,
default=RIGHT_IMAGE_PATH,
help='The input image for right image.'
)
parser.add_argument(
'-v', '--video', type=str,
help='The input video for pole detection.'
)
parser.add_argument(
'-o', '--onnx', action='store_true',
help="Option to use onnxrutime to run or not."
)
args = update_parser(parser)
# # ======================
# # Main functions
# # ======================
def preprocessing(left_img,right_img,input_shape):
left_img = cv2.resize(left_img,(input_shape[3],input_shape[2]))
right_img = cv2.resize(right_img,(input_shape[3],input_shape[2]))
left_img = cv2.cvtColor(left_img, cv2.COLOR_BGR2RGB)
right_img = cv2.cvtColor(right_img, cv2.COLOR_BGR2RGB)
combined_img = np.concatenate((left_img, right_img), axis=-1) / 255.0
combined_img = combined_img.transpose(2, 0, 1)
return np.expand_dims(combined_img, 0).astype(np.float32)
def recognize_from_image(net):
# read left and right images
left_img = imread(args.left)
right_img = imread(args.right)
# get model info
if args.onnx:
input_name = net.get_inputs()[0].name
output_name = net.get_outputs()[0].name
input_shape = net.get_inputs()[0].shape
else:
input_shape = net.get_input_shape()
# preprocessing
input_tensor = preprocessing(left_img,right_img,input_shape)
if args.onnx:
disparity_map = net.run( [output_name], { input_name : input_tensor })[0][0]
else:
disparity_map = net.run( input_tensor )[0][0]
# estimate depth
disparity_map = np.array(disparity_map)
depth_map = camera_config.f*camera_config.baseline / disparity_map
color_disparity = draw_disparity(disparity_map)
color_depth = draw_depth(depth_map, max_distance)
# save output
color_depth = cv2.resize(color_depth, (left_img.shape[1],left_img.shape[0]))
cv2.imwrite("output.png",color_depth)
logger.info('Script finished successfully.')
def recognize_from_video(net):
# get image list
left_images = glob.glob( args.video + '/image_L/*.png')
left_images.sort()
right_images = glob.glob( args.video + '/image_R/*.png')
right_images.sort()
# get model info
if args.onnx:
input_name = net.get_inputs()[0].name
output_name = net.get_outputs()[0].name
input_shape = net.get_inputs()[0].shape
else:
input_shape = net.get_input_shape()
if not left_images or not right_images:
logger.error("This model requires stereo images")
return
cv2.namedWindow("Estimated depth", cv2.WINDOW_NORMAL)
for left_path, right_path in zip(left_images, right_images):
# Read frame from the video
left_img = imread(left_path)
right_img = imread(right_path)
# preprocessing
input_tensor = preprocessing(left_img,right_img,input_shape)
if args.onnx:
disparity_map = net.run( [output_name], { input_name : input_tensor })[0][0]
else:
disparity_map = net.run( input_tensor )[0][0]
# estimate depth
disparity_map = np.array(disparity_map)#.astype(np.uint8)
depth_map = camera_config.f*camera_config.baseline/disparity_map
color_disparity = draw_disparity(disparity_map)
color_depth = draw_depth(depth_map, max_distance)
# show output
color_depth = cv2.resize(color_depth, (left_img.shape[1],left_img.shape[0]))
combined_image = np.hstack((left_img, color_depth))
cv2.imshow("Estimated depth", combined_image)
# Press key q to stop
if cv2.waitKey(1) == ord('q'):
break
if cv2.getWindowProperty('Estimated depth', cv2.WND_PROP_VISIBLE) == 0:
break
cv2.destroyAllWindows()
logger.info('Script finished successfully.')
def main():
# model files check and download
check_and_download_models(WEIGHT_PATH, MODEL_PATH, REMOTE_PATH)
# net initialize
if args.onnx:
import onnxruntime
net = onnxruntime.InferenceSession(WEIGHT_PATH)
else:
logger.info(f'env_id: {args.env_id}')
net = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=args.env_id)
if args.video is not None:
# video mode
recognize_from_video(net)
else:
# image mode
recognize_from_image(net)
if __name__ == '__main__':
main()