当前位置:   article > 正文

Python相片视频转卡通动漫风格效果源码方案_python视频转卡通模型下载

python视频转卡通模型下载

相片转卡通动漫风格的比较多,而且各大bat厂都有很多方案,很多都能免费用一段时间。市场上应用也算是比较广泛。本质就算通过api请求大厂服务器,然后返回一张风格画的图片。但是对于视频风格化的源码方案非常少。原因有:1:视频对分辨率要求高 ,视频的帧率高很多视频帧率都是30,也就是1秒30张图,运算量非常大。2:视频风格化运算量大,这样时间花费更长,而应用又没那么广泛。图片很多都是做头像娱乐一下使用,视频估计也就能发下抖音,本人打算做搬运。它还可以制作卡通动画吗?!用来制作影视制作后期也是可以的。

本方案能离线在本地电脑运行,算力使用cpu或者gpu.支持视频/图片 4种动漫卡通风格化选择。 

【源码在文章的最后,关注订阅可见】

 对于人物肖像:

对于风景效果:

由此源码开发的软件叫:mcartoon ,我们来看看这个软件界面和视频风格化的视频效果。

Mcartoon视频转动漫风AI工具使用教程

 软件有4种风格可选,对于不同画风的图片,选择合适的风格达到最好的效果。

上代码:

环境:python 3.6+ , tensorflow ,torch.
  1. import os
  2. import argparse
  3. from PIL import Image
  4. import numpy as np
  5. import torch
  6. from torchvision.transforms.functional import to_tensor, to_pil_image
  7. from model import Generator
  8. torch.backends.cudnn.enabled = False
  9. torch.backends.cudnn.benchmark = False
  10. torch.backends.cudnn.deterministic = True
  11. def load_image(image_path, x32=False):
  12. img = Image.open(image_path).convert("RGB")
  13. if x32:
  14. def to_32s(x):
  15. return 256 if x < 256 else x - x % 32
  16. w, h = img.size
  17. img = img.resize((to_32s(w), to_32s(h)))
  18. return img
  19. def test(args):
  20. device = args.device
  21. net = Generator()
  22. net.load_state_dict(torch.load(args.checkpoint, map_location="cpu"))
  23. net.to(device).eval()
  24. print(f"model loaded: {args.checkpoint}")
  25. os.makedirs(args.output_dir, exist_ok=True)
  26. for image_name in sorted(os.listdir(args.input_dir)):
  27. if os.path.splitext(image_name)[-1].lower() not in [".jpg", ".png", ".bmp", ".tiff"]:
  28. continue
  29. image = load_image(os.path.join(args.input_dir, image_name), args.x32)
  30. with torch.no_grad():
  31. image = to_tensor(image).unsqueeze(0) * 2 - 1
  32. out = net(image.to(device), args.upsample_align).cpu()
  33. out = out.squeeze(0).clip(-1, 1) * 0.5 + 0.5
  34. out = to_pil_image(out)
  35. out.save(os.path.join(args.output_dir, image_name))
  36. print(f"image saved: {image_name}")
  37. if __name__ == '__main__':
  38. parser = argparse.ArgumentParser()
  39. parser.add_argument(
  40. '--checkpoint',
  41. type=str,
  42. default='./weights/paprika.pt',
  43. )
  44. parser.add_argument(
  45. '--input_dir',
  46. type=str,
  47. default='./samples/inputs',
  48. )
  49. parser.add_argument(
  50. '--output_dir',
  51. type=str,
  52. default='./samples/results',
  53. )
  54. parser.add_argument(
  55. '--device',
  56. type=str,
  57. default='cuda:0',
  58. )
  59. parser.add_argument(
  60. '--upsample_align',
  61. type=bool,
  62. default=False,
  63. help="Align corners in decoder upsampling layers"
  64. )
  65. parser.add_argument(
  66. '--x32',
  67. action="store_true",
  68. help="Resize images to multiple of 32"
  69. )
  70. args = parser.parse_args()
  71. test(args)

完整的,源码地址:

https://github.com/bryandlee/animegan2-pytorch

这样,你就可以选择图片 完成4种风格转换。风格转换时选择不同的模型。

如果你需要转换影片,请看我提供的源码:这里视频操作使用的

moviepy。我们在handle_frame里面处理每一帧的花画,然后每一帧处理完就是一个视频啦。

  1. def handle_frame(image_frame):
  2. print(f"model shape: {image_frame.shape}")
  3. device = "cpu" if not use_gpu.get() else "cuda:0"
  4. with torch.no_grad():
  5. image = to_tensor(image_frame).unsqueeze(0) * 2 - 1
  6. out = net(image.to(device), False).cpu()
  7. out = out.squeeze(0).clip(-1, 1) * 0.5 + 0.5
  8. out = to_pil_image(out)
  9. out.shape = image_frame.shape
  10. out.dtype = image_frame.dtype
  11. results = out
  12. return results
  13. def videoTo(inputpath , outputpath):
  14. global device
  15. global net
  16. device = "cpu" if not use_gpu.get() else "cuda:0"
  17. net = Generator()
  18. net.load_state_dict(torch.load(getCheckPoint(), map_location="cpu"))
  19. net.to(device).eval()
  20. video = VideoFileClip(inputpath)
  21. result = video.fl_image(handle_frame)
  22. result.write_videofile(outputpath,logger=MyBarLogger())

好啦。我们看看最好的效果。

Mcartoon视频转卡通风格算法演示视频

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/981106
推荐阅读
相关标签
  

闽ICP备14008679号