Skip to content

Commit ccaf313

Browse files
author
Dmitry Sidnev
committed
Fixes
1 parent eeaba33 commit ccaf313

File tree

5 files changed

+51
-63
lines changed

5 files changed

+51
-63
lines changed

demos/background_subtraction_demo/python/README.md

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ The demo workflow is the following:
3333
1. The demo application reads image/video frames one by one, resizes them to fit into the input image blob of the network (`image`).
3434
2. The demo visualizes the resulting background subtraction. Certain command-line options affect the visualization:
3535
* If you specify `--target_bgr`, background will be replaced by a chosen image or video. By default background replaced by green field.
36-
* If you specify `--blur_bgr`, background will be slightly blurred to make the result image more natural.
37-
* If you specify `--show_with_original_frame`, the result image will be merged with an original input image.
36+
* If you specify `--blur_bgr`, background will be blurred according to a set value. By default equal to zero and is not applied.
37+
* If you specify `--show_with_original_frame`, the result image will be merged with an input one.
3838

3939
> **NOTE**: By default, Open Model Zoo demos expect input with BGR channels order. If you trained your model to work with RGB order, you need to manually rearrange the default channels order in the demo application or reconvert your model using the Model Optimizer tool with the `--reverse_input_channels` argument specified. For more information about the argument, refer to **When to Reverse Input Channels** section of [Converting a Model Using General Conversion Parameters](https://docs.openvino.ai/latest/openvino_docs_MO_DG_prepare_model_convert_model_Converting_Model.html#general-conversion-parameters).
4040
@@ -68,18 +68,20 @@ omz_converter --list models.lst
6868
Run the application with the `-h` option to see the following usage message:
6969

7070
```
71-
sage: background_subtraction_demo.py [-h] -m MODEL
72-
[--adapter {openvino,remote}] -i INPUT
73-
[-d DEVICE] [-pt "<num>"]
74-
[--keep_aspect_ratio] [--labels LABELS]
75-
[--target_bgr TARGET_BGR] [--blur_bgr]
76-
[-nireq NUM_INFER_REQUESTS]
77-
[-nstreams NUM_STREAMS]
78-
[-nthreads NUM_THREADS] [--loop]
79-
[-o OUTPUT] [-limit OUTPUT_LIMIT]
80-
[--no_show] [--show_with_original_frame]
81-
[--output_resolution OUTPUT_RESOLUTION]
82-
[-u UTILIZATION_MONITORS] [-r]
71+
usage: background_subtraction_demo.py [-h] -m MODEL
72+
[--adapter {openvino,remote}] -i INPUT
73+
[-d DEVICE] [-t PROB_THRESHOLD]
74+
[--resize_type {crop,standard,fit_to_window,fit_to_window_letterbox}]
75+
[--labels LABELS]
76+
[--target_bgr TARGET_BGR]
77+
[--blur_bgr BLUR_BGR]
78+
[-nireq NUM_INFER_REQUESTS]
79+
[-nstreams NUM_STREAMS]
80+
[-nthreads NUM_THREADS] [--loop]
81+
[-o OUTPUT] [-limit OUTPUT_LIMIT]
82+
[--no_show] [--show_with_original_frame]
83+
[--output_resolution OUTPUT_RESOLUTION]
84+
[-u UTILIZATION_MONITORS] [-r]
8385
8486
Options:
8587
-h, --help Show this help message and exit.
@@ -97,15 +99,18 @@ Options:
9799
GPU, HDDL or MYRIAD is acceptable. The demo will look
98100
for a suitable plugin for device specified. Default
99101
value is CPU.
100-
-pt "<num>", --prob_threshold "<num>"
102+
-t PROB_THRESHOLD, --prob_threshold PROB_THRESHOLD
101103
Optional. Probability threshold for detections
102104
filtering.
103-
--keep_aspect_ratio Optional. Force image resize to keep aspect ratio.
105+
--resize_type {crop,standard,fit_to_window,fit_to_window_letterbox}
106+
Optional. A resize type for model preprocess. By
107+
defauld used model predefined type.
104108
--labels LABELS Optional. Labels mapping file.
105109
--target_bgr TARGET_BGR
106110
Optional. Background onto which to composite the
107111
output (by default to green field).
108-
--blur_bgr Optional. Blur background.
112+
--blur_bgr BLUR_BGR Optional. Background blur strength (by default with
113+
value 0 is not applied).
109114
110115
Inference options:
111116
-nireq NUM_INFER_REQUESTS, --num_infer_requests NUM_INFER_REQUESTS
@@ -140,6 +145,7 @@ Input/output options:
140145
141146
Debug options:
142147
-r, --raw_output_message
148+
Optional. Output inference results as mask histogram.
143149
```
144150

145151
Running the application with an empty list of options yields the short version of the usage message and an error message.

demos/background_subtraction_demo/python/background_subtraction_demo.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
sys.path.append(str(Path(__file__).resolve().parents[2] / 'common/python'))
2828
sys.path.append(str(Path(__file__).resolve().parents[2] / 'common/python/openvino/model_zoo'))
2929

30-
from model_api.models import OutputTransform, get_instance_segmentation_model
30+
from model_api.models import MaskRCNNModel, OutputTransform, RESIZE_TYPES, YolactModel
3131
from model_api.performance_metrics import PerformanceMetrics
3232
from model_api.pipelines import get_user_config, AsyncPipeline
3333
from model_api.adapters import create_core, OpenvinoAdapter, RemoteAdapter
@@ -54,17 +54,15 @@ def build_argparser():
5454
help='Optional. Specify the target device to infer on; CPU, GPU, HDDL or MYRIAD is '
5555
'acceptable. The demo will look for a suitable plugin for device specified. '
5656
'Default value is CPU.')
57-
args.add_argument('-pt', '--prob_threshold',
58-
help='Optional. Probability threshold for detections filtering.',
59-
default=0.5, type=float, metavar='"<num>"')
60-
args.add_argument('--keep_aspect_ratio',
61-
help='Optional. Force image resize to keep aspect ratio.',
62-
action='store_true')
57+
args.add_argument('-t', '--prob_threshold', default=0.5, type=float,
58+
help='Optional. Probability threshold for detections filtering.')
59+
args.add_argument('--resize_type', default=None, choices=RESIZE_TYPES.keys(),
60+
help='Optional. A resize type for model preprocess. By defauld used model predefined type.')
6361
args.add_argument('--labels', help='Optional. Labels mapping file.', default=None, type=str)
6462
args.add_argument('--target_bgr', default=None, type=str,
6563
help='Optional. Background onto which to composite the output (by default to green field).')
66-
args.add_argument('--blur_bgr', default=False, action='store_true',
67-
help='Optional. Blur background.')
64+
args.add_argument('--blur_bgr', default=0, type=int,
65+
help='Optional. Background blur strength (by default with value 0 is not applied).')
6866

