Skip to content

Commit 19a3139

Browse files
authored
Merge branch 'openvinotoolkit:master' into master
2 parents 715f645 + f7cd466 commit 19a3139

File tree

209 files changed

+3360
-3038
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

209 files changed

+3360
-3038
lines changed

data/dataset_definitions.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ datasets:
159159
annotation_conversion:
160160
converter: mscoco_mask_rcnn
161161
annotation_file: instances_val2017_persons.json
162-
has_background: True
162+
has_background: False
163163
sort_annotations: True
164164
annotation: mscoco_mask_rcnn_person.pickle
165165
dataset_meta: mscoco_mask_rcnn_person.json

demos/3d_segmentation_demo/python/3d_segmentation_demo.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ def read_image(test_data_path, data_name, sizes=(128, 128, 128), is_series=True,
245245
def main():
246246
args = parse_arguments()
247247

248-
# --------------------------------- 1. Load Plugin for inference engine ---------------------------------
249248
log.info('OpenVINO Inference Engine')
250249
log.info('\tbuild: {}'.format(get_version()))
251250
core = Core()
@@ -262,7 +261,6 @@ def main():
262261
raise AttributeError("Device {} do not support of 3D convolution. "
263262
"Please use CPU, GPU or HETERO:*CPU*, HETERO:*GPU*")
264263

265-
# --------------------- 2. Read IR Generated by ModelOptimizer (.xml and .bin files) ---------------------
266264
log.info('Reading model {}'.format(args.path_to_model))
267265
model = core.read_model(args.path_to_model)
268266

@@ -279,12 +277,10 @@ def main():
279277

280278
n, c, d, h, w = model.inputs[0].shape
281279

282-
# ------------------------------------ 3. Loading model to the plugin -------------------------------------
283280
compiled_model = core.compile_model(model, args.target_device)
284281
infer_request = compiled_model.create_infer_request()
285282
log.info('The model {} is loaded to {}'.format(args.path_to_model, args.target_device))
286283

287-
# --------------------------------------- 4. Preparing input data -----------------------------------------
288284
start_time = perf_counter()
289285
if not os.path.exists(args.path_to_input_data):
290286
raise AttributeError("Path to input data: '{}' does not exist".format(args.path_to_input_data))
@@ -312,9 +308,7 @@ def main():
312308
original_data = data_crop
313309
original_size = original_data.shape[-3:]
314310

315-
# ---------------------------------------------- 5. Do inference --------------------------------------------
316311
result = infer_request.infer({input_tensor_name: data_crop})
317-
# ---------------------------- 6. Processing of the received inference results ------------------------------
318312
result = next(iter(result.values()))
319313
batch, channels, out_d, out_h, out_w = result.shape
320314

@@ -375,7 +369,6 @@ def main():
375369
total_latency = (perf_counter() - start_time) * 1e3
376370
log.info("Metrics report:")
377371
log.info("\tLatency: {:.1f} ms".format(total_latency))
378-
# --------------------------------------------- 7. Save output -----------------------------------------------
379372
tiff_output_name = os.path.join(args.path_to_output, 'output.tiff')
380373
Image.new('RGB', (original_data.shape[3], original_data.shape[2])).save(tiff_output_name,
381374
append_images=list_img, save_all=True)

demos/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ macro(add_demo)
134134
target_include_directories(${OMZ_DEMO_NAME} PRIVATE ${OMZ_DEMO_INCLUDE_DIRECTORIES})
135135
endif()
136136

137-
target_link_libraries(${OMZ_DEMO_NAME} PRIVATE ${OpenCV_LIBRARIES} ${InferenceEngine_LIBRARIES}
137+
target_link_libraries(${OMZ_DEMO_NAME} PRIVATE ${OpenCV_LIBRARIES} openvino::runtime ${InferenceEngine_LIBRARIES}
138138
${OMZ_DEMO_DEPENDENCIES} ngraph::ngraph utils gflags)
139139

140140
if(UNIX)
@@ -143,6 +143,8 @@ macro(add_demo)
143143
endmacro()
144144

145145
find_package(OpenCV REQUIRED COMPONENTS core highgui videoio imgproc imgcodecs gapi)
146+
find_package(OpenVINO REQUIRED COMPONENTS Runtime)
147+
# TODO: remove InferenceEngine and ngraph after 2022.1
146148
find_package(InferenceEngine REQUIRED)
147149
find_package(ngraph REQUIRED)
148150

demos/background_subtraction_demo/python/README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This demo shows how to perform background subtraction using OpenVINO.
88
99
## How It Works
1010

11-
The demo application expects an instance segmentation model in the Intermediate Representation (IR) format with the following constraints:
11+
The demo application expects an instance segmentation or background matting model in the Intermediate Representation (IR) format with the following constraints:
1212
1. for instance segmentation models based on `Mask RCNN` approach:
1313
* One input: `image` for input image.
1414
* At least three outputs including:
@@ -22,12 +22,29 @@ The demo application expects an instance segmentation model in the Intermediate
2222
* `conf` with confidence scores for each class for all boxes
2323
* `mask` with fixed-size mask channels for all boxes.
2424
* `proto` with fixed-size segmentation heat maps prototypes for all boxes.
25+
3. for image background matting models:
26+
* Two inputs:
27+
* `src` for input image
28+
* `bgr` for input real background
29+
* At least two outputs including:
30+
* `fgr` with normalized in [0, 1] range foreground
31+
* `pha` with normalized in [0, 1] range alpha
32+
4. for video background matting models based on RNN architecture:
33+
* Five inputs:
34+
* `src` for input image
35+
* recurrent inputs: `r1`, `r2`, `r3`, `r4`
36+
* At least six outputs including:
37+
* `fgr` with normalized in [0, 1] range foreground
38+
* `pha` with normalized in [0, 1] range alpha
39+
* recurrent outputs: `rr1`, `rr2`, `rr3`, `rr4`
2540

2641
The use case for the demo is an online conference where is needed to show only foreground - people and, respectively, to hide or replace background.
2742
Based on this an instance segmentation model must be trained at least for person class.
2843

2944
As input, the demo application accepts a path to a single image file, a video file or a numeric ID of a web camera specified with a command-line argument `-i`
3045

46+
> **NOTE**: if you use image background matting models, `--background` argument should be specified. This is a background image that equal to a real background behind a person on an input frame and must have the same shape as an input image.
47+
3148
The demo workflow is the following:
3249

3350
1. The demo application reads image/video frames one by one, resizes them to fit into the input image blob of the network (`image`).
@@ -60,6 +77,8 @@ omz_converter --list models.lst
6077

6178
* instance-segmentation-person-????
6279
* yolact-resnet50-fpn-pytorch
80+
* background-matting-mobilenetv2
81+
* robust-video-matting
6382

6483
> **NOTE**: Refer to the tables [Intel's Pre-Trained Models Device Support](../../../models/intel/device_support.md) and [Public Pre-Trained Models Device Support](../../../models/public/device_support.md) for the details on models inference support at different devices.
6584
@@ -74,6 +93,7 @@ usage: background_subtraction_demo.py [-h] -m MODEL
7493
[--resize_type {crop,standard,fit_to_window,fit_to_window_letterbox}]
7594
[--labels LABELS]
7695
[--target_bgr TARGET_BGR]
96+
[--background BACKGROUND]
7797
[--blur_bgr BLUR_BGR]
7898
[-nireq NUM_INFER_REQUESTS]
7999
[-nstreams NUM_STREAMS]
@@ -87,7 +107,8 @@ Options:
87107
-h, --help Show this help message and exit.
88108
-m MODEL, --model MODEL
89109
Required. Path to an .xml file with a trained model or
90-
address of model inference service if using OVMS adapter.
110+
address of model inference service if using OVMS
111+
adapter.
91112
--adapter {openvino,ovms}
92113
Optional. Specify the model adapter. Default is
93114
openvino.
@@ -110,6 +131,11 @@ Options:
110131
--target_bgr TARGET_BGR
111132
Optional. Background onto which to composite the
112133
output (by default to green field).
134+
--background BACKGROUND
135+
Optional. Background image for background-matting
136+
model. This is a background image that equal to a real
137+
background behind a person on an input frame and must
138+
have the same shape as an input image.
113139
--blur_bgr BLUR_BGR Optional. Background blur strength (by default with
114140
value 0 is not applied).
115141

demos/background_subtraction_demo/python/background_subtraction_demo.py

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626

2727
sys.path.append(str(Path(__file__).resolve().parents[2] / 'common/python'))
2828

29-
from openvino.model_zoo.model_api.models import MaskRCNNModel, OutputTransform, RESIZE_TYPES, YolactModel
29+
from openvino.model_zoo.model_api.models import MaskRCNNModel, OutputTransform, RESIZE_TYPES, YolactModel, ImageMattingWithBackground, VideoBackgroundMatting
30+
from openvino.model_zoo.model_api.models.utils import load_labels
3031
from openvino.model_zoo.model_api.performance_metrics import PerformanceMetrics
3132
from openvino.model_zoo.model_api.pipelines import get_user_config, AsyncPipeline
3233
from openvino.model_zoo.model_api.adapters import create_core, OpenvinoAdapter, OVMSAdapter
@@ -44,7 +45,7 @@ def build_argparser():
4445
args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.')
4546
args.add_argument('-m', '--model', required=True,
4647
help='Required. Path to an .xml file with a trained model '
47-
'or address of model inference service if using ovms adapter.')
48+
'or address of model inference service if using OVMS adapter.')
4849
args.add_argument('--adapter', help='Optional. Specify the model adapter. Default is openvino.',
4950
default='openvino', type=str, choices=('openvino', 'ovms'))
5051
args.add_argument('-i', '--input', required=True,
@@ -61,6 +62,10 @@ def build_argparser():
6162
args.add_argument('--labels', help='Optional. Labels mapping file.', default=None, type=str)
6263
args.add_argument('--target_bgr', default=None, type=str,
6364
help='Optional. Background onto which to composite the output (by default to green field).')
65+
args.add_argument('--background', default=None, type=str,
66+
help='Optional. Background image for background-matting model. This is a background image '
67+
'that equal to a real background behind a person on an input frame and must have the '
68+
'same shape as an input image.')
6469
args.add_argument('--blur_bgr', default=0, type=int,
6570
help='Optional. Background blur strength (by default with value 0 is not applied).')
6671

@@ -99,13 +104,31 @@ def build_argparser():
99104
return parser
100105

101106

102-
def get_model(model_adapter, configuration):
107+
def get_model(model_adapter, configuration, args):
103108
inputs = model_adapter.get_input_layers()
104109
outputs = model_adapter.get_output_layers()
110+
need_bgr_input = False
111+
is_matting_model = False
105112
if len(inputs) == 1 and len(outputs) == 4 and 'proto' in outputs.keys():
106-
return YolactModel(model_adapter, configuration)
113+
model = YolactModel(model_adapter, configuration)
114+
elif len(inputs) == 5 and len(outputs) == 6 and 'pha' in outputs.keys():
115+
model = VideoBackgroundMatting(model_adapter, configuration)
116+
is_matting_model = True
117+
elif len(inputs) == 2 and len(outputs) in (2, 3) and 'bgr' in inputs.keys():
118+
if args.background is None:
119+
raise ValueError('The ImageMattingWithBackground model expects the specified "--background" option.')
120+
model = ImageMattingWithBackground(model_adapter, configuration)
121+
need_bgr_input = True
122+
is_matting_model = True
107123
else:
108-
return MaskRCNNModel(model_adapter, configuration)
124+
model = MaskRCNNModel(model_adapter, configuration)
125+
if not need_bgr_input and args.background is not None:
126+
log.warning('The \"--background\" option works only for ImageMattingWithBackground model. Option will be omitted.')
127+
128+
if args.raw_output_message and is_matting_model:
129+
log.warning('\'--raw_output_message\' argument is set but is used background-matting based model, nothing to show')
130+
args.raw_output_message = False
131+
return model, need_bgr_input
109132

110133

111134
def print_raw_results(outputs, frame_id):
@@ -132,14 +155,13 @@ def fit_to_window(input_img, output_resolution):
132155
return output
133156

134157

135-
def render_results(frame, objects, output_resolution, target_bgr, person_id, blur_kernel=0, show_with_original_frame=False):
136-
blur_kernel = tuple([blur_kernel] * 2) if blur_kernel else blur_kernel
137-
if target_bgr is None:
138-
target_bgr = cv2.blur(frame, blur_kernel) if blur_kernel else np.full(frame.shape, [155, 255, 120], dtype=np.uint8)
139-
else:
140-
target_bgr = cv2.resize(target_bgr, (frame.shape[1], frame.shape[0]))
141-
if blur_kernel:
142-
target_bgr = cv2.blur(target_bgr, blur_kernel)
158+
def process_matting(objects, target_bgr):
159+
fgr, pha = objects
160+
output = fgr * pha + target_bgr * (1 - pha)
161+
return (output * 255).astype(np.uint8)
162+
163+
164+
def process_masks(objects, frame, target_bgr, person_id):
143165
classes, masks = objects[1], objects[3]
144166
# Choose masks only for person class
145167
valid_inds = classes == person_id
@@ -154,6 +176,23 @@ def render_results(frame, objects, output_resolution, target_bgr, person_id, blu
154176
composed_mask = cv2.medianBlur(composed_mask.astype(np.uint8), 11)
155177
composed_mask = np.repeat(np.expand_dims(composed_mask, axis=-1), 3, axis=2)
156178
output = np.where(composed_mask == 1, frame, target_bgr)
179+
return output
180+
181+
182+
def render_results(frame, objects, output_resolution, target_bgr, person_id, blur_kernel=0, show_with_original_frame=False):
183+
blur_kernel = tuple([blur_kernel] * 2) if blur_kernel else blur_kernel
184+
if target_bgr is None:
185+
target_bgr = cv2.blur(frame, blur_kernel) if blur_kernel else np.full(frame.shape, [155, 255, 120], dtype=np.uint8)
186+
else:
187+
target_bgr = cv2.resize(target_bgr, (frame.shape[1], frame.shape[0]))
188+
if blur_kernel:
189+
target_bgr = cv2.blur(target_bgr, blur_kernel)
190+
191+
if len(objects) == 4:
192+
output = process_masks(objects, frame, target_bgr, person_id)
193+
else:
194+
output = process_matting(objects, target_bgr.astype(np.float32) / 255)
195+
157196
if show_with_original_frame:
158197
output = cv2.hconcat([frame, output])
159198
h, w = output.shape[:2]
@@ -176,14 +215,17 @@ def main():
176215
elif args.adapter == 'ovms':
177216
model_adapter = OVMSAdapter(args.model)
178217

179-
labels = ['__background__', 'person'] if args.labels is None else args.labels
218+
labels = ['__background__', 'person'] if args.labels is None else load_labels(args.labels)
219+
assert len(labels), 'The file with class labels is empty'
180220

181221
configuration = {
182222
'confidence_threshold': args.prob_threshold,
183223
'resize_type': args.resize_type
184224
}
185225

186-
model = get_model(model_adapter, configuration)
226+
model, need_bgr_input = get_model(model_adapter, configuration, args)
227+
228+
input_bgr = open_images_capture(args.background, False).read() if need_bgr_input else None
187229

188230
person_id = -1
189231
for i, label in enumerate(labels):
@@ -226,7 +268,8 @@ def main():
226268
cap.fps(), tuple(output_resolution)):
227269
raise RuntimeError("Can't open video writer")
228270
# Submit for inference
229-
pipeline.submit_data(frame, next_frame_id, {'frame': frame, 'start_time': start_time})
271+
data = {'src': frame, 'bgr': input_bgr} if input_bgr is not None else frame
272+
pipeline.submit_data(data, next_frame_id, {'frame': frame, 'start_time': start_time})
230273
next_frame_id += 1
231274
else:
232275
# Wait for empty request
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# This file can be used with the --list option of the model downloader.
22
instance-segmentation-person-????
33
yolact-resnet50-fpn-pytorch
4+
# TODO: background-matting-mobilenetv2
5+
# TODO: robust-video-matting

demos/colorization_demo/python/colorization_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def main(args):
6767
core = Core()
6868

6969
log.info('Reading model {}'.format(args.model))
70-
model = core.read_model(args.model, args.model.with_suffix(".bin"))
70+
model = core.read_model(args.model)
7171

7272
input_tensor_name = 'data_l'
7373
input_shape = model.input(input_tensor_name).shape

demos/common/cpp/monitors/include/monitors/presenter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class Presenter {
2727
cv::Size graphSize = {150, 60},
2828
std::size_t historySize = 20);
2929
void addRemoveMonitor(MonitorType monitor);
30-
void handleKey(int key); // handles c, d, m, h keys
31-
void drawGraphs(cv::Mat& frame);
30+
void handleKey(int key); // handles C, D, M, H keys
31+
void drawGraphs(const cv::Mat& frame);
3232
std::vector<std::string> reportMeans() const;
3333

3434
const int yPos;

demos/common/cpp/monitors/src/presenter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void Presenter::handleKey(int key) {
111111
}
112112
}
113113

114-
void Presenter::drawGraphs(cv::Mat& frame) {
114+
void Presenter::drawGraphs(const cv::Mat& frame) {
115115
const std::chrono::steady_clock::time_point curTimeStamp = std::chrono::steady_clock::now();
116116
if (curTimeStamp - prevTimeStamp >= std::chrono::milliseconds{1000}) {
117117
prevTimeStamp = curTimeStamp;

demos/common/cpp/utils/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ source_group("include" FILES ${HEADERS})
1010

1111
add_library(utils STATIC ${HEADERS} ${SOURCES})
1212
target_include_directories(utils PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
13-
target_link_libraries(utils PRIVATE gflags ${InferenceEngine_LIBRARIES} opencv_core opencv_imgcodecs opencv_videoio)
13+
target_link_libraries(utils PRIVATE gflags openvino::runtime ${InferenceEngine_LIBRARIES} opencv_core opencv_imgcodecs opencv_videoio)

0 commit comments

Comments
 (0)