Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to ignore ControlNet input mask in non-inpaint modes #1914

Merged
merged 1 commit into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions scripts/controlnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,10 @@ def choose_input_image(
else:
input_image = HWC3(image['image'])

have_mask = 'mask' in image and not ((image['mask'][:, :, 0] == 0).all() or (image['mask'][:, :, 0] == 255).all())
have_mask = 'mask' in image and not (
(image['mask'][:, :, 0] <= 5).all() or
(image['mask'][:, :, 0] >= 250).all()
)

if 'inpaint' in unit.module:
logger.info("using inpaint as input")
Expand All @@ -639,7 +642,7 @@ def choose_input_image(
alpha = np.zeros_like(color)[:, :, 0:1]
input_image = np.concatenate([color, alpha], axis=2)
else:
if have_mask:
if have_mask and not shared.opts.data.get("controlnet_ignore_noninpaint_mask", False):
logger.info("using mask as input")
input_image = HWC3(image['mask'][:, :, 0])
unit.module = 'none' # Always use black bg and white line
Expand Down Expand Up @@ -1056,6 +1059,9 @@ def on_ui_settings():
False, "Disable control type selection", gr.Checkbox, {"interactive": True}, section=section))
shared.opts.add_option("controlnet_disable_openpose_edit", shared.OptionInfo(
False, "Disable openpose edit", gr.Checkbox, {"interactive": True}, section=section))
shared.opts.add_option("controlnet_ignore_noninpaint_mask", shared.OptionInfo(
False, "Ignore mask on ControlNet input image if control type is not inpaint",
gr.Checkbox, {"interactive": True}, section=section))


batch_hijack.instance.do_hijack()
Expand Down
14 changes: 7 additions & 7 deletions scripts/controlnet_ui/controlnet_ui_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,17 +562,17 @@ def run_annotator(image, module, pres, pthr_a, pthr_b, t2i_w, t2i_h, pp, rm):
*self.openpose_editor.update(''),
)

img = HWC3(image["image"])
if not (
(image["mask"][:, :, 0] == 0).all()
or (image["mask"][:, :, 0] == 255).all()
):
img = HWC3(image["mask"][:, :, 0])

img = HWC3(image["image"])
has_mask = not (
(image["mask"][:, :, 0] <= 5).all()
or (image["mask"][:, :, 0] >= 250).all()
)
if "inpaint" in module:
color = HWC3(image["image"])
alpha = image["mask"][:, :, 0:1]
img = np.concatenate([color, alpha], axis=2)
elif has_mask and not shared.opts.data.get("controlnet_ignore_noninpaint_mask", False):
img = HWC3(image["mask"][:, :, 0])

module = global_state.get_module_basename(module)
preprocessor = self.preprocessors[module]
Expand Down