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

C# CodeFormer Inpainting 人脸填充

效果

项目

代码

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;namespace CodeFormer_Demo
{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;int modelSize = 512;string model_path;Mat image;Mat result_image;SessionOptions options;InferenceSession onnx_session;Tensor<float> input_tensor;List<NamedOnnxValue> input_container;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;result_image = OnnxHelper.Run(image, modelSize, input_tensor, input_container, onnx_session, ref dt1, ref dt2);if (!result_image.Empty()){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 = Application.StartupPath;model_path = startupPath + "\\model\\codeformer_inpainting.onnx";modelSize = 512;// 创建输出会话,用于输出模型读取信息options = new SessionOptions();options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;//设置为CPU上运行options.AppendExecutionProvider_CPU(0);// 创建推理模型类,读取本地模型文件onnx_session = new InferenceSession(model_path, options);// 输入Tensorinput_tensor = new DenseTensor<float>(new[] { 1, 3, modelSize, modelSize });// 创建输入容器input_container = new List<NamedOnnxValue>();}private void button3_Click(object sender, EventArgs e){if (pictureBox2.Image == null){return;}Bitmap output = new Bitmap(pictureBox2.Image);var sdf = new SaveFileDialog();sdf.Title = "保存";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";if (sdf.ShowDialog() == DialogResult.OK){switch (sdf.FilterIndex){case 1:{output.Save(sdf.FileName, ImageFormat.Jpeg);break;}case 2:{output.Save(sdf.FileName, ImageFormat.Png);break;}case 3:{output.Save(sdf.FileName, ImageFormat.Bmp);break;}case 4:{output.Save(sdf.FileName, ImageFormat.Emf);break;}case 5:{output.Save(sdf.FileName, ImageFormat.Exif);break;}case 6:{output.Save(sdf.FileName, ImageFormat.Gif);break;}case 7:{output.Save(sdf.FileName, ImageFormat.Icon);break;}case 8:{output.Save(sdf.FileName, ImageFormat.Tiff);break;}case 9:{output.Save(sdf.FileName, ImageFormat.Wmf);break;}}MessageBox.Show("保存成功,位置:" + sdf.FileName);}}}
}

下载

可执行程序exe下载

源码下载

参考

GitHub - sczhou/CodeFormer: [NeurIPS 2022] Towards Robust Blind Face Restoration with Codebook Lookup Transformer

http://www.lryc.cn/news/197564.html

相关文章:

  • 将Sketch文件转化为PSD文件的简单在线工具!
  • 【广州华锐互动】利用AR进行野外地质调查学习,培养学生实践能力
  • Jmeter快速入门
  • 详解js数组操作——filter()方法
  • 基于MATLAB的图像条形码识别系统(matlab毕毕业设计2)
  • F5.5G落进现实:目标网带来的光之路
  • Python调用c++生成的dll
  • 算法基础学习|二分
  • mac M1 pro 安装grpc 报错
  • 交银国际:拼多多财报预测:主站盈利提升有望带动业绩超预期
  • 【SA8295P 源码分析 (二)】50 - OpenWFD Server 启动流程 之 wfd_server_tpp 线程池源码分析
  • 9.strspn函数
  • 电脑蓝牙与ESP32蓝牙连接,让电脑发现ESP32
  • k8s 暴露pod
  • Apache Dubbo 首个 Node.js 3.0-alpha 版本正式发布
  • Node.js中Buffer API详解
  • 【Hello Algorithm】暴力递归到动态规划(三)
  • gitLab更新11.11.3->16.1.5
  • 12-k8s-HPA自动扩缩容
  • 从十月稻田,看大米为何能卖出200亿市值?
  • 功能集成,不占空间,同为科技TOWE嵌入式桌面PDU超级插座
  • 使用pdf.js预览pdf文件时如何兼容chrome66版本
  • 一篇文章讲明白double、float丢失精度的问题
  • Day 2 Qt
  • ArmSoM-W3之RK3588 MPP环境配置
  • 【C++ 拷贝构造函数详解】
  • [计算机提升] 用户和用户组
  • 开路、断路和短路区别
  • springBoot web开发自动配置和默认效果
  • 论文阅读:Efficient Point Cloud Segmentation with Geometry-Aware Sparse Networks