|
from train import AnimeSegmentation |
|
import cv2 |
|
import numpy as np |
|
from loadimg import load_img |
|
import gradio as gr |
|
|
|
import torch |
|
|
|
model = AnimeSegmentation.from_pretrained("skytnt/anime-seg") |
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
model.eval() |
|
model.to(device) |
|
img_size = model._hub_mixin_config["img_size"] |
|
|
|
|
|
def get_mask(model, input_img, use_amp=True, s=640): |
|
input_img = (input_img / 255).astype(np.float32) |
|
h, w = h0, w0 = input_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(input_img, (w, h)) |
|
img_input = np.transpose(img_input, (2, 0, 1)) |
|
img_input = img_input[np.newaxis, :] |
|
tmpImg = torch.from_numpy(img_input).type(torch.FloatTensor).to(model.device) |
|
with torch.no_grad(): |
|
if use_amp: |
|
with amp.autocast(): |
|
pred = model(tmpImg) |
|
pred = pred.to(dtype=torch.float32) |
|
else: |
|
pred = model(tmpImg) |
|
pred = pred.cpu().numpy()[0] |
|
pred = np.transpose(pred, (1, 2, 0)) |
|
pred = pred[ph // 2:ph // 2 + h, pw // 2:pw // 2 + w] |
|
pred = cv2.resize(pred, (w0, h0))[:, :, np.newaxis] |
|
return pred |
|
|
|
|
|
def process(img): |
|
path = load_img(img,output_type="str") |
|
img = cv2.cvtColor(cv2.imread(path, cv2.IMREAD_COLOR), cv2.COLOR_BGR2RGB) |
|
mask = get_mask(model, img, use_amp= False, s=img_size) |
|
img = np.concatenate((img, mask * img, mask.repeat(3, 2) * 255), axis=1).astype(np.uint8) |
|
out = load_img(img) |
|
return out |
|
|
|
|
|
|
|
|
|
demo = gr.Interface(process,"image","image") |
|
|
|
demo.launch(debug=True) |
|
|