C# 人像卡通化 Onnx photo2cartoon

效果

C# 人像卡通化 Onnx photo2cartoon_第1张图片

项目

C# 人像卡通化 Onnx photo2cartoon_第2张图片

代码

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 input_tensor;
        List input_ontainer;
        IDisposableReadOnlyCollection result_infer;
        DisposableNamedOnnxValue[] results_onnxvalue;

        Tensor 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(y, x)[0] / 255f - 0.5f) / 0.5f;
                    input_tensor[0, 1, y, x] = (resize_image.At(y, x)[1] / 255f - 0.5f) / 0.5f;
                    input_tensor[0, 2, y, x] = (resize_image.At(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();

            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(new[] { 1, 3, 256, 256 });

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

Demo下载

说明

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

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

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

人像分割参考:

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

人脸检测参考:

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

3、参考

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

你可能感兴趣的:(C#,AI,OpenCV,人工智能,深度学习,C#,人像卡通化)