import PIL.Image import gradio as gr import huggingface_hub import onnxruntime as rt import numpy as np import cv2 providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] model_path = huggingface_hub.hf_hub_download("skytnt/anime-seg", "isnetis.onnx") rmbg_model = rt.InferenceSession(model_path, providers=providers) def custom_background(background, foreground): x = (background.size[0] - foreground.size[0]) / 2 y = (background.size[1] - foreground.size[1]) / 2 box = (x, y, foreground.size[0] + x, foreground.size[1] + y) crop = background.crop(box) final_image = crop.copy() # put the foreground in the centre of the background paste_box = (0, final_image.size[1] - foreground.size[1], final_image.size[0], final_image.size[1]) final_image.paste(foreground, paste_box, mask=foreground) return final_image def get_mask(img, s=1024): img = (img / 255).astype(np.float32) h, w = h0, w0 = img.shape[:-1] h, w = (s, int(s * w / h)) if h > w else (int(s * h / w), s) ph, pw = s - h, s - w img_input = np.zeros([s, s, 3], dtype=np.float32) img_input[ph // 2:ph // 2 + h, pw // 2:pw // 2 + w] = cv2.resize(img, (w, h)) img_input = np.transpose(img_input, (2, 0, 1)) img_input = img_input[np.newaxis, :] mask = rmbg_model.run(None, {'img': img_input})[0][0] mask = np.transpose(mask, (1, 2, 0)) mask = mask[ph // 2:ph // 2 + h, pw // 2:pw // 2 + w] mask = cv2.resize(mask, (w0, h0))[:, :, np.newaxis] return mask def predict(img, new_background): mask = get_mask(img) img = (mask * img + 255 * (1 - mask)).astype(np.uint8) mask = (mask * 255).astype(np.uint8) img = np.concatenate([img, mask], axis=2, dtype=np.uint8) mask = mask.repeat(3, axis=2) if new_background is not None: foreground = PIL.Image.fromarray(img) return mask, custom_background(new_background, foreground) return mask, img footer = r"""