C# 人像卡通化 Onnx photo2cartoon

效果

项目

代码

复制代码
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace 人像卡通化
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        string startupPath;
        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;
        string model_path;
        Mat image;
        Mat result_image;
        int modelSize = 256;

        SessionOptions options;
        InferenceSession onnx_session;
        Tensor<float> input_tensor;
        List<NamedOnnxValue> input_ontainer;
        IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;
        DisposableNamedOnnxValue[] results_onnxvalue;

        Tensor<float> result_tensors;
        float[] result_array;

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            textBox1.Text = "";
            image = new Mat(image_path);
            pictureBox2.Image = null;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            textBox1.Text = "";
            pictureBox2.Image = null;

            int oldwidth = image.Cols;
            int oldheight = image.Rows;

            //缩放图片大小
            int maxEdge = Math.Max(image.Rows, image.Cols);
            float ratio = 1.0f * modelSize / maxEdge;
            int newHeight = (int)(image.Rows * ratio);
            int newWidth = (int)(image.Cols * ratio);
            Mat resize_image = image.Resize(new OpenCvSharp.Size(newWidth, newHeight));
            int width = resize_image.Cols;
            int height = resize_image.Rows;
            if (width != modelSize || height != modelSize)
            {
                resize_image = resize_image.CopyMakeBorder(0, modelSize - newHeight, 0, modelSize - newWidth, BorderTypes.Constant, new Scalar(255, 255, 255));
            }

            Cv2.CvtColor(resize_image, resize_image, ColorConversionCodes.BGR2RGB);

            // 输入Tensor
            for (int y = 0; y < resize_image.Height; y++)
            {
                for (int x = 0; x < resize_image.Width; x++)
                {
                    input_tensor[0, 0, y, x] = (resize_image.At<Vec3b>(y, x)[0] / 255f - 0.5f) / 0.5f;
                    input_tensor[0, 1, y, x] = (resize_image.At<Vec3b>(y, x)[1] / 255f - 0.5f) / 0.5f;
                    input_tensor[0, 2, y, x] = (resize_image.At<Vec3b>(y, x)[2] / 255f - 0.5f) / 0.5f;
                }
            }

            //将 input_tensor 放入一个输入参数的容器,并指定名称
            input_ontainer.Add(NamedOnnxValue.CreateFromTensor("input", input_tensor));

            dt1 = DateTime.Now;
            //运行 Inference 并获取结果
            result_infer = onnx_session.Run(input_ontainer);
            dt2 = DateTime.Now;

            //将输出结果转为DisposableNamedOnnxValue数组
            results_onnxvalue = result_infer.ToArray();

            //读取第一个节点输出并转为Tensor数据
            result_tensors = results_onnxvalue[0].AsTensor<float>();

            result_array = result_tensors.ToArray();

            float[] temp_r = new float[256 * 256];
            float[] temp_g = new float[256 * 256];
            float[] temp_b = new float[256 * 256];

            Array.Copy(result_array, temp_r, 256 * 256);
            Array.Copy(result_array, 256 * 256, temp_g, 0, 256 * 256);
            Array.Copy(result_array, 256 * 256 * 2, temp_b, 0, 256 * 256);

            Mat rmat = new Mat(256, 256, MatType.CV_32F, temp_r);
            Mat gmat = new Mat(256, 256, MatType.CV_32F, temp_g);
            Mat bmat = new Mat(256, 256, MatType.CV_32F, temp_b);

            rmat = (rmat + 1f) * 127.5f;
            gmat = (gmat + 1f) * 127.5f;
            bmat = (bmat + 1f) * 127.5f;

            result_image = new Mat();
            Cv2.Merge(new Mat[] { rmat, gmat, bmat }, result_image);

            if (!result_image.Empty())
            {
                //还原图像大小
                if (width != modelSize || height != modelSize)
                {
                    Rect rect = new Rect(0, 0, width, height);
                    result_image = result_image.Clone(rect);
                }
                result_image = result_image.Resize(new OpenCvSharp.Size(oldwidth, oldheight));

                pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
                textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
            }
            else
            {
                textBox1.Text = "无信息";
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = System.Windows.Forms.Application.StartupPath;
            model_path = startupPath + "\\photo2cartoon_weights.onnx";

            // 创建输出会话,用于输出模型读取信息
            options = new SessionOptions();
            options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
            // 设置为CPU上运行
            options.AppendExecutionProvider_CPU(0);

            // 创建推理模型类,读取本地模型文件
            onnx_session = new InferenceSession(model_path, options);

            // 输入Tensor
            input_tensor = new DenseTensor<float>(new[] { 1, 3, 256, 256 });

            // 创建输入容器
            input_ontainer = new List<NamedOnnxValue>();
        }
    }
}

Demo下载

说明

1、该例子只是人像转卡通像,转之前需要如下前置处理(为了效果更好)

  • 检测人脸及关键点。
  • 根据关键点旋转校正人脸。
  • 将关键点边界框按固定的比例扩张并裁剪出人脸区域。
  • 使用人像分割模型将背景置白。

2、该模型不能用于分割半身像,因为该模型是专用模型,需先裁剪出人脸区域再输入

人像分割参考:

C# PaddleInference.PP-HumanSeg 人像分割 替换背景色-CSDN博客

人脸检测参考:

C# DlibDotNet 人脸识别、人脸68特征点识别、人脸5特征点识别、人脸对齐,三角剖分,人脸特征比对-CSDN博客

3、参考

GitHub - minivision-ai/photo2cartoon-paddle: 人像卡通化探索项目 (photo-to-cartoon translation project)

相关推荐
文火冰糖的硅基工坊16 小时前
[光学原理与应用-480]:《国产检测设备对比表》
前端·人工智能·系统架构·制造·半导体·产业链
河南博为智能科技有限公司16 小时前
动力环境监控主机-全方位一体化监控解决方案
运维·服务器·人工智能·物联网·边缘计算
moshumu117 小时前
局域网访问Win11下的WSL中的jupyter notebook
ide·python·深度学习·神经网络·机器学习·jupyter
北京耐用通信18 小时前
耐达讯自动化Modbus RTU转Profibus,让电磁阀连接从此与众不同!
网络·人工智能·网络协议·网络安全·自动化
Phoenixtree_DongZhao18 小时前
ICLM 2025 Time Series 时间序列论文汇总(论文链接)
人工智能·时间序列
eve杭18 小时前
网络安全细则[特殊字符]
大数据·人工智能·5g·网络安全
图学习的小张19 小时前
Windows安装mamba全流程(全网最稳定最成功)
人工智能·windows·深度学习·语言模型
lisw0519 小时前
数据科学与AI的未来就业前景如何?
人工智能·机器学习·软件工程
索西引擎19 小时前
AI 智能体的运行模式
人工智能·ai智能体
reasonsummer19 小时前
【办公类-117-01】20250924通义万相视频2.5——三个小人(幼儿作品动态化)
人工智能·音视频·通义万相