Skip to content

Commit 6218da6

Browse files
author
Roman Donchenko
authored
Merge pull request #1594 from fzhar/image_inpainting_gui_refactoring_2
Command line and mask processign are changed for image_inpainting_demo.
2 parents 0d74e97 + 326e6db commit 6218da6

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

demos/python_demos/image_inpainting_demo/README.md

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ to fill holes in images.
77
This demo can work in 2 modes:
88

99
* GUI mode: areas for inpainting can be marked interactively using mouse painting
10-
* Auto mode (use -a option for it): image will be processed automatically using randomly applied mask (-r option) or using specific color-based mask (-mc option to set mask color)
10+
* Auto mode (use -ac or -ar option for it): image will be processed automatically using randomly applied mask (-ar option) or using specific color-based mask (-ac option)
1111

1212
Running the application with the `-h` option yields the following usage message:
1313

1414
```
15-
usage: image_inpainting_demo.py [-h] -m MODEL [-i INPUT] [-d DEVICE] [-r]
15+
usage: image_inpainting_demo.py [-h] -m MODEL [-i INPUT] [-d DEVICE]
1616
[-p PARTS] [-mbw MAX_BRUSH_WIDTH]
17-
[-ml MAX_LENGTH] [-mv MAX_VERTEX]
18-
[-mc MASK_COLOR [MASK_COLOR ...]] [--no_show]
19-
[-o OUTPUT] [-a]
17+
[-ml MAX_LENGTH] [-mv MAX_VERTEX] [--no_show]
18+
[-o OUTPUT] [-ac C C C] [-ar]
19+
2020
Options:
2121
-h, --help Show this help message and exit.
2222
-m MODEL, --model MODEL
@@ -28,31 +28,32 @@ Options:
2828
GPU, FPGA, HDDL or MYRIAD is acceptable. The demo will
2929
look for a suitable plugin for device specified.
3030
Default value is CPU
31-
-r, --rnd Optional. Use random mask for inpainting (with
32-
parameters set by -p, -mbw, -mk and -mv).Skipped in
33-
GUI mode
3431
-p PARTS, --parts PARTS
35-
Optional. Number of parts to draw mask. Skipped in GUI
32+
Optional. Number of parts to draw mask. Ignored in GUI
3633
mode
3734
-mbw MAX_BRUSH_WIDTH, --max_brush_width MAX_BRUSH_WIDTH
38-
Optional. Max width of brush to draw mask. Skipped in
35+
Optional. Max width of brush to draw mask. Ignored in
3936
GUI mode
4037
-ml MAX_LENGTH, --max_length MAX_LENGTH
41-
Optional. Max strokes length to draw mask. Skipped in
38+
Optional. Max strokes length to draw mask. Ignored in
4239
GUI mode
4340
-mv MAX_VERTEX, --max_vertex MAX_VERTEX
44-
Optional. Max number of vertex to draw mask. Skipped
41+
Optional. Max number of vertex to draw mask. Ignored
4542
in GUI mode
46-
-mc MASK_COLOR [MASK_COLOR ...], --mask_color MASK_COLOR [MASK_COLOR ...]
47-
Optional. Color to be treated as mask (provide 3 RGB
48-
components in range of 0...255). Default is 0 0 0.
49-
Skipped in GUI mode
50-
--no_show Optional. Don't show output. Cannot be used in GUI mode
43+
--no_show Optional. Don't show output. Cannot be used in GUI
44+
mode
5145
-o OUTPUT, --output OUTPUT
5246
Optional. Save output to the file with provided
53-
filename. Skipped in GUI mode
54-
-a, --auto Optional. Use automatic (non-interactive) mode instead
55-
of GUI
47+
filename. Ignored in GUI mode
48+
-ac C C C, --auto_mask_color C C C
49+
Optional. Use automatic (non-interactive) mode with
50+
color mask.Provide color to be treated as mask (3 RGB
51+
components in range of 0...255). Cannot be used
52+
together with -ar.
53+
-ar, --auto_mask_random
54+
Optional. Use automatic (non-interactive) mode with
55+
random mask for inpainting (with parameters set by -p,
56+
-mbw, -mk and -mv). Cannot be used together with -ac.
5657
```
5758

5859
To run the demo, you can use public or pretrained models. You can download the pretrained models with the OpenVINO™ [Model Downloader](../../../tools/downloader/README.md).

