import gradio as gr import os import cv2 def inference(file, mask, model, alpha_influence, segmentation_strength): im = cv2.imread(file, cv2.IMREAD_COLOR) cv2.imwrite(os.path.join("input.png"), im) from rembg import new_session, remove input_path = 'input.png' output_path = 'output.png' mask_path = 'mask.png' with open(input_path, 'rb') as i: with open(output_path, 'wb') as o: with open(mask_path, 'wb') as m: input = i.read() output = remove( input, session=new_session(model), only_mask=(True if mask == "Mask only" else False), alpha=alpha_influence, bg_color=(0, 0, 0, segmentation_strength) ) o.write(output) m.write(output) return os.path.join("output.png"), os.path.join("mask.png") title = "RemBG_Super" description = "Gradio demo for RemBG. To use it, simply upload your image and adjust the alpha influence and segmentation strength." article = "

Github Repo

" def show_processed_image(output_image_path): output_image = cv2.imread(output_image_path) return output_image def show_processed_mask(mask_image_path): mask_image = cv2.imread(mask_image_path) return mask_image iface = gr.Interface( inference, [ gr.inputs.Image(type="filepath", label="Input"), gr.inputs.Radio( [ "Default", "Mask only" ], type="value", default="Default", label="Choices" ), gr.inputs.Dropdown([ "u2net", "u2netp", "u2net_human_seg", "u2net_cloth_seg", "silueta", "isnet-general-use", "isnet-anime", "sam", ], type="value", default="isnet-general-use", label="Models" ), gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.5, label="Alpha Influence"), gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.5, label="Segmentation Strength"), ], [ gr.outputs.Image(type="plot", label="Processed Image"), gr.outputs.Image(type="plot", label="Processed Mask"), ], title=title, description=description, article=article, examples=[["lion.png", "Default", "u2net", 0.5, 0.5], ["girl.jpg", "Default", "u2net", 0.5, 0.5], ["anime-girl.jpg", "Default", "isnet-anime", 0.5, 0.5]], enable_queue=True ) iface.launch()