Skip to content

Commit 2ae8c13

Browse files
upload mask come back
1 parent bb4fee5 commit 2ae8c13

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This extension aim for connecting [AUTOMATIC1111 Stable Diffusion WebUI](https:/
1818
- `2023/05/22`: [v1.4.1](https://github.com/continue-revolution/sd-webui-segment-anything/releases/tag/v1.4.1) [EditAnything](https://github.com/sail-sg/EditAnything) is ready to use! You can generate random segmentation and copy the output to EditAnything ControlNet.
1919
- `2023/05/29`: [v1.4.2](https://github.com/continue-revolution/sd-webui-segment-anything/releases/tag/v1.4.2) You may now do SAM inference on CPU by checking "Use CPU for SAM". This is for some MAC users who are not able to do SAM inference on GPU. I discourage other users from using this feature because it is significantly slower than CUDA.
2020
- `2023/06/01`: [v1.5.0](https://github.com/continue-revolution/sd-webui-segment-anything/releases/tag/v1.5.0) You may now choose to use local GroundingDINO to bypass C++ problem. See [FAQ](#faq)-1 for more detail.
21+
- `2023/06/04`: [v1.5.1](https://github.com/continue-revolution/sd-webui-segment-anything/releases/tag/v1.5.1) `Upload Mask to ControlNet Inpainting` comes bask in response to [ControlNet inpaint improvement](https://github.com/Mikubill/sd-webui-controlnet/discussions/1464). You should see a new tab beside `AutoSAM` after updating the extension. This feature will again be removed once ControlNet extension has its own uploading feature.
2122

2223
## FAQ
2324

scripts/process_params.py

+30-14
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,38 @@ def get_input_and_mask(self, mask_blur):
6060
class SAMProcessUnit:
6161
def __init__(self, args: Tuple, is_img2img=False):
6262
self.is_img2img = is_img2img
63-
sam_inpaint_args = args[:8]
63+
self.sam_inpaint_unit = SAMInpaintUnit(args, is_img2img)
64+
6465
args = args[8:]
65-
self.sam_inpaint_unit = SAMInpaintUnit(sam_inpaint_args, is_img2img)
66-
67-
self.cnet_seg_output_gallery: List[Dict] = None
68-
self.cnet_seg_enable_copy: bool = False
69-
self.cnet_seg_idx: int = 0
70-
self.cnet_seg_gallery_input: int = 0
71-
args = self.init_cnet_seg_process(args)
66+
self.cnet_seg_output_gallery: List[Dict] = None
67+
self.cnet_seg_enable_copy: bool = False
68+
self.cnet_seg_idx: int = 0
69+
self.cnet_seg_gallery_input: int = 0
70+
self.init_cnet_seg_process(args)
7271

72+
args = args[4:]
7373
self.crop_inpaint_unit = SAMInpaintUnit(args, is_img2img)
74+
75+
args = args[8:]
76+
self.cnet_upload_enable: bool = False
77+
self.cnet_upload_num: int = 0
78+
self.cnet_upload_img_inpaint: Image.Image = None
79+
self.cnet_upload_mask_inpaint: Image.Image = None
80+
self.init_cnet_upload_process(args)
7481

7582

7683
def init_cnet_seg_process(self, args):
77-
csp_args = args[:4]
78-
self.cnet_seg_output_gallery = csp_args[0]
79-
self.cnet_seg_enable_copy = csp_args[1]
80-
self.cnet_seg_idx = csp_args[2]
81-
self.cnet_seg_gallery_input = csp_args[3]
82-
return args[4:]
84+
self.cnet_seg_output_gallery = args[0]
85+
self.cnet_seg_enable_copy = args[1]
86+
self.cnet_seg_idx = args[2]
87+
self.cnet_seg_gallery_input = args[3]
88+
89+
90+
def init_cnet_upload_process(self, args):
91+
self.cnet_upload_enable = args[0]
92+
self.cnet_upload_num = args[1]
93+
self.cnet_upload_img_inpaint = args[2]
94+
self.cnet_upload_mask_inpaint = args[3]
8395

8496

8597
def set_process_attributes(self, p):
@@ -103,6 +115,10 @@ def set_process_attributes(self, p):
103115
cnet_seg_gallery_index += self.cnet_seg_gallery_input
104116
self.set_p_value(p, 'control_net_input_image', self.cnet_seg_idx,
105117
Image.open(self.cnet_seg_output_gallery[cnet_seg_gallery_index]['name']))
118+
119+
if self.cnet_upload_enable and self.cnet_upload_img_inpaint is not None and self.cnet_upload_mask_inpaint is not None:
120+
self.set_p_value(p, 'control_net_input_image', self.cnet_upload_num,
121+
{"image": self.cnet_upload_img_inpaint, "mask": self.cnet_upload_mask_inpaint.convert("L")})
106122

107123

108124
def set_p_value(self, p, attr: str, idx: int, v):

scripts/sam.py

+18
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,24 @@ def layout_show(mode):
733733
crop_category_input, crop_batch_dilation_amt, crop_batch_source_dir, crop_batch_dest_dir,
734734
crop_batch_save_image, crop_batch_save_mask, crop_batch_save_image_with_mask, crop_batch_save_background, *auto_sam_config],
735735
outputs=[crop_batch_progress])
736+
737+
738+
with gr.TabItem(label="Upload Mask to ControlNet Inpainting"):
739+
gr.Markdown("This panel is for those who want to upload mask to ControlNet inpainting. It is not part of the SAM feature. It might be removed someday when ControlNet support uploading image and mask. "
740+
"It serves as a temporarily workaround to overcome the unavailability of image with mask uploading feature in ControlNet extension.")
741+
with gr.Row():
742+
cnet_upload_enable = gr.Checkbox(value=False, label="Enable uploading manually created mask to SAM.")
743+
cnet_upload_num = gr.Radio(value="0", choices=[str(i) for i in range(max_cn_num())], label='ControlNet Inpaint Number', type="index")
744+
with gr.Column(visible=False) as cnet_upload_panel:
745+
cnet_upload_img_inpaint = gr.Image(label="Image for ControlNet Inpaint", show_label=False, source="upload", interactive=True, type="pil")
746+
cnet_upload_mask_inpaint = gr.Image(label="Mask for ControlNet Inpaint", source="upload", interactive=True, type="pil")
747+
cnet_upload_enable.change(
748+
fn=gr_show,
749+
inputs=[cnet_upload_enable],
750+
outputs=[cnet_upload_panel],
751+
show_progress=False)
752+
cnet_upload_process = (cnet_upload_enable, cnet_upload_num, cnet_upload_img_inpaint, cnet_upload_mask_inpaint)
753+
ui_process += cnet_upload_process
736754

737755
with gr.Row():
738756
switch = gr.Button(value="Switch to Inpaint Upload")

0 commit comments

Comments
 (0)