521xueweihan / HelloGitHub

:octocat: 分享 GitHub 上有趣、入门级的开源项目。Share interesting, entry-level open source projects on GitHub.
https://hellogithub.com
90.59k stars 9.56k forks source link

项目自荐 | TimeWarp - 在线视频背景替换 #1663

Closed Charmve closed 3 years ago

Charmve commented 3 years ago

项目推荐

image


👉 See image results with interactive zoom-in

✨ Features

示例代码:

import torch
from torchvision.transforms.functional import to_tensor, to_pil_image
from PIL import Image
model = torch.jit.load('model/TorchScript/torchscript_resnet50_fp32.pth').eval() 
# RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a # # CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.
# model = torch.load('model/TorchScript/torchscript_resnet50_fp32.pth',map_location ='cpu')
src = Image.open('dataset/img/12.png')
bgr = Image.open('dataset/bgr/12.png')
src = to_tensor(src).unsqueeze(0)
bgr = to_tensor(bgr).unsqueeze(0)
if src.size(2) <= 2048 and src.size(3) <= 2048:
  model.backbone_scale = 1/4
  model.refine_sample_pixels = 80_000
else:
  model.backbone_scale = 1/8
  model.refine_sample_pixels = 320_000
pha, fgr = model(src, bgr)[:2]
com = pha * fgr + (1 - pha) * torch.tensor([120/255, 255/255, 155/255], device='cpu').view(1, 3, 1, 1)
to_pil_image(com[0].cpu())
to_pil_image(pha[0].cpu()).save('result/pha.png')
to_pil_image(fgr[0].cpu()).save('result/fgr.png')
to_pil_image(com[0].cpu()).save('result/com.png')

截图


demo Real-Time High-Resolution Background Matting[1]
521xueweihan commented 3 years ago

非常感谢您推荐项目。

该项目暂不能收录到 HelloGitHub 月刊中,HelloGitHub 推荐项目审核标准 #271。 期待持续完善该项目,后续推荐更多的项目。

再次感谢您对 HelloGitHub 的支持 🙏