Python使用AI animegan2-pytorch制作属于你的漫画头像

  • 1. 效果图
  • 2. 原理
  • 3. 源码
  • 参考
git clone https://github.com/bryandlee/animegan2-pytorch
cd ./animegan2-pytorch
python test.py --photo_path images/photo_test.jpg --save_path images/animegan2_result.png

1. 效果图

官方效果图如下:

效果图v2 512模型如下:

效果图v1 512模型如下:

效果图v1 效果不太好如下:

效果图rece如下
人物会有一种病态的美,过于白了,风景上效果更好一些;
人物与photo2cartoon的效果图有点像;

效果图paprika 模型如下
人物纹理痕迹太过明显,更适合风景
下一张明兰的效果还不错,不同的模型在不同的图像上也会有些微差别;

origin vs v1Res vs v2Res vs paprikaRes vs celedistillResAll 风景效果对比图如下:


origin vs v1Res vs v2Res vs paprikaRes vs celedistillResAll 人物效果对比图如下:

2. 原理

人像/风景卡通风格渲染的目标是,在保持原图像 ID 信息和纹理细节的同时,将真实照片转换为卡通风格的非真实感图像。

3. 源码

源码及示例文件模型等见资源:https://download.csdn.net/download/qq_40985985/87739198

# animegan2-pytroch 生成漫画头像或者风景图
# python test.py --checkpoint weights/face_paint_512_v2.pt --input_dir samples/faces/ --device cpu --output_dir samples/resv2
# model loaded: weights/face_paint_512_v2.ptimport os
import argparsefrom PIL import Image
import numpy as npimport torch
from torchvision.transforms.functional import to_tensor, to_pil_imagefrom model import Generatortorch.backends.cudnn.enabled = False
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = Truedef 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 % 32w, h = img.sizeimg = img.resize((to_32s(w), to_32s(h)))return imgdef test(args):device = args.devicenet = 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"]:continueimage = load_image(os.path.join(args.input_dir, image_name), args.x32)with torch.no_grad():image = to_tensor(image).unsqueeze(0) * 2 - 1out = net(image.to(device), args.upsample_align).cpu()out = out.squeeze(0).clip(-1, 1) * 0.5 + 0.5out = 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)
# 原图VS效果图绘制
# python plot_sample.py# 获取输入路径的所有图像
import cv2
import imutils
import numpy as np
from imutils import pathsimagePaths = sorted(list(paths.list_images("samples")))list = [x for x in imagePaths if x.find('inputs') > 0]
print(list)resv1 = [x for x in imagePaths if x.find("resv1") > 0]
resv2 = [x for x in imagePaths if x.find("resv2") > 0]
cele = [x for x in imagePaths if x.find("cele") > 0]
pap = [x for x in imagePaths if x.find("paprika") > 0]img = None
for i in list:if (i.find("ml2.jpg") < 0): continueimg = Nonefor j in resv1:if (j.split("\\")[2].__eq__(i.split("\\")[2])):origin = cv2.imread(i)res = cv2.imread(j)if (origin.shape[0] != res.shape[0] or origin.shape[1] != res.shape[1]):res = cv2.resize(res, (origin.shape[1], origin.shape[0]))# print(origin.shape, res.shape)# print('origin vs ' + j.split("\\")[1].replace("res", "") + 'Res')cv2.imshow('origin vs ' + j.split("\\")[1].replace("res", "") + 'Res',imutils.resize(np.hstack([origin, res]), width=300))if (img is None):img = imutils.resize(np.hstack([origin, res]), width=300)else:imgA = np.vstack([img, imutils.resize(np.hstack([origin, res]), width=300)])img = imgAcv2.imshow('origin vs ' + j.split("\\")[1].replace("res", "") + 'ResAll',img)# cv2.waitKey(0)for j in resv2:if (j.split("\\")[2].__eq__(i.split("\\")[2])):origin = cv2.imread(i)res = cv2.imread(j)if (origin.shape[0] != res.shape[0] or origin.shape[1] != res.shape[1]):res = cv2.resize(res, (origin.shape[1], origin.shape[0]))# cv2.imshow('origin vs ' + j.split("\\")[1].replace("res", "") + 'Res',#            imutils.resize(np.hstack([origin, res]), width=300))if (img is None):img = imutils.resize(np.hstack([origin, res]), width=300)else:imgA = np.vstack([img, imutils.resize(np.hstack([origin, res]), width=300)])img = imgA# cv2.imshow('origin vs ' + j.split("\\")[1].replace("res", "") + 'ResAll',#            img)# cv2.waitKey(0)for j in pap:if (j.split("\\")[2].__eq__(i.split("\\")[2])):# print('--------------\t', i, j)origin = cv2.imread(i)res = cv2.imread(j)if (origin.shape[0] != res.shape[0] or origin.shape[1] != res.shape[1]):res = cv2.resize(res, (origin.shape[1], origin.shape[0]))# print(origin.shape, res.shape)# print('origin vs ' + j.split("\\")[1].replace("res", "") + 'Res')# cv2.imshow('origin vs ' + j.split("\\")[1].replace("res", "") + 'Res',#            imutils.resize(np.hstack([origin, res]), width=300))# list.append(imutils.resize(np.hstack([origin, res]), width=300))if (img is None):img = imutils.resize(np.hstack([origin, res]), width=300)else:imgA = np.vstack([img, imutils.resize(np.hstack([origin, res]), width=300)])img = imgA# cv2.imshow('origin vs ' + j.split("\\")[1].replace("res", "") + 'ResAll',#            img)# cv2.waitKey(0)for j in cele:if (j.split("\\")[2].__eq__(i.split("\\")[2])):# print('--------------\t', i, j)origin = cv2.imread(i)res = cv2.imread(j)if (origin.shape[0] != res.shape[0] or origin.shape[1] != res.shape[1]):res = cv2.resize(res, (origin.shape[1], origin.shape[0]))# print(origin.shape, res.shape)# print('origin vs ' + j.split("\\")[1].replace("res", "") + 'Res')# cv2.imshow('origin vs ' + j.split("\\")[1].replace("res", "") + 'Res',#            imutils.resize(np.hstack([origin, res]), width=300))# list.append(imutils.resize(np.hstack([origin, res]), width=300))if (img is None):img = imutils.resize(np.hstack([origin, res]), width=300)else:imgA = np.vstack([img, imutils.resize(np.hstack([origin, res]), width=300)])img = imgAcv2.imshow('origin vs v1Res vs v2Res vs paprikaRes vs celedistillResAll',img)cv2.waitKey(0)

