项目简介
RMBG v1.4 是最先进的背景去除模型,旨在有效地将前景与背景分离,适用于各种类别和图像类型。该模型已经在精心挑选的数据集上进行了训练,包括:一般库存图片、电子商务、游戏和广告内容,使其适用于商业用例,支持大规模企业内容创作。目前,该模型的准确性、效率和多功能性与主流开源模型相媲美。
试用链接在文章最后
Demo
这个开源模型的效果,恐怕比大多数不开源的效果都要好
安装pip install -qr https://huggingface.co/briaai/RMBG-1.4/resolve/main/requirements.txt
用法
加载管道
rom transformers import pipeline
image_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)
pillow_image = pipe(image_path)
加载模型
from transformers import AutoModelForImageSegmentation
model = 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)
image_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)
result=model(image)
result_image = postprocess_image(result[0][0], orig_im_size)
pil_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
暂无评论内容