找回密码
 立即注册
首页 业界区 安全 分手后,我把你从我的世界删除

分手后,我把你从我的世界删除

肿圬后 2025-5-31 23:29:01
谁没谈过几次恋爱,相处过程难免会发现彼此不合,想把对方删除却苦于没有方法。知道我遇到MI-GAN这个项目。
假设你们之前有这么一张图片:
1.jpeg

现在你想把后者从照片中删除,要么自己P图,要么调用上面的模型。模型的地址我都给你准备好了,你跟着做就好了。
  1. git clone https://github.com/Picsart-AI-Research/MI-GAN/
复制代码
然后在该项目中新建1个test.py的文件,其内容如下:
  1. import cv2
  2. import torch
  3. import numpy as np
  4. from PIL import Image
  5. from lib.model_zoo.migan_inference import Generator as MIGAN
  6. def read_mask(mask_path, invert=False):
  7.     mask = Image.open(mask_path)
  8.     mask = resize(mask, max_size=512, interpolation=Image.NEAREST)
  9.     mask = np.array(mask)
  10.     if len(mask.shape) == 3:
  11.         if mask.shape[2] == 4:
  12.             _r, _g, _b, _a = np.rollaxis(mask, axis=-1)
  13.             mask = np.dstack([_a, _a, _a])
  14.         elif mask.shape[2] == 2:
  15.             _l, _a = np.rollaxis(mask, axis=-1)
  16.             mask = np.dstack([_a, _a, _a])
  17.         elif mask.shape[2] == 3:
  18.             _r, _g, _b = np.rollaxis(mask, axis=-1)
  19.             mask = np.dstack([_r, _r, _r])
  20.     else:
  21.         mask = np.dstack([mask, mask, mask])
  22.     if invert:
  23.         mask = 255 - mask
  24.     mask[mask < 255] = 0
  25.     return Image.fromarray(mask).convert("L")
  26. def resize(image, max_size, interpolation=Image.BICUBIC):
  27.     w, h = image.size
  28.     if w > max_size or h > max_size:
  29.         resize_ratio = max_size / w if w > h else max_size / h
  30.         image = image.resize((int(w * resize_ratio), int(h * resize_ratio)), interpolation)
  31.     return image
  32. def preprocess(img: Image, mask: Image, resolution: int) -> torch.Tensor:
  33.     img = img.resize((resolution, resolution), Image.BICUBIC)
  34.     mask = mask.resize((resolution, resolution), Image.NEAREST)
  35.     img = np.array(img)
  36.     mask = np.array(mask)[:, :, np.newaxis] // 255
  37.     img = torch.Tensor(img).float() * 2 / 255 - 1
  38.     mask = torch.Tensor(mask).float()
  39.     img = img.permute(2, 0, 1).unsqueeze(0)
  40.     mask = mask.permute(2, 0, 1).unsqueeze(0)
  41.     x = torch.cat([mask - 0.5, img * mask], dim=1)
  42.     return x
  43. resolution=256
  44. model = MIGAN(resolution=resolution)
  45. model.load_state_dict(torch.load("migan_256_ffhq.pt",weights_only=True))
  46. model.eval()
  47. img_path = "原图路径"
  48. mask_path = "蒙版图路径"
  49. img = Image.open(img_path).convert("RGB")
  50. img_resized = resize(img, max_size=resolution)
  51. mask = read_mask(mask_path, invert=True)
  52. mask_resized = resize(mask, max_size=resolution, interpolation=Image.NEAREST)
  53. x = preprocess(img_resized, mask_resized, resolution)
  54. with torch.no_grad():
  55.     result_image = model(x)[0]
  56. result_image = (result_image * 0.5 + 0.5).clamp(0, 1) * 255
  57. result_image = result_image.to(torch.uint8).permute(1, 2, 0).detach().to("cpu").numpy()
  58. result_image = cv2.resize(result_image, dsize=img_resized.size, interpolation=cv2.INTER_CUBIC)
  59. mask_resized = np.array(mask_resized)[:, :, np.newaxis] // 255
  60. composed_img = img_resized * mask_resized + result_image * (1 - mask_resized)
  61. composed_img = Image.fromarray(cv2.resize(composed_img, img.size))
  62. composed_img.save("output.png")
复制代码
这里需要准备两张图,一张是原图,另一张是蒙版图,蒙版图以黑白两色为主,其中人物使用白色表示,可以使用画图工具手动绘制。类似如下:
2.png

而模型可以访问模型自行下载,密码是2162,根据需要选择尺寸和类型。
最后运行后的效果如下:
3.jpeg

可以看到成功的把你从我的世界中删除了。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册