Spaces:
Runtime error
Runtime error
File size: 3,498 Bytes
395d300 b7d88cf 395d300 d5cfdf1 395d300 b7d88cf 395d300 d5cfdf1 395d300 d5cfdf1 395d300 d78befe 395d300 d5cfdf1 395d300 d5cfdf1 395d300 d5cfdf1 395d300 d5cfdf1 395d300 3dfc879 d5cfdf1 395d300 d5cfdf1 58db293 7b45d0b 395d300 d78befe 58db293 d78befe d5cfdf1 395d300 d5cfdf1 395d300 d5cfdf1 395d300 ce90b79 8e69757 d5cfdf1 395d300 d5cfdf1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
import gradio as gr
import os
os.system("mim install mmengine")
os.system('mim install "mmcv>=2.0.0"')
os.system("mim install mmdet")
import cv2
from PIL import Image
import numpy as np
import zipfile
import shutil
from animeinsseg import AnimeInsSeg, AnimeInstances
from animeinsseg.anime_instances import get_color
if not os.path.exists("models"):
os.mkdir("models")
os.system("huggingface-cli lfs-enable-largefiles .")
os.system(
"git clone https://huggingface.co/dreMaz/AnimeInstanceSegmentation models/AnimeInstanceSegmentation"
)
ckpt = r"models/AnimeInstanceSegmentation/rtmdetl_e60.ckpt"
mask_thres = 0.7
instance_thres = 0.3
refine_kwargs = {
"refine_method": "refinenet_isnet"
} # set to None if not using refinenet
# refine_kwargs = None
net = AnimeInsSeg(ckpt, mask_thr=mask_thres, refine_kwargs=refine_kwargs)
def fn(image):
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
instances: AnimeInstances = net.infer(
img, output_type="numpy", pred_score_thr=instance_thres
)
# 创建一个临时文件夹,用来存放每个人物的去除背景的图片
temp_dir = "outputs"
# 删除临时文件夹,避免占用空间
if os.path.isdir(temp_dir):
shutil.rmtree(temp_dir)
os.makedirs(temp_dir, exist_ok=True)
images = []
# instances.bboxes, instances.masks will be None, None if no obj is detected
if instances.bboxes is None:
return None
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
for ii, (xywh, mask) in enumerate(zip(instances.bboxes, instances.masks)):
# 创建一个空白的白色图像,和原图大小一致
white = np.full_like(img2, 255)
# 把mask转换为bool类型,方便后续操作
mask = mask.astype(np.bool_)
# 对掩码进行高斯模糊,平滑边缘
mask_smoothed = cv2.GaussianBlur(mask.astype(np.float32), (3, 3), 0)
# 用原图中对应的区域替换白色图像中的区域,实现去除背景的效果
white[mask_smoothed > 0.5] = img2[mask_smoothed > 0.5]
# 给每个人物编号,然后用cv2.imwrite函数来保存图片到文件夹中
filename = f"person_{ii+1}.png"
filepath = os.path.join(temp_dir, filename)
images.append(white)
cv2.imwrite(filepath, white[..., ::-1])
# 创建一个压缩包,然后用zipfile.write函数来把文件夹中的所有图片添加到压缩包中
zip_name = "persons.zip"
zip_path = os.path.join(temp_dir, zip_name)
with zipfile.ZipFile(zip_path, "w") as zf:
for file in os.listdir(temp_dir):
if file != zip_name:
zf.write(os.path.join(temp_dir, file), file)
# 返回一个图片文件和一个压缩包文件
return images, zip_path
# 在gr.Interface中添加outputs参数,把fn函数的输出作为一个列表传入
iface = gr.Interface(
# design titles and text descriptions
title="Anime Subject Instance Segmentation",
fn=fn,
inputs=gr.Image(type="numpy"),
outputs=[
gr.Gallery(
label="Anime Subject Instance Segmentation",
show_label=False,
elem_id="gallery",
columns=[4],
rows=[4],
object_fit="contain",
height="auto",
),
gr.File(type="filepath", label="Download Zip"),
], # 添加一个压缩包组件,给它一个名称
examples=["1562990.jpg", "612989.jpg", "sample_3.jpg"],
)
iface.launch() |