RMBG v1.4:最强开源图片背景去除模型,超越大部分闭源模型,已达商用效果,附免费体验链接

项目简介

RMBG v1.4 是最先进的背景去除模型,旨在有效地将前景与背景分离,适用于各种类别和图像类型。该模型已经在精心挑选的数据集上进行了训练,包括:一般库存图片、电子商务、游戏和广告内容,使其适用于商业用例,支持大规模企业内容创作。目前,该模型的准确性、效率和多功能性与主流开源模型相媲美。

试用链接在文章最后

Demo

图片[1]-RMBG v1.4:最强开源图片背景去除模型,超越大部分闭源模型,已达商用效果,附免费体验链接-爆品运营狮

这个开源模型的效果,恐怕比大多数不开源的效果都要好

图片[2]-RMBG v1.4:最强开源图片背景去除模型,超越大部分闭源模型,已达商用效果,附免费体验链接-爆品运营狮

安装

pip install -qr https://huggingface.co/briaai/RMBG-1.4/resolve/main/requirements.txt

用法

加载管道

rom transformers import pipelineimage_path = "https://farm5.staticflickr.com/4007/4322154488_997e69e4cf_z.jpg"pipe = pipeline("image-segmentation", model="briaai/RMBG-1.4", trust_remote_code=True)pillow_mask = pipe(image_path, return_mask = True) # outputs a pillow maskpillow_image = pipe(image_path) # applies mask on input and returns a pillow image

加载模型

from transformers import AutoModelForImageSegmentationmodel = AutoModelForImageSegmentation.from_pretrained("briaai/RMBG-1.4",trust_remote_code=True)def preprocess_image(im: np.ndarray, model_input_size: list) -> torch.Tensor:    if len(im.shape) 3:        im = im[:, :, np.newaxis]    # orig_im_size=im.shape[0:2]    im_tensor = torch.tensor(im, dtype=torch.float32).permute(2,0,1)    im_tensor = F.interpolate(torch.unsqueeze(im_tensor,0), size=model_input_size, mode='bilinear')    image = torch.divide(im_tensor,255.0)    image = normalize(image,[0.5,0.5,0.5],[1.0,1.0,1.0])    return image
def postprocess_image(result: torch.Tensor, im_size: list)-> np.ndarray:    result = torch.squeeze(F.interpolate(result, size=im_size, mode='bilinear') ,0)    ma = torch.max(result)    mi = torch.min(result)    result = (result-mi)/(ma-mi)    im_array = (result*255).permute(1,2,0).cpu().data.numpy().astype(np.uint8)    im_array = np.squeeze(im_array)    return im_array
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")model.to(device)
# prepare inputimage_path = "https://farm5.staticflickr.com/4007/4322154488_997e69e4cf_z.jpg"orig_im = io.imread(image_path)orig_im_size = orig_im.shape[0:2]image = preprocess_image(orig_im, model_input_size).to(device)
# inference result=model(image)
# post processresult_image = postprocess_image(result[0][0], orig_im_size)
# save resultpil_im = Image.fromarray(result_image)no_bg_image = Image.new("RGBA", pil_im.size, (0,0,0,0))orig_image = Image.open(image_path)no_bg_image.paste(orig_image, mask=pil_im)

项目链接

https://huggingface.co/briaai/RMBG-1.4

试用链接:

https://huggingface.co/spaces/briaai/BRIA-RMBG-1.4

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容