当前位置: 首页 > news >正文

Unity使用AnimeGANv3实现动漫风格化效果(一)

先放上几张效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

AnimeGANv3 地址 https://github.com/TachibanaYoshino/AnimeGANv3.git

Unity主要代码

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using Debug = UnityEngine.Debug;public class AnimeGANv3 : MonoBehaviour
{private InferenceSession session;private string modelPath = Application.streamingAssetsPath + "/model/AnimeGANv3_Hayao_36.onnx";public Texture2D input;private Texture2D output;public RawImage rimgInput;public RawImage rimgOutput;void Start(){var ortEnvInstance = OrtEnv.Instance();string[] aps = ortEnvInstance.GetAvailableProviders();foreach (var ap in aps){Debug.Log(ap);}// 创建会话选项,可以设置使用CPU或GPUvar options = new SessionOptions();// 如果使用GPU,可以设置ExecutionProvider为CUDA(需要安装CUDA和cuDNN,并且ONNX Runtime支持)//options.AppendExecutionProvider_DML(0);// 否则使用CPUoptions.AppendExecutionProvider_CPU();//options.AppendExecutionProvider_CUDA();try{session = new InferenceSession(modelPath, options);}catch (Exception e){Debug.LogError("Failed to load model: " + e.Message);return;}rimgInput.texture = input; }private void Update(){if (Input.GetMouseButtonDown(0)){Stopwatch stopwatch = new Stopwatch();stopwatch.Start();output = RunInference(input);stopwatch.Stop();long lastInferenceTime = stopwatch.ElapsedMilliseconds;// 输出耗时信息Debug.Log($"推理完成!总耗时: {lastInferenceTime}ms");rimgOutput.texture = output;}}public Texture2D RunInference(Texture2D inputTexture){// 将输入纹理转换为模型所需的输入张量// 假设模型输入为[1, 512, 512, 3],且数值范围是[-1,1]或[0,1]?根据原始代码,模型输入是[-1,1]// 首先调整纹理大小到512x512Texture2D resizedTexture = ResizeTexture(inputTexture, 512, 512);// 将Texture2D转换为Tensor<float>var inputTensor = TextureToTensor(resizedTexture);// 创建输入数据容器var inputs = new List<NamedOnnxValue>{NamedOnnxValue.CreateFromTensor("AnimeGANv3_input:0", inputTensor) // 注意:这里的"input_1"需要与模型输入节点名称一致};// 运行推理using (var results = session.Run(inputs)){// 获取输出,假设输出节点名为"output_1"var outputTensor = results.FirstOrDefault().Value as Tensor<float>;// 将输出张量转换为Texture2DTexture2D outputTexture = TensorToTexture(outputTensor);return outputTexture;}}private Texture2D ResizeTexture(Texture2D source, int newWidth, int newHeight){// 调整纹理大小,可以使用双线性滤波RenderTexture rt = RenderTexture.GetTemporary(newWidth, newHeight);RenderTexture.active = rt;Graphics.Blit(source, rt);Texture2D resizedTexture = new Texture2D(newWidth, newHeight);resizedTexture.ReadPixels(new Rect(0, 0, newWidth, newHeight), 0, 0);resizedTexture.Apply();RenderTexture.ReleaseTemporary(rt);return resizedTexture;}private Tensor<float> TextureToTensor(Texture2D texture){// 将Texture2D转换为Tensor<float>,形状为[1, height, width, 3]int width = texture.width;int height = texture.height;var tensor = new DenseTensor<float>(new[] { 1, height, width, 3 });// 获取纹理的像素数据Color32[] pixels = texture.GetPixels32();// 将像素数据填充到张量中,注意颜色通道顺序和归一化for (int y = 0; y < height; y++){for (int x = 0; x < width; x++){int index = y * width + x;// 将颜色从Color32转换为float,并归一化到[-1,1](根据原始代码的输入范围)// 注意:原始代码中输入是[-1,1],所以这里需要将像素值从[0,255]转换为[-1,1]tensor[0, y, x, 0] = (pixels[index].r / 127.5f) - 1.0f;tensor[0, y, x, 1] = (pixels[index].g / 127.5f) - 1.0f;tensor[0, y, x, 2] = (pixels[index].b / 127.5f) - 1.0f;}}return tensor;}private Texture2D TensorToTexture(Tensor<float> tensor){// 假设张量形状为[1, height, width, 3]int height = tensor.Dimensions[1];int width = tensor.Dimensions[2];Texture2D texture = new Texture2D(width, height);Color32[] pixels = new Color32[width * height];for (int y = 0; y < height; y++){for (int x = 0; x < width; x++){// 将张量中的值从[-1,1]转换回[0,255]float r = (tensor[0, y, x, 0] + 1.0f) * 127.5f;float g = (tensor[0, y, x, 1] + 1.0f) * 127.5f;float b = (tensor[0, y, x, 2] + 1.0f) * 127.5f;pixels[y * width + x] = new Color32((byte)r, (byte)g, (byte)b, 255);}}texture.SetPixels32(pixels);texture.Apply();return texture;}void OnDestroy(){session?.Dispose();}
}

最后是我的工程地址

https://github.com/xue-fei/AnimeGANv3-unity.git

http://www.dtcms.com/a/553188.html

相关文章:

  • (七)TRPO 算法 PPO 算法
  • RK3568前置知识
  • 逻辑回归正则化强度实验报告:不同 λ 值对模型系数与泛化能力的影响
  • LeetCode每日一题——反转链表
  • 南京市网站叫企业做的网站可不可以自己改主题
  • 怎么查询网站是谁做的seo查询工具网站
  • 【开源鸿蒙-AVCodec Kit】音视频编解码封装解封装部件介绍,转自开源鸿蒙官媒OpenAtom OpenHarmony
  • 【保姆级教程】MySQL 5.7 彻底卸载与重新安装全流程(附常见问题解决)
  • Debian 安装 Domain Admin
  • Java Maven+lombok+MySql+HikariCP 操作数据库
  • Golang 镜像拉取与 Docker 部署全教程
  • 纯css:一个好玩的按钮边框动态动画
  • html5网站建设基本流程图更改wordpress标签分割符合
  • 山东中讯网站建设专业外贸网站制作价格
  • harbor-从源码理解镜像清理的逻辑实现
  • 为什么安装epel-release
  • Apache Maven 项目的开发指南
  • NET系列算法
  • 基于可视化天气系统demo,基于python+ matplotlib+request爬虫,开发语言python,数据库无,10个可视化界面,需要的可以了联系。
  • 被网站开发公司坑湖北网站设计
  • 可视化视角:AI + 实时流 + 可访问性时代的 3 大改变
  • Rust `std::iter` 深度解析:`Iterator` Trait、适配器与性能
  • MacOS学习笔记
  • 搭建网站程序网站域名和服务器到期
  • 从零开发一款实用插件,掌握VSCode扩展生态核心技术
  • mapbox高阶,使用自定义图层实现雷达扫描效果
  • 上海网站空间租用WordPress渗透思路
  • 邦邦汽服x优湃能源汽车零部件绿色循环中心揭牌暨中保智修新能源技术中心授牌仪式圆满举行
  • 蓝牙钥匙 第30次 蓝牙钥匙在汽车共享与分时租赁场景中的技术创新与实践
  • 百度AI眼镜Pro预售启幕,Snap/微美全息AR眼镜技术领跑掌握市场主动权