参考

  • https://alltodata.blog.csdn.net/article/details/125183830
  • https://github.com/bryandlee/animegan2-pytorch

Python使用AI animegan2-pytorch制作属于你的漫画头像/风景图片相关推荐

  1. Python使用AI photo2cartoon制作属于你的漫画头像

    Python使用AI photo2cartoon制作属于你的漫画头像 1. 效果图 2. 原理 3. 源码 参考 git clone https://github.com/minivision-ai/ ...

  2. 用python画卡通图_需要用Python和OpenCV制作一张卡通漫画版的图片

    我正在尝试制作一个能使任何图像看起来像卡通漫画的功能. 这是我到目前为止的代码: import numpy import cv2 __author__ = "Michael Beyeler& ...

  3. 照片变漫画头像如何操作?照片变漫画头像制作方法推荐

    最近发现有好多朋友的头像都是将自己的照片制成的漫画人物,看起来特别可爱有趣,这让很喜欢动漫的我产生了强烈的好奇心!于是我就去询问她们是如何做到的,她们告诉我现在只要借助一些图片特效工具就可以实现照片变 ...

  4. 男生|女生漫画头像怎么制作,分享3种免费制作方法,不用求人

    大家发现没有,最近特别流行卡通漫画头像!一些小伙伴们通过处理自己的照片,把照片制作成漫画头像,让照片看起来更有趣.那么,男生.女生漫画头像怎么制作呢?需要用到哪些工具?今天给大家分享3种免费制作漫画头 ...

  5. python制作ai小说网_【案例分享】使用Python创建AI比你想象的轻松

    您可能对AI领域,主要开发阶段,成就,结果和产品使用感兴趣.有数百个免费源和教程描述使用Python的AI.但是,没有必要浪费你的时间看他们.这里是一个详细的指南,你需要知道在使用Python构建人工 ...

  6. python人工智能的重要性_为什么说Python是AI时代必备技能?

    如今,人工智能作为一项划时代的技术,它将催生时代的变革,带来划时代的机遇. 而Python作为当下最为简洁.优雅的语言,近年来和人工智能一样,地位逐渐上升,甚至被专家称为"AI时代必备技能& ...

  7. 深度学习必备书籍——《Python深度学习 基于Pytorch》

    作为一名机器学习|深度学习的博主,想和大家分享几本深度学习的书籍,让大家更快的入手深度学习,成为AI达人!今天给大家介绍的是:<Python深度学习 基于Pytorch> 文章目录 一.背 ...

  8. 【深度学习】基于Torch的Python开源机器学习库PyTorch卷积神经网络

    [深度学习]基于Torch的Python开源机器学习库PyTorch卷积神经网络 文章目录 1 CNN概述 2 PyTorch实现步骤2.1 加载数据2.2 CNN模型2.3 训练2.4 可视化训练 ...

  9. 【深度学习】基于Torch的Python开源机器学习库PyTorch回归

    [深度学习]基于Torch的Python开源机器学习库PyTorch回归 文章目录1 torch.autograd 2 torch.nn.functional 3 详细的回归DEMO3.1 DATAS ...