demos/python_demos/image_inpainting_demo/image_inpainting_demo.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ def build_arg_parser():
3333
help="Optional. Specify the target device to infer on; CPU, GPU, FPGA, HDDL or MYRIAD is "
3434
"acceptable. The demo will look for a suitable plugin for device specified. "
3535
"Default value is CPU", default="CPU", type=str)
36-
args.add_argument("-r", "--rnd",
37-
help="Optional. Use random mask for inpainting (with parameters set by -p, -mbw, -mk and -mv)."
38-
" Ignored in GUI mode",
39-
action='store_true')
4036
args.add_argument("-p", "--parts", help="Optional. Number of parts to draw mask. Ignored in GUI mode",
4137
default=8, type=int)
4238
args.add_argument("-mbw", "--max_brush_width", help="Optional. Max width of brush to draw mask."
@@ -46,13 +42,16 @@ def build_arg_parser():
4642
default=100, type=int)
4743
args.add_argument("-mv", "--max_vertex", help="Optional. Max number of vertex to draw mask. Ignored in GUI mode",
4844
default=20, type=int)
49-
args.add_argument("-mc", "--mask_color",
50-
help="Optional. Color to be treated as mask (provide 3 RGB components in range of 0...255)."
51-
" Default is 0 0 0. Ignored in GUI mode", default=[0, 0, 0], type=int, nargs=3)
5245
args.add_argument("--no_show", help="Optional. Don't show output. Cannot be used in GUI mode", action='store_true')
5346
args.add_argument("-o", "--output", help="Optional. Save output to the file with provided filename."
5447
" Ignored in GUI mode", default="", type=str)
55-
args.add_argument("-a", "--auto", help="Optional. Use automatic (non-interactive) mode instead of GUI",
48+
args.add_argument("-ac", "--auto_mask_color", help="Optional. Use automatic (non-interactive) mode with color mask."
49+
"Provide color to be treated as mask (3 RGB components in range of 0...255). "
50+
"Cannot be used together with -ar.",
51+
metavar='C', default=None, type=int, nargs=3)
52+
args.add_argument("-ar", "--auto_mask_random",
53+
help="Optional. Use automatic (non-interactive) mode with random mask for inpainting"
54+
" (with parameters set by -p, -mbw, -mk and -mv). Cannot be used together with -ac.",
5655
action='store_true')
5756

5857
return parser
@@ -82,24 +81,25 @@ def create_random_mask(parts, max_vertex, max_length, max_brush_width, h, w, max
8281
return mask
8382

8483

85-
def inpaint_random_holes(img, args):
86-
mask_color = args.mask_color[::-1] # argument comes in RGB mode, but we will use BGR notation below
87-
84+
def inpaint_auto(img, args):
8885
ie = IECore()
8986

9087
inpainting_processor = ImageInpainting(ie, args.model, args.device)
9188

92-
#--- Resize to model input and generate random mask
93-
img = cv2.resize(img, (inpainting_processor.input_width, inpainting_processor.input_height))
94-
95-
if args.rnd:
89+
#--- Generating mask
90+
if args.auto_mask_random:
9691
mask = create_random_mask(args.parts, args.max_vertex, args.max_length, args.max_brush_width,
9792
inpainting_processor.input_height, inpainting_processor.input_width)
9893
else:
99-
top = np.full(img.shape, mask_color, np.uint8)
100-
mask = cv2.inRange(img, top, top) / 255
94+
# argument comes in RGB mode, but we will use BGR notation below
95+
top = np.full(img.shape, args.auto_mask_color[::-1], np.uint8)
96+
mask = cv2.inRange(img, top, top)
97+
mask = cv2.resize(mask, (inpainting_processor.input_width, inpainting_processor.input_height))
98+
_, mask = cv2.threshold(mask, 1, 1, cv2.THRESH_BINARY)
10199
mask = np.expand_dims(mask, 2)
102100

101+
#--- Resizing image and removing masked areas from it
102+
img = cv2.resize(img, (inpainting_processor.input_width, inpainting_processor.input_height))
103103
masked_image = (img * (1 - mask) + 255 * mask).astype(np.uint8)
104104

105105
#--- Inpaint and show results
@@ -117,9 +117,13 @@ def main():
117117
print("Cannot load image " + args.input)
118118
return -1
119119

120-
if args.auto:
120+
if args.auto_mask_color and args.auto_mask_random:
121+
print("Error: -ar and -ac options cannot be used together...")
122+
return -1
123+
124+
if args.auto_mask_color or args.auto_mask_random:
121125
# Command-line inpaining for just one image
122-
concat_image, result = inpaint_random_holes(img,args)
126+
concat_image, result = inpaint_auto(img,args)
123127
if args.output != "":
124128
cv2.imwrite(args.output, result)
125129
if not args.no_show:

0 commit comments

Comments
 (0)