Fablezeros1/BRIA-RMBG-2.0
0
1import os2import gradio as gr3from gradio_imageslider import ImageSlider4from loadimg import load_img5import spaces6from transformers import AutoModelForImageSegmentation7import torch8from torchvision import transforms9 10torch.set_float32_matmul_precision(["high", "highest"][0])11 12birefnet = AutoModelForImageSegmentation.from_pretrained(13 "briaai/RMBG-2.0", trust_remote_code=True14)15birefnet.to("cuda")16transform_image = transforms.Compose(17 [18 transforms.Resize((1024, 1024)),19 transforms.ToTensor(),20 transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),21 ]22)23 24output_folder = 'output_images'25if not os.path.exists(output_folder):26 os.makedirs(output_folder)27 28def fn(image):29 im = load_img(image, output_type="pil")30 im = im.convert("RGB")31 origin = im.copy()32 image = process(im) 33 image_path = os.path.join(output_folder, "no_bg_image.png")34 image.save(image_path)35 return (image, origin), image_path36 37@spaces.GPU38def process(image):39 image_size = image.size40 input_images = transform_image(image).unsqueeze(0).to("cuda")41 # Prediction42 with torch.no_grad():43 preds = birefnet(input_images)[-1].sigmoid().cpu()44 pred = preds[0].squeeze()45 pred_pil = transforms.ToPILImage()(pred)46 mask = pred_pil.resize(image_size)47 image.putalpha(mask)48 return image49 50def process_file(f):51 name_path = f.rsplit(".",1)[0]+".png"52 im = load_img(f, output_type="pil")53 im = im.convert("RGB")54 transparent = process(im)55 transparent.save(name_path)56 return name_path57 58slider1 = ImageSlider(label="RMBG-2.0", type="pil")59slider2 = ImageSlider(label="RMBG-2.0", type="pil")60image = gr.Image(label="Upload an image")61image2 = gr.Image(label="Upload an image",type="filepath")62text = gr.Textbox(label="Paste an image URL")63png_file = gr.File(label="output png file")64 65 66chameleon = load_img("giraffe.jpg", output_type="pil")67 68url = "http://farm9.staticflickr.com/8488/8228323072_76eeddfea3_z.jpg"69 70tab1 = gr.Interface(71 fn, inputs=image, outputs=[slider1, gr.File(label="output png file")], examples=[chameleon], api_name="image"72)73 74tab2 = gr.Interface(fn, inputs=text, outputs=[slider2, gr.File(label="output png file")], examples=[url], api_name="text")75tab3 = gr.Interface(process_file, inputs=image2, outputs=png_file, examples=["giraffe.jpg"], api_name="png")76 77 78demo = gr.TabbedInterface(79 [tab1, tab2], ["input image", "input url"], title = (80 "RMBG-2.0 for background removal <br>"81 "<span style='font-size:16px; font-weight:300;'>"82 "Background removal model developed by "83 "<a href='https://bria.ai' target='_blank'>BRIA.AI</a>, trained on a carefully selected dataset,<br> "84 "and is available as an open-source model for non-commercial use.</span><br>"85 "<span style='font-size:16px; font-weight:500;'> For testing upload your image and wait.<br>"86 "<a href='https://go.bria.ai/3ZCBTLH' target='_blank'>Commercial use license</a> | "87 "<a href='https://huggingface.co/briaai/RMBG-2.0' target='_blank'>Model card</a> | "88 "<a href='https://blog.bria.ai/brias-new-state-of-the-art-remove-background-2.0-outperforms-the-competition' target='_blank'>Blog</a>"89 "</span><br>"90 "<span style='font-size:16px; font-weight:300;'>"91 "API Endpoint available on: "92 "<a href='https://platform.bria.ai/console/api/image-editing' target='_blank'>Bria.ai</a>, "93 "<a href='https://fal.ai/models/fal-ai/bria/background/remove' target='_blank'>fal.ai</a><br>"94 "ComfyUI node is available here: "95 "<a href='https://github.com/Bria-AI/ComfyUI-BRIA-API' target='_blank'>ComfyUI Node</a><br>"96 "Purchase commercial weigths for commercial use: "97 "<a href='https://go.bria.ai/3D5EGp0' target='_blank'>here</a>"98 "</span>"99)100 101 102 103)104 105if __name__ == "__main__":106 demo.launch(show_error=True)107 