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

这篇具有很好参考价值的文章主要介绍了Python使用AI animegan2-pytorch制作属于你的漫画头像/风景图片。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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模型如下:
python animegan,Python进阶,Python OpenCV,图像处理,人工智能,python,pytorch,漫画头像/风景,animegan2

效果图v1 512模型如下:
python animegan,Python进阶,Python OpenCV,图像处理,人工智能,python,pytorch,漫画头像/风景,animegan2

效果图v1 效果不太好如下:
python animegan,Python进阶,Python OpenCV,图像处理,人工智能,python,pytorch,漫画头像/风景,animegan2

效果图rece如下
人物会有一种病态的美,过于白了,风景上效果更好一些;
人物与photo2cartoon的效果图有点像;
python animegan,Python进阶,Python OpenCV,图像处理,人工智能,python,pytorch,漫画头像/风景,animegan2

python animegan,Python进阶,Python OpenCV,图像处理,人工智能,python,pytorch,漫画头像/风景,animegan2

效果图paprika 模型如下
人物纹理痕迹太过明显,更适合风景
下一张明兰的效果还不错,不同的模型在不同的图像上也会有些微差别;
python animegan,Python进阶,Python OpenCV,图像处理,人工智能,python,pytorch,漫画头像/风景,animegan2
python animegan,Python进阶,Python OpenCV,图像处理,人工智能,python,pytorch,漫画头像/风景,animegan2

python animegan,Python进阶,Python OpenCV,图像处理,人工智能,python,pytorch,漫画头像/风景,animegan2

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

python animegan,Python进阶,Python OpenCV,图像处理,人工智能,python,pytorch,漫画头像/风景,animegan2
python animegan,Python进阶,Python OpenCV,图像处理,人工智能,python,pytorch,漫画头像/风景,animegan2

origin vs v1Res vs v2Res vs paprikaRes vs celedistillResAll 人物效果对比图如下:
python animegan,Python进阶,Python OpenCV,图像处理,人工智能,python,pytorch,漫画头像/风景,animegan2
python animegan,Python进阶,Python OpenCV,图像处理,人工智能,python,pytorch,漫画头像/风景,animegan2

2. 原理

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

3. 源码

源码及示例文件模型等见资源:https://download.csdn.net/download/qq_40985985/87739198文章来源地址https://www.toymoban.com/news/detail-821137.html

# 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.pt

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)

# 原图VS效果图绘制
# python plot_sample.py

# 获取输入路径的所有图像
import cv2
import imutils
import numpy as np
from imutils import paths

imagePaths = 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): continue
    img = None
    for 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 = imgA
            cv2.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 = imgA
            cv2.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制作属于你的漫画头像/风景图片的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • pytorch快速训练ai作画模型的python代码

    在 PyTorch 中训练 AI 作画模型的基本步骤如下: 准备数据集: 需要准备一个包含许多图像的数据集, 这些图像可以是手绘的或者是真实的图像. 定义模型: 选择一个适当的深度学习模型, 并使用 PyTorch 定义该模型. 例如, 可以使用卷积神经网络 (CNN) 或者生成对抗网络 (GAN). 训练模型

    2024年02月09日
    浏览(45)
  • ChatGPT学python——制作自己的AI模型(一)初步了解

    「作者主页」 :雪碧有白泡泡 「个人网站」 :雪碧的个人网站 「推荐专栏」 : ★ java一站式服务 ★ ★ 前端炫酷代码分享 ★ ★ uniapp-从构建到提升 ★ ★ 从0到英雄,vue成神之路 ★ ★ 解决算法,一个专栏就够了 ★ ★ 架构咱们从0说 ★ ★ 数据流通的精妙之道★ 通过【

    2024年02月14日
    浏览(29)
  • Python-Pytorch框架-实现AI自动瞄准(下)

    OpenCV与Ptorch框架搭建一个利用目标骨骼关键点检测实现AI自动瞄准的娱乐项目(该项目仅供学习OpenCV、Ptorch框架、游戏自动化等参考)。 该项目思路大致分为如下步骤: 利用Pywin32以及OpenCV获取游戏窗口图像 数据集获取(本文为17骨骼关键点) 搭建Ptorch训练框架 利用深度学习

    2023年04月08日
    浏览(32)
  • 如何使用AI帮你制作PPT

    在现代人日益忙碌的生活中,难免需要一些轻松愉快的聊天来放松身心。而现在,有了 ChatGPT,轻松愉快的聊天变得更加智能、有趣且不受时间、地点限制! ChatGPT 是基于 GPT-3.5-turbo 训练模型的智能聊天机器人。它可以应用于广泛的应用场景,例如: 在社交媒体上模拟真实用

    2024年02月08日
    浏览(39)
  • Python制作AI贪吃蛇,很多很多细节、思路都写下来了!

    前提:本文实现AI贪吃蛇自行对战,加上人机对战,读者可再次基础上自行添加电脑VS电脑和玩家VS玩家(其实把人机对战写完,这2个都没什么了,思路都一样) 1.智能模式:电脑自己玩(自己吃食物) 2.人机对战:电脑和人操作(在上步的基础上加一个键盘控制的贪吃蛇即可

    2024年02月05日
    浏览(32)
  • Python+ChatGPT制作了一个AI百宝箱,太实用了!!

    ChatGPT 最近在互联网掀起了一阵热潮,其高度智能化的功能能够给我们现实生活带来诸多的便利,可以帮助你写文章、写报告、写周报、做表格、做策划甚至还会写代码。只要与文字相关的工作,它几乎都能给出一份满意的答卷。 小编趁着有空上去玩了一下,也发现了其中的

    2023年04月17日
    浏览(27)
  • 使用PyTorch开发AI大模型

    在过去的几年里,人工智能(AI)技术的发展迅速,尤其是深度学习(Deep Learning)技术,它已经成为解决许多复杂问题的关键技术之一。PyTorch是一个流行的深度学习框架,它提供了易于使用的API,使得开发人员可以快速地构建和训练AI大模型。在本文中,我们将讨论如何使用PyTor

    2024年02月22日
    浏览(34)
  • 怎么制作AI绘画?学会这几个技巧就能制作AI绘画,这些Python高级必会知识点你能答出来几个

    大家知道AI绘画吗?这是最近很火的一种绘画方式,我有时候能在社交平台上看到别人发出来的图画。问了才知道,这是通过人工智能软件制作出来的,只要提供画面的描述,AI就能根据这些词汇进行创作。所以即使是不会绘画的小伙伴也不用担心啦,我们通过这些软件

    2024年04月16日
    浏览(34)
  • 使用AI自动生成PPT提高制作效率

    在制作PPT方面,很多制作者都会轻易跳进一个怪圈:“我要制作一个关于关爱老人的PPT,该怎么做呢,有模板没有?”这个会涉及很多逻辑需要经过不断的思考,制作PPT要通过很多素材、使用技巧、方法经验的不断积累,然而在短时间内制作高质量的PPT这对初学者来说是件非常

    2024年01月17日
    浏览(46)
  • Stable Diffusion教学 使用Lora制作AI网红 【AI绘画真人教程】

    我选择使用呆瓜一键解压版本 提取码: ketj 然后按这个视频操作【AI绘画】启动器正式发布!一键启动/修复/更新/模型下载管理全支持!_哔哩哔哩_bilibili 下载后解压,直接打开 2.看完说明再下载 (自己安装会出各种状况,不介意!),解压里面的两个压缩包。 打开sd-webui启动

    2024年02月02日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包