赞
踩
相片转卡通动漫风格的比较多,而且各大bat厂都有很多方案,很多都能免费用一段时间。市场上应用也算是比较广泛。本质就算通过api请求大厂服务器,然后返回一张风格画的图片。但是对于视频风格化的源码方案非常少。原因有:1:视频对分辨率要求高 ,视频的帧率高很多视频帧率都是30,也就是1秒30张图,运算量非常大。2:视频风格化运算量大,这样时间花费更长,而应用又没那么广泛。图片很多都是做头像娱乐一下使用,视频估计也就能发下抖音,本人打算做搬运。它还可以制作卡通动画吗?!用来制作影视制作后期也是可以的。
本方案能离线在本地电脑运行,算力使用cpu或者gpu.支持视频/图片 4种动漫卡通风格化选择。
【源码在文章的最后,关注订阅可见】
对于人物肖像:
对于风景效果:
由此源码开发的软件叫:mcartoon ,我们来看看这个软件界面和视频风格化的视频效果。
Mcartoon视频转动漫风AI工具使用教程
软件有4种风格可选,对于不同画风的图片,选择合适的风格达到最好的效果。
上代码:
环境:python 3.6+ , tensorflow ,torch.
- import os
- import argparse
-
- from PIL import Image
- import numpy as np
-
- import torch
- from torchvision.transforms.functional import to_tensor, to_pil_image
-
- from model import Generator
-
-
- torch.backends.cudnn.enabled = False
- torch.backends.cudnn.benchmark = False
- torch.backends.cudnn.deterministic = True
-
-
- def load_image(image_path, x32=False):
- img = Image.open(image_path).convert("RGB")
-
- if x32:
- def to_32s(x):
- return 256 if x < 256 else x - x % 32
- w, h = img.size
- img = img.resize((to_32s(w), to_32s(h)))
-
- return img
-
-
- def test(args):
- device = args.device
-
- net = Generator()
- net.load_state_dict(torch.load(args.checkpoint, map_location="cpu"))
- net.to(device).eval()
- print(f"model loaded: {args.checkpoint}")
-
- os.makedirs(args.output_dir, exist_ok=True)
-
- for image_name in sorted(os.listdir(args.input_dir)):
- if os.path.splitext(image_name)[-1].lower() not in [".jpg", ".png", ".bmp", ".tiff"]:
- continue
-
- image = load_image(os.path.join(args.input_dir, image_name), args.x32)
-
- with torch.no_grad():
- image = to_tensor(image).unsqueeze(0) * 2 - 1
- out = net(image.to(device), args.upsample_align).cpu()
- out = out.squeeze(0).clip(-1, 1) * 0.5 + 0.5
- out = to_pil_image(out)
-
- out.save(os.path.join(args.output_dir, image_name))
- print(f"image saved: {image_name}")
-
-
- if __name__ == '__main__':
-
- parser = argparse.ArgumentParser()
- parser.add_argument(
- '--checkpoint',
- type=str,
- default='./weights/paprika.pt',
- )
- parser.add_argument(
- '--input_dir',
- type=str,
- default='./samples/inputs',
- )
- parser.add_argument(
- '--output_dir',
- type=str,
- default='./samples/results',
- )
- parser.add_argument(
- '--device',
- type=str,
- default='cuda:0',
- )
- parser.add_argument(
- '--upsample_align',
- type=bool,
- default=False,
- help="Align corners in decoder upsampling layers"
- )
- parser.add_argument(
- '--x32',
- action="store_true",
- help="Resize images to multiple of 32"
- )
- args = parser.parse_args()
-
- test(args)

完整的,源码地址:
https://github.com/bryandlee/animegan2-pytorch
这样,你就可以选择图片 完成4种风格转换。风格转换时选择不同的模型。
如果你需要转换影片,请看我提供的源码:这里视频操作使用的
moviepy。我们在handle_frame里面处理每一帧的花画,然后每一帧处理完就是一个视频啦。
- def handle_frame(image_frame):
- print(f"model shape: {image_frame.shape}")
- device = "cpu" if not use_gpu.get() else "cuda:0"
- with torch.no_grad():
- image = to_tensor(image_frame).unsqueeze(0) * 2 - 1
- out = net(image.to(device), False).cpu()
- out = out.squeeze(0).clip(-1, 1) * 0.5 + 0.5
- out = to_pil_image(out)
- out.shape = image_frame.shape
- out.dtype = image_frame.dtype
- results = out
- return results
-
- def videoTo(inputpath , outputpath):
- global device
- global net
- device = "cpu" if not use_gpu.get() else "cuda:0"
-
- net = Generator()
- net.load_state_dict(torch.load(getCheckPoint(), map_location="cpu"))
- net.to(device).eval()
-
- video = VideoFileClip(inputpath)
- result = video.fl_image(handle_frame)
-
- result.write_videofile(outputpath,logger=MyBarLogger())

好啦。我们看看最好的效果。
Mcartoon视频转卡通风格算法演示视频
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。