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

implement batch mode #27

Merged
Merged
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
37 changes: 30 additions & 7 deletions scripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,33 @@
from modules.extensions import extensions_dir

from collections import OrderedDict

from PIL import Image

model_cache = OrderedDict()
sam_model_dir = os.path.join(
extensions_dir, "PBRemTools/models/")
model_list = [f for f in os.listdir(sam_model_dir) if os.path.isfile(
os.path.join(sam_model_dir, f)) and f.split('.')[-1] != 'txt']


def processing(input_image, td_abg_enabled, h_split, v_split, n_cluster, alpha, th_rate, cascadePSP_enabled, fast, psp_L, sa_enabled, seg_query, model_name, predicted_iou_threshold, stability_score_threshold, clip_threshold):
image = pil2cv(input_image)
def processing(single_image, batch_image, input_tab_state, *rem_args):
# 0: single
if (input_tab_state == 0):
processed = process_image(single_image, *rem_args)
return processed
# 1 (or ohter): batch
else:
processed = []
for i in batch_image:
image = Image.open(i)
r = process_image(image, *rem_args)
processed.append(r[0])
processed.append(r[1])
return processed

def process_image(target_image, *rem_args):
image = pil2cv(target_image)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
mask, image = get_foreground(image, td_abg_enabled, h_split, v_split, n_cluster, alpha, th_rate, cascadePSP_enabled, fast, psp_L, sa_enabled, seg_query, model_name, predicted_iou_threshold, stability_score_threshold, clip_threshold)
mask, image = get_foreground(image, *rem_args)
return image, mask

class Script(scripts.Script):
Expand All @@ -47,9 +61,14 @@ def ui(self, is_img2img):

def on_ui_tabs():
with gr.Blocks(analytics_enabled=False) as PBRemTools:
input_tab_state = gr.State(value=0)
with gr.Row():
with gr.Column():
input_image = gr.Image(type="pil")
with gr.Tabs():
with gr.TabItem(label="Single") as input_tab_single:
single_image = gr.Image(type="pil")
with gr.TabItem(label="Batch") as input_tab_batch:
batch_image = gr.File(label="Batch Images", file_count="multiple", interactive=True, type="file")
with gr.Accordion("Mask Setting", open=True):
with gr.Accordion("Segment Anything & CLIP", open=True):
with gr.Accordion("Segment Anything & CLIP", open=True):
Expand Down Expand Up @@ -80,9 +99,13 @@ def on_ui_tabs():
with gr.Row():
with gr.Column():
gallery = gr.Gallery(label="outputs", show_label=True, elem_id="gallery").style(grid=2)

# 0: single 1: batch
input_tab_single.select(fn=lambda: 0, inputs=[], outputs=[input_tab_state])
input_tab_batch.select(fn=lambda: 1, inputs=[], outputs=[input_tab_state])
submit.click(
processing,
inputs=[input_image, td_abg_enabled, h_split, v_split, n_cluster, alpha, th_rate, cascadePSP_enabled, fast, psp_L, sa_enabled, seg_query, model_name, predicted_iou_threshold, stability_score_threshold, clip_threshold],
inputs=[single_image, batch_image, input_tab_state, td_abg_enabled, h_split, v_split, n_cluster, alpha, th_rate, cascadePSP_enabled, fast, psp_L, sa_enabled, seg_query, model_name, predicted_iou_threshold, stability_score_threshold, clip_threshold],
outputs=gallery
)

Expand Down