6967
infer_args = parser.add_argument_group('Inference options')
7068
infer_args.add_argument('-nireq', '--num_infer_requests', help='Optional. Number of infer requests.',
@@ -101,6 +99,15 @@ def build_argparser():
10199
return parser
102100

103101

102+
def get_model(model_adapter, configuration):
103+
inputs = model_adapter.get_input_layers()
104+
outputs = model_adapter.get_output_layers()
105+
if len(inputs) == 1 and len(outputs) == 4 and 'proto' in outputs.keys():
106+
return YolactModel(model_adapter, configuration)
107+
else:
108+
return MaskRCNNModel(model_adapter, configuration)
109+
110+
104111
def print_raw_results(outputs, frame_id):
105112
scores, classes, boxes, masks = outputs
106113
log.debug(' -------------------------- Frame # {} -------------------------- '.format(frame_id))
@@ -125,13 +132,13 @@ def fit_to_window(input_img, output_resolution):
125132
return output
126133

127134

128-
def render_results(frame, objects, output_resolution, target_bgr, person_id, blur_bgr=False, show_with_original_frame=False):
129-
blur_kernel = (11, 11)
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
130137
if target_bgr is None:
131-
target_bgr = cv2.blur(frame, blur_kernel) if blur_bgr else np.full(frame.shape, [155, 255, 120], dtype=np.uint8)
138+
target_bgr = cv2.blur(frame, blur_kernel) if blur_kernel else np.full(frame.shape, [155, 255, 120], dtype=np.uint8)
132139
else:
133140
target_bgr = cv2.resize(target_bgr, (frame.shape[1], frame.shape[0]))
134-
if blur_bgr:
141+
if blur_kernel:
135142
target_bgr = cv2.blur(target_bgr, blur_kernel)
136143
classes, masks = objects[1], objects[3]
137144
# Choose masks only for person class
@@ -175,11 +182,10 @@ def main():
175182

176183
configuration = {
177184
'prob_threshold': args.prob_threshold,
178-
'labels': labels,
179-
'resize_type': 'fit_to_window' if args.keep_aspect_ratio else 'standard'
185+
'resize_type': args.resize_type
180186
}
181187

182-
model = get_instance_segmentation_model(model_adapter, configuration)
188+
model = get_model(model_adapter, configuration)
183189

184190
person_id = -1
185191
for i, label in enumerate(labels):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# This file can be used with the --list option of the model downloader.
2-
# instance-segmentation-person-????
2+
instance-segmentation-person-????
33
yolact-resnet50-fpn-pytorch

demos/common/python/openvino/model_zoo/model_api/models/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from .faceboxes import FaceBoxes
2626
from .hpe_associative_embedding import HpeAssociativeEmbedding
2727
from .image_model import ImageModel
28-
from .instance_segmentation import get_instance_segmentation_model
28+
from .instance_segmentation import MaskRCNNModel, YolactModel
2929
from .model import Model
3030
from .monodepth import MonoDepthModel
3131
from .open_pose import OpenPose
@@ -51,6 +51,7 @@
5151
'HpeAssociativeEmbedding',
5252
'ImageModel',
5353
'InputTransform',
54+
'MaskRCNNModel',
5455
'Model',
5556
'MonoDepthModel',
5657
'OpenPose',
@@ -62,10 +63,10 @@
6263
'SegmentationModel',
6364
'SSD',
6465
'UltraLightweightFaceDetection',
66+
'YolactModel',
6567
'YOLO',
6668
'YoloV3ONNX',
6769
'YoloV4',
6870
'YOLOF',
6971
'YOLOX',
70-
'get_instance_segmentation_model',
7172
]

