• C# CodeFormer Inpainting 人脸填充


    目录

    介绍

    效果

    模型信息

    项目

    代码

    下载


    介绍

    github地址:https://github.com/sczhou/CodeFormer

    [NeurIPS 2022] Towards Robust Blind Face Restoration with Codebook Lookup Transformer

     

    效果

    模型信息

    codeformer_inpainting.onnx

    Inputs
    -------------------------
    name:input
    tensor:Float[1, 3, 512, 512]
    ---------------------------------------------------------------

    Outputs
    -------------------------
    name:output
    tensor:Float[1, 3, 512, 512]
    name:logits
    tensor:Float[1, 256, 512]
    name:onnx::Shape_1319
    tensor:Float[1, 256, 16, 16]
    ---------------------------------------------------------------

    项目

    VS2022

    .net framework 4.8

    OpenCvSharp 4.8

    Microsoft.ML.OnnxRuntime 1.16.2

    代码

    创建Tensor
    input_tensor = new DenseTensor(new[] { 1, 3, 512, 512 });
    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;
        }
    }

    1. using Microsoft.ML.OnnxRuntime;
    2. using Microsoft.ML.OnnxRuntime.Tensors;
    3. using OpenCvSharp;
    4. using System;
    5. using System.Collections.Generic;
    6. using System.Drawing;
    7. using System.Drawing.Imaging;
    8. using System.Windows.Forms;
    9. namespace CodeFormer_Demo
    10. {
    11. public partial class Form1 : Form
    12. {
    13. public Form1()
    14. {
    15. InitializeComponent();
    16. }
    17. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    18. string image_path = "";
    19. string startupPath;
    20. DateTime dt1 = DateTime.Now;
    21. DateTime dt2 = DateTime.Now;
    22. int modelSize = 512;
    23. string model_path;
    24. Mat image;
    25. Mat result_image;
    26. SessionOptions options;
    27. InferenceSession onnx_session;
    28. Tensor<float> input_tensor;
    29. List<NamedOnnxValue> input_container;
    30. private void button1_Click(object sender, EventArgs e)
    31. {
    32. OpenFileDialog ofd = new OpenFileDialog();
    33. ofd.Filter = fileFilter;
    34. if (ofd.ShowDialog() != DialogResult.OK) return;
    35. pictureBox1.Image = null;
    36. image_path = ofd.FileName;
    37. pictureBox1.Image = new Bitmap(image_path);
    38. textBox1.Text = "";
    39. image = new Mat(image_path);
    40. pictureBox2.Image = null;
    41. }
    42. private void button2_Click(object sender, EventArgs e)
    43. {
    44. if (image_path == "")
    45. {
    46. return;
    47. }
    48. textBox1.Text = "";
    49. pictureBox2.Image = null;
    50. result_image = OnnxHelper.Run(image, modelSize, input_tensor, input_container, onnx_session, ref dt1, ref dt2);
    51. if (!result_image.Empty())
    52. {
    53. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
    54. textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
    55. }
    56. else
    57. {
    58. textBox1.Text = "无信息";
    59. }
    60. }
    61. private void Form1_Load(object sender, EventArgs e)
    62. {
    63. startupPath = Application.StartupPath;
    64. model_path = startupPath + "\\model\\codeformer_inpainting.onnx";
    65. modelSize = 512;
    66. // 创建输出会话,用于输出模型读取信息
    67. options = new SessionOptions();
    68. options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
    69. //设置为CPU上运行
    70. options.AppendExecutionProvider_CPU(0);
    71. // 创建推理模型类,读取本地模型文件
    72. onnx_session = new InferenceSession(model_path, options);
    73. // 输入Tensor
    74. input_tensor = new DenseTensor<float>(new[] { 1, 3, modelSize, modelSize });
    75. // 创建输入容器
    76. input_container = new List<NamedOnnxValue>();
    77. }
    78. private void button3_Click(object sender, EventArgs e)
    79. {
    80. if (pictureBox2.Image == null)
    81. {
    82. return;
    83. }
    84. Bitmap output = new Bitmap(pictureBox2.Image);
    85. var sdf = new SaveFileDialog();
    86. sdf.Title = "保存";
    87. sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";
    88. if (sdf.ShowDialog() == DialogResult.OK)
    89. {
    90. switch (sdf.FilterIndex)
    91. {
    92. case 1:
    93. {
    94. output.Save(sdf.FileName, ImageFormat.Jpeg);
    95. break;
    96. }
    97. case 2:
    98. {
    99. output.Save(sdf.FileName, ImageFormat.Png);
    100. break;
    101. }
    102. case 3:
    103. {
    104. output.Save(sdf.FileName, ImageFormat.Bmp);
    105. break;
    106. }
    107. case 4:
    108. {
    109. output.Save(sdf.FileName, ImageFormat.Emf);
    110. break;
    111. }
    112. case 5:
    113. {
    114. output.Save(sdf.FileName, ImageFormat.Exif);
    115. break;
    116. }
    117. case 6:
    118. {
    119. output.Save(sdf.FileName, ImageFormat.Gif);
    120. break;
    121. }
    122. case 7:
    123. {
    124. output.Save(sdf.FileName, ImageFormat.Icon);
    125. break;
    126. }
    127. case 8:
    128. {
    129. output.Save(sdf.FileName, ImageFormat.Tiff);
    130. break;
    131. }
    132. case 9:
    133. {
    134. output.Save(sdf.FileName, ImageFormat.Wmf);
    135. break;
    136. }
    137. }
    138. MessageBox.Show("保存成功,位置:" + sdf.FileName);
    139. }
    140. }
    141. }
    142. }

    下载

    可执行程序exe下载

    源码下载

  • 相关阅读:
    gdb监视
    AI诈骗的防范与应对:维护数字安全的责任
    [SpringBoot系列]SpringBoot如何整合SSMP
    大数据技术之 HBase简介
    分享一个用python写的本地WIFI密码查看器
    07-Go语言中map数据结构用法
    Adobe Acrobat Pro DC 2023:提升工作效率,激发创意灵感 mac/win版
    Python添加水印简简单单,三行代码教你批量添加
    【虚拟机】网卡不见了,失效了怎么办
    一文了解有限空间作业管理办法
  • 原文地址:https://blog.csdn.net/lw112190/article/details/133894628