Skip to content

Commit 682ce9a

Browse files
ov 2.0: python image_inpainting_demo (#3056)
1 parent da141d2 commit 682ce9a

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

demos/image_inpainting_demo/python/image_inpainting_demo.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22
"""
3-
Copyright (c) 2019-2020 Intel Corporation
3+
Copyright (c) 2019-2022 Intel Corporation
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
@@ -20,7 +20,7 @@
2020

2121
import numpy as np
2222
import cv2
23-
from openvino.inference_engine import IECore, get_version
23+
from openvino.runtime import Core, get_version
2424

2525
from inpainting_gui import InpaintingGUI
2626
from inpainting import ImageInpainting
@@ -132,10 +132,10 @@ def main():
132132

133133
log.info('OpenVINO Inference Engine')
134134
log.info('\tbuild: {}'.format(get_version()))
135-
ie = IECore()
135+
core = Core()
136136

137137
log.info('Reading model {}'.format(args.model))
138-
inpainting_processor = ImageInpainting(ie, args.model, args.device)
138+
inpainting_processor = ImageInpainting(core, args.model, args.device)
139139
log.info('The model {} is loaded to {}'.format(args.model, args.device))
140140

141141
if args.auto_mask_color or args.auto_mask_random:
Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Copyright (c) 2019-2020 Intel Corporation
2+
Copyright (c) 2019-2022 Intel Corporation
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
55
You may obtain a copy of the License at
@@ -15,40 +15,49 @@
1515

1616

1717
class ImageInpainting:
18-
def __init__(self, ie, model_path, device='CPU'):
19-
model = ie.read_network(model_path, model_path.with_suffix('.bin'))
18+
def __init__(self, core, model_path, device='CPU'):
19+
model = core.read_model(model_path, model_path.with_suffix('.bin'))
2020

21-
assert len(model.input_info) == 2, "Expected 2 input blob"
22-
assert len(model.outputs) == 1, "Expected 1 output blobs"
21+
if len(model.inputs) != 2:
22+
raise RuntimeError("The model expects 2 input layers")
23+
if len(model.outputs) != 1:
24+
raise RuntimeError("The model expects 1 output layer")
2325

24-
self._input_layer_names = sorted(model.input_info)
25-
self._output_layer_name = next(iter(model.outputs))
26+
self.image_input_layer, self.mask_input_layer = sorted([node.get_any_name() for node in model.inputs])
2627

27-
self._ie = ie
28-
self._exec_model = self._ie.load_network(model, device)
28+
compiled_model = core.compile_model(model, device)
29+
self.infer_request = compiled_model.create_infer_request()
2930

30-
_, channels, input_height, input_width = model.input_info[self._input_layer_names[0]].input_data.shape
31-
assert channels == 3, "Expected 3-channel input"
31+
self.nchw_layout = model.input(self.image_input_layer).shape[1] == 3
32+
if self.nchw_layout:
33+
_, _, input_height, input_width = model.input(self.image_input_layer).shape
34+
_, mask_channels, mask_height, mask_width = model.input(self.mask_input_layer).shape
35+
else:
36+
_, input_height, input_width, _ = model.input(self.image_input_layer).shape
37+
_, mask_height, mask_width, mask_channels = model.input(self.mask_input_layer).shape
3238

33-
_, channels, mask_height, mask_width = model.input_info[self._input_layer_names[1]].input_data.shape
34-
assert channels == 1, "Expected 1-channel input"
39+
if mask_channels != 1:
40+
raise RuntimeError("The model expects 1 channel for {} input layer".format(self.mask_input_layer))
3541

36-
assert mask_height == input_height and mask_width == input_width, "Mask size expected to be equal to image size"
42+
if mask_height != input_height or mask_width != input_width:
43+
raise RuntimeError("Mask size is expected to be equal to image size")
3744
self.input_height = input_height
3845
self.input_width = input_width
3946

4047

4148
def infer(self, image, mask):
42-
output = self._exec_model.infer(inputs={self._input_layer_names[0]: image, self._input_layer_names[1]: mask})
43-
return output[self._output_layer_name]
49+
output = self.infer_request.infer(inputs={self.image_input_layer: image, self.mask_input_layer: mask})
50+
return next(iter(output.values()))
4451

4552

46-
def process(self, src_image, mask):
47-
image = np.transpose(src_image, (2, 0, 1))
48-
mask = np.transpose(mask, (2, 0, 1))
53+
def process(self, image, mask):
54+
if self.nchw_layout:
55+
image = np.transpose(image, (2, 0, 1))
56+
mask = np.transpose(mask, (2, 0, 1))
4957
image = np.expand_dims(image, axis=0)
5058
mask = np.expand_dims(mask, axis=0)
5159
output = self.infer(image, mask)
5260

53-
output = np.transpose(output, (0, 2, 3, 1)).astype(np.uint8)
54-
return output[0]
61+
if self.nchw_layout:
62+
output = np.transpose(output, (0, 2, 3, 1))
63+
return output.astype(np.uint8)[0]

0 commit comments

Comments
 (0)