demos/common/python/openvino/model_zoo/model_api/models/instance_segmentation.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
from .image_model import ImageModel
2121
from .model import WrapperError
22-
from .types import ListValue, NumericalValue
23-
from .utils import load_labels, nms
22+
from .types import NumericalValue
23+
from .utils import nms
2424

2525

2626
class MaskRCNNModel(ImageModel):
@@ -30,11 +30,6 @@ def __init__(self, model_adapter, configuration):
3030
super().__init__(model_adapter, configuration)
3131
self._check_io_number((1, 2), (3, 4, 5, 8))
3232
self.is_segmentoly = len(self.inputs) == 2
33-
if isinstance(self.labels, (list, tuple)):
34-
self.labels = self.labels
35-
else:
36-
self.labels = load_labels(self.labels) if self.labels else None
37-
3833
self.output_blob_name = self._get_outputs()
3934

4035
@classmethod
@@ -45,10 +40,6 @@ def parameters(cls):
4540
default_value=None,
4641
description='Probability threshold for detections filtering'
4742
),
48-
'labels': ListValue(
49-
default_value=None,
50-
description='List of labels'
51-
),
5243
})
5344
return parameters
5445

@@ -160,9 +151,6 @@ class YolactModel(ImageModel):
160151
def __init__(self, model_adapter, configuration):
161152
super().__init__(model_adapter, configuration)
162153
self._check_io_number(1, 4)
163-
if not isinstance(self.labels, (list, tuple)):
164-
self.labels = load_labels(self.labels) if self.labels else None
165-
166154
self.output_blob_name = self._get_outputs()
167155

168156
@classmethod
@@ -173,10 +161,6 @@ def parameters(cls):
173161
default_value=None,
174162
description='Probability threshold for detections filtering'
175163
),
176-
'labels': ListValue(
177-
default_value=None,
178-
description='List of labels'
179-
),
180164
})
181165
return parameters
182166

@@ -302,12 +286,3 @@ def _sanitize_coordinates(_x1, _x2, img_size, shift=0, padding=0):
302286
x1 = np.clip(_x1 - padding, 0, img_size)
303287
x2 = np.clip(_x2 + padding, 0, img_size)
304288
return x1, x2
305-
306-
307-
def get_instance_segmentation_model(model_adapter, configuration):
308-
inputs = model_adapter.get_input_layers()
309-
outputs = model_adapter.get_output_layers()
310-
if len(inputs) == 1 and len(outputs) == 4 and 'proto' in outputs.keys():
311-
return YolactModel(model_adapter, configuration)
312-
else:
313-
return MaskRCNNModel(model_adapter, configuration)

0 commit comments

Comments
 (0)