最新文章

  1. 马云:“996 是一种巨大的福气”
  2. write_cfgmem 产生存储器配置文件?
  3. Openssl:构建CA的过程并实现web服务基于https访问的网络架构
  4. 【Vegas原创】添加SQL Server Agent作业步骤中的运行身份
  5. 【C语言】输入10个人的成绩,求平均值
  6. 菜鸟教程html图片自动播放,HTML img 标签 | 菜鸟教程
  7. csv java 科学计数法_Java入门笔记1/0(输入与输出)
  8. HP-UX Oracle Ioctl ASYNC_CONFIG error=1
  9. Windows下使用pthread
  10. Mac安装Redis
  11. 文字时钟罗盘动态html代码_文字时钟罗盘动态html代码工具-文字时钟APP最新版下载-游戏窝...
  12. 安装使用 GoldenDict 查词神器 (Windows/Mac/Linux)
  13. html+css+js制作LOL官网,web前端大作业(3个页面+模拟登录+链接)
  14. 矩阵分析之 实矩阵分解(3)Cholesky分解
  15. php 写ps功能,ps的作用是什么
  16. matlab识别不出rep,请教:MATLAB中遗传算法如何处理REP等函数的未识别
  17. 2017年语义理解总结(一)
  18. 2021/3/30前端百度笔试题
  19. 聚米移动广告平台——广告主不可错过的投放选择
  20. 手机扫电脑浏览器页面里的二维码后,电脑中该页面自动跳转,什么原理

热门文章

  1. 如何为LumaQQ添加聊天机器人
  2. 口播神器,基于Edge,微软TTS(text-to-speech)文字转语音免费开源库edge-tts实践(Python3.10)
  3. DHCP option 43是什么
  4. H3C Wlan Option43补充说明
  5. 视频网站如何选择云服务器配置?
  6. SQL中case when then用法
  7. js中 new Date()和Date.now()的区别
  8. docker desktop使用教程(一)
  9. 在线购物系统——设计类图
  10. Knowledge and Hard work