Skip to content

Commit

Permalink
Fooocus mask enhance, mask upload and invert.蒙版增强功能,上传蒙版、反转手涂蒙版 (llly…
Browse files Browse the repository at this point in the history
…asviel#1645)

* Update webui.py

Added a mask upload interface.添加蒙版上传界面。

* Add mask upload feature

Add mask upload feature.添加蒙版上传功能。

* Add mask upload feature

Add mask upload feature.添加蒙版上传功能。

* Try to fix the problem when drawing mates with external enrichment.

Swap the judgment criteria and try to fix the problem when drawing mates with external enrichment.
调换判断条件,尝试修复和外部扩充绘制配合时出现的问题

* 修改注释

* 修改注释

* Update readme.md

更新日志

* Add mask upload check box,

Add check box, default does not show mask upload box, check it and then show it.
添加复选框,默认不显示蒙版上传框,勾选后再显示。

* back to normal

还原

* Try adding a mask inversion feature

Try adding a mask inversion feature.
尝试添加蒙版反转功能。
增加一个复选框来决定是否将蒙版反转。

* Try adding a mask inversion feature

Try adding a mask inversion feature.
尝试添加蒙版反转功能。
增加一个复选框来决定是否将蒙版反转。

* Fixed word errors

* Fix the words of the description
  • Loading branch information
xhoxye authored Jan 2, 2024
1 parent 0c4f20a commit b5163e0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
32 changes: 31 additions & 1 deletion modules/async_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def worker():
import shared
import random
import copy

# xhoxye4
# 导入 OpenCV 库
import cv2
# xhoxye4

import modules.default_pipeline as pipeline
import modules.core as core
import modules.flags as flags
Expand Down Expand Up @@ -138,6 +144,14 @@ def handler(async_task):
uov_input_image = args.pop()
outpaint_selections = args.pop()
inpaint_input_image = args.pop()

# xhoxye5
# 接收参数
inpaint_mask_image = args.pop()
inpaint_mask_image_checkbox = args.pop()
invert_mask_checkbox = args.pop()
# xhoxye5

inpaint_additional_prompt = args.pop()

cn_tasks = {x: [] for x in flags.ip_list}
Expand Down Expand Up @@ -273,7 +287,23 @@ def handler(async_task):
current_tab == 'ip' and advanced_parameters.mixing_image_prompt_and_inpaint)) \
and isinstance(inpaint_input_image, dict):
inpaint_image = inpaint_input_image['image']
inpaint_mask = inpaint_input_image['mask'][:, :, 0]

# xhoxye6
#inpaint_mask = inpaint_input_image['mask'][:, :, 0]
# use uploaded inpaint mask image, if not brush for inpaint.
# 如果没有手涂蒙版,则使用上传蒙版,并缩放。调换判断条件,尝试修复和外部扩充绘制配合时出现的问题.
# 添加反转手涂蒙版的判断
if inpaint_mask_image_checkbox and not np.any(inpaint_input_image['mask'] == [255, 255, 255]) and inpaint_mask_image is not None:
inpaint_height, inpaint_width = inpaint_image.shape[:2]
resized_mask_image = cv2.resize(inpaint_mask_image, (inpaint_width, inpaint_height))

inpaint_mask = resized_mask_image[:, :, 0]
else:
inpaint_mask = inpaint_input_image['mask'][:, :, 0]
if invert_mask_checkbox:
inpaint_mask = np.invert(inpaint_mask)
# xhoxye6

inpaint_image = HWC3(inpaint_image)
if isinstance(inpaint_image, np.ndarray) and isinstance(inpaint_mask, np.ndarray) \
and (np.any(inpaint_mask > 127) or len(outpaint_selections) > 0):
Expand Down
23 changes: 21 additions & 2 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,28 @@ def ip_advance_checked(x):
outputs=ip_ad_cols + ip_types + ip_stops + ip_weights,
queue=False, show_progress=False)
with gr.TabItem(label='Inpaint or Outpaint') as inpaint_tab:
inpaint_input_image = grh.Image(label='Drag above image to here', source='upload', type='numpy', tool='sketch', height=500, brush_color="#FFFFFF", elem_id='inpaint_canvas')
# xhoxye1
#inpaint_input_image = grh.Image(label='Drag above image to here', source='upload', type='numpy', tool='sketch', height=500, brush_color="#FFFFFF", elem_id='inpaint_canvas')

with gr.Row():
inpaint_input_image = grh.Image(label='Drag inpaint or outpaint image to here', source='upload', type='numpy', tool='sketch', height=500, brush_color="#FFFFFF", elem_id='inpaint_canvas')

inpaint_mask_image = grh.Image(label='Drag inpaint mask image to here', source='upload', type='numpy', height=500,visible=False)
# xhoxye1

with gr.Row():
inpaint_additional_prompt = gr.Textbox(placeholder="Describe what you want to inpaint.", elem_id='inpaint_additional_prompt', label='Inpaint Additional Prompt', visible=False)
outpaint_selections = gr.CheckboxGroup(choices=['Left', 'Right', 'Top', 'Bottom'], value=[], label='Outpaint Direction')
inpaint_mode = gr.Dropdown(choices=modules.flags.inpaint_options, value=modules.flags.inpaint_option_default, label='Method')
example_inpaint_prompts = gr.Dataset(samples=modules.config.example_inpaint_prompts, label='Additional Prompt Quick List', components=[inpaint_additional_prompt], visible=False)

# xhoxye2
with gr.TabItem(label='Inpaint advanced') as inpaint_advanced:
inpaint_mask_image_checkbox = gr.Checkbox(label='Enable upload mask', value=False, container=False)
inpaint_mask_image_checkbox.change(lambda x: gr.update(visible=x), inputs=inpaint_mask_image_checkbox, outputs=inpaint_mask_image, queue=False)
invert_mask_checkbox = gr.Checkbox(label='Invert hand-drawn mask', value=False, container=False)
# xhoxye2

gr.HTML('* Powered by Fooocus Inpaint Engine <a href="https://github.com/lllyasviel/Fooocus/discussions/414" target="_blank">\U0001F4D4 Document</a>')
example_inpaint_prompts.click(lambda x: x[0], inputs=example_inpaint_prompts, outputs=inpaint_additional_prompt, show_progress=False, queue=False)
with gr.TabItem(label='Describe') as desc_tab:
Expand Down Expand Up @@ -510,7 +526,10 @@ def inpaint_mode_change(mode):
ctrls += [base_model, refiner_model, refiner_switch] + lora_ctrls
ctrls += [input_image_checkbox, current_tab]
ctrls += [uov_method, uov_input_image]
ctrls += [outpaint_selections, inpaint_input_image, inpaint_additional_prompt]
# xhoxye3
#ctrls += [outpaint_selections, inpaint_input_image, inpaint_additional_prompt]
ctrls += [outpaint_selections, inpaint_input_image, inpaint_mask_image, inpaint_mask_image_checkbox, invert_mask_checkbox, inpaint_additional_prompt]
# xhoxye3
ctrls += ip_ctrls

state_is_generating = gr.State(False)
Expand Down

0 comments on commit b5163e0

Please sign in to comment.