|
import numpy as np |
|
import torch |
|
import torch.nn.functional as F |
|
from torchvision.transforms.functional import normalize |
|
|
|
import gradio as gr |
|
from gradio_imageslider import ImageSlider |
|
from briarmbg import BriaRMBG |
|
import PIL |
|
from PIL import Image |
|
from typing import Tuple |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
net=BriaRMBG() |
|
model_path = "./model.pth" |
|
if torch.cuda.is_available(): |
|
net.load_state_dict(torch.load(model_path)) |
|
net=net.cuda() |
|
else: |
|
net.load_state_dict(torch.load(model_path,map_location="cpu")) |
|
net.eval() |
|
|
|
def image_size_by_min_resolution( |
|
image: Image.Image, |
|
resolution: Tuple, |
|
resample=None, |
|
): |
|
w, h = image.size |
|
|
|
image_min = min(w, h) |
|
resolution_min = min(resolution) |
|
|
|
scale_factor = image_min / resolution_min |
|
|
|
resize_to: Tuple[int, int] = ( |
|
int(w // scale_factor), |
|
int(h // scale_factor), |
|
) |
|
return resize_to |
|
|
|
|
|
def resize_image(image): |
|
image = image.convert('RGB') |
|
new_image_size = image_size_by_min_resolution(image=image,resolution=(1024, 1024)) |
|
image = image.resize(new_image_size, Image.BILINEAR) |
|
return image |
|
|
|
|
|
def process(image): |
|
|
|
|
|
print(type(image)) |
|
print(image.shape) |
|
orig_image = Image.fromarray(image) |
|
return [orig_image] |
|
w,h = orig_im_size = orig_image.size |
|
image = resize_image(orig_image) |
|
im_np = np.array(image) |
|
im_tensor = torch.tensor(im_np, dtype=torch.float32).permute(2,0,1) |
|
im_tensor = torch.unsqueeze(im_tensor,0) |
|
im_tensor = torch.divide(im_tensor,255.0) |
|
im_tensor = normalize(im_tensor,[0.5,0.5,0.5],[1.0,1.0,1.0]) |
|
if torch.cuda.is_available(): |
|
im_tensor=im_tensor.cuda() |
|
|
|
|
|
result=net(im_tensor) |
|
|
|
|
|
result = torch.squeeze(F.interpolate(result[0][0], size=(h,w), mode='bilinear') ,0) |
|
ma = torch.max(result) |
|
mi = torch.min(result) |
|
result = (result-mi)/(ma-mi) |
|
|
|
|
|
im_array = (result*255).cpu().data.numpy().astype(np.uint8) |
|
pil_im = Image.fromarray(np.squeeze(im_array)) |
|
|
|
new_im = Image.new("RGBA", pil_im.size, (0,0,0)) |
|
new_im.paste(orig_image, mask=pil_im) |
|
|
|
return [new_im] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
title = "background_removal" |
|
description = "remove image background" |
|
examples = [['./input.jpg'],] |
|
output = ImageSlider(position=0.5,label='Image without background slider-view', type="pil") |
|
demo = gr.Interface(fn=process,inputs="image", outputs=output, examples=examples, title=title, description=description) |
|
|
|
if __name__ == "__main__": |
|
demo.launch(share=False) |