Skip to content

Commit 439aec2

Browse files
author
Aleksei Korobeinikov
authored
OV 2.0: python colorization demo (#3010)
* update colorization demo to OV 2.0 * fix search in input names * micro changes for inputs * add sys.exit()
1 parent da1e0da commit 439aec2

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

demos/colorization_demo/python/colorization_demo.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22
"""
3-
Copyright (c) 2018-2020 Intel Corporation
3+
Copyright (c) 2018-2021 Intel Corporation
44
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
@@ -15,7 +15,7 @@
1515
limitations under the License.
1616
"""
1717

18-
from openvino.inference_engine import IECore, get_version
18+
from openvino.runtime import Core, get_version
1919
import cv2 as cv
2020
import numpy as np
2121
import logging as log
@@ -60,33 +60,28 @@ def build_arg():
6060
help="Optional. List of monitors to show initially.")
6161
return parser
6262

63-
64-
if __name__ == '__main__':
65-
args = build_arg().parse_args()
66-
63+
def main(args):
6764
cap = open_images_capture(args.input, args.loop)
6865

6966
log.info('OpenVINO Inference Engine')
7067
log.info('\tbuild: {}'.format(get_version()))
71-
ie = IECore()
68+
core = Core()
7269

7370
log.info('Reading model {}'.format(args.model))
74-
load_net = ie.read_network(args.model, args.model.with_suffix(".bin"))
75-
load_net.batch_size = 1
71+
model = core.read_model(args.model, args.model.with_suffix(".bin"))
7672

77-
input_blob = next(iter(load_net.input_info))
78-
input_shape = load_net.input_info[input_blob].input_data.shape
73+
input_tensor_name = 'data_l'
74+
input_shape = model.input(input_tensor_name).shape
7975
assert input_shape[1] == 1, "Expected model input shape with 1 channel"
8076

8177
inputs = {}
82-
for input_name in load_net.input_info:
83-
inputs[input_name] = np.zeros(load_net.input_info[input_name].input_data.shape)
78+
for input in model.inputs:
79+
inputs[input.get_any_name()] = np.zeros(input.shape)
8480

85-
assert len(load_net.outputs) == 1, "Expected number of outputs is equal 1"
86-
output_blob = next(iter(load_net.outputs))
87-
output_shape = load_net.outputs[output_blob].shape
81+
assert len(model.outputs) == 1, "Expected number of outputs is equal 1"
8882

89-
exec_net = ie.load_network(network=load_net, device_name=args.device)
83+
compiled_model = core.compile_model(model, device_name=args.device)
84+
infer_request = compiled_model.create_infer_request()
9085
log.info('The model {} is loaded to {}'.format(args.model, args.device))
9186

9287
_, _, h_in, w_in = input_shape
@@ -118,11 +113,12 @@ def build_arg():
118113
img_rgb = frame.astype(np.float32) / 255
119114
img_lab = cv.cvtColor(img_rgb, cv.COLOR_RGB2Lab)
120115
img_l_rs = cv.resize(img_lab.copy(), (w_in, h_in))[:, :, 0]
121-
inputs[input_blob] = img_l_rs
122116

123-
res = exec_net.infer(inputs=inputs)
117+
inputs[input_tensor_name] = np.expand_dims(img_l_rs, axis=[0, 1])
124118

125-
update_res = np.squeeze(res[output_blob])
119+
res = next(iter(infer_request.infer(inputs).values()))
120+
121+
update_res = np.squeeze(res)
126122

127123
out = update_res.transpose((1, 2, 0))
128124
out = cv.resize(out, (w_orig, h_orig))
@@ -166,3 +162,7 @@ def build_arg():
166162
metrics.log_total()
167163
for rep in presenter.reportMeans():
168164
log.info(rep)
165+
166+
if __name__ == "__main__":
167+
args = build_arg().parse_args()
168+
sys.exit(main(args) or 0)

0 commit comments

Comments
 (0)