分手后,我把你从我的世界删除
谁没谈过几次恋爱,相处过程难免会发现彼此不合,想把对方删除却苦于没有方法。知道我遇到MI-GAN这个项目。假设你们之前有这么一张图片:
现在你想把后者从照片中删除,要么自己P图,要么调用上面的模型。模型的地址我都给你准备好了,你跟着做就好了。
git clone https://github.com/Picsart-AI-Research/MI-GAN/然后在该项目中新建1个test.py的文件,其内容如下:
import cv2
import torch
import numpy as np
from PIL import Image
from lib.model_zoo.migan_inference import Generator as MIGAN
def read_mask(mask_path, invert=False):
mask = Image.open(mask_path)
mask = resize(mask, max_size=512, interpolation=Image.NEAREST)
mask = np.array(mask)
if len(mask.shape) == 3:
if mask.shape == 4:
_r, _g, _b, _a = np.rollaxis(mask, axis=-1)
mask = np.dstack()
elif mask.shape == 2:
_l, _a = np.rollaxis(mask, axis=-1)
mask = np.dstack()
elif mask.shape == 3:
_r, _g, _b = np.rollaxis(mask, axis=-1)
mask = np.dstack()
else:
mask = np.dstack()
if invert:
mask = 255 - mask
mask = 0
return Image.fromarray(mask).convert("L")
def resize(image, max_size, interpolation=Image.BICUBIC):
w, h = image.size
if w > max_size or h > max_size:
resize_ratio = max_size / w if w > h else max_size / h
image = image.resize((int(w * resize_ratio), int(h * resize_ratio)), interpolation)
return image
def preprocess(img: Image, mask: Image, resolution: int) -> torch.Tensor:
img = img.resize((resolution, resolution), Image.BICUBIC)
mask = mask.resize((resolution, resolution), Image.NEAREST)
img = np.array(img)
mask = np.array(mask)[:, :, np.newaxis] // 255
img = torch.Tensor(img).float() * 2 / 255 - 1
mask = torch.Tensor(mask).float()
img = img.permute(2, 0, 1).unsqueeze(0)
mask = mask.permute(2, 0, 1).unsqueeze(0)
x = torch.cat(, dim=1)
return x
resolution=256
model = MIGAN(resolution=resolution)
model.load_state_dict(torch.load("migan_256_ffhq.pt",weights_only=True))
model.eval()
img_path = "原图路径"
mask_path = "蒙版图路径"
img = Image.open(img_path).convert("RGB")
img_resized = resize(img, max_size=resolution)
mask = read_mask(mask_path, invert=True)
mask_resized = resize(mask, max_size=resolution, interpolation=Image.NEAREST)
x = preprocess(img_resized, mask_resized, resolution)
with torch.no_grad():
result_image = model(x)
result_image = (result_image * 0.5 + 0.5).clamp(0, 1) * 255
result_image = result_image.to(torch.uint8).permute(1, 2, 0).detach().to("cpu").numpy()
result_image = cv2.resize(result_image, dsize=img_resized.size, interpolation=cv2.INTER_CUBIC)
mask_resized = np.array(mask_resized)[:, :, np.newaxis] // 255
composed_img = img_resized * mask_resized + result_image * (1 - mask_resized)
composed_img = Image.fromarray(cv2.resize(composed_img, img.size))
composed_img.save("output.png")这里需要准备两张图,一张是原图,另一张是蒙版图,蒙版图以黑白两色为主,其中人物使用白色表示,可以使用画图工具手动绘制。类似如下:
而模型可以访问模型自行下载,密码是2162,根据需要选择尺寸和类型。
最后运行后的效果如下:
可以看到成功的把你从我的世界中删除了。
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]