1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| using Microsoft.ML.OnnxRuntime; using Microsoft.ML.OnnxRuntime.Tensors; using OpenCvSharp; using System; using System.Collections.Generic; using System.Drawing; using System.Text; using System.Windows.Forms;
namespace Onnx_Demo { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png"; string image_path = ""; string image_path_mask = ""; DateTime dt1 = DateTime.Now; DateTime dt2 = DateTime.Now; string model_path; Mat image; Mat image_mask;
SessionOptions options; InferenceSession onnx_session; Tensor<float> input_tensor; Tensor<float> input_tensor_mask; List<NamedOnnxValue> input_container; IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;
StringBuilder sb = new StringBuilder();
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; }
button2.Enabled = false; pictureBox2.Image = null; textBox1.Text = "";
image = new Mat(image_path); int w = image.Width; int h = image.Height; image_mask = new Mat(image_path_mask);
Common.Preprocess(image, image_mask, input_tensor, input_tensor_mask);
input_container.Add(NamedOnnxValue.CreateFromTensor("image", input_tensor));
input_container.Add(NamedOnnxValue.CreateFromTensor("mask", input_tensor_mask));
dt1 = DateTime.Now; result_infer = onnx_session.Run(input_container); dt2 = DateTime.Now;
Mat result = Common.Postprocess(result_infer);
Cv2.Resize(result, result, new OpenCvSharp.Size(w, h));
sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
pictureBox2.Image = new Bitmap(result.ToMemoryStream()); textBox1.Text = sb.ToString();
button2.Enabled = true; }
private void Form1_Load(object sender, EventArgs e) { model_path = "model/big_lama_regular_inpaint.onnx";
options = new SessionOptions(); options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO; options.AppendExecutionProvider_CPU(0);
onnx_session = new InferenceSession(model_path, options);
input_tensor = new DenseTensor<float>(new[] { 1, 3, 1000, 1504 });
input_tensor_mask = new DenseTensor<float>(new[] { 1, 1, 1000, 1504 });
input_container = new List<NamedOnnxValue>();
image_path = "test_img/test.jpg"; pictureBox1.Image = new Bitmap(image_path);
image_path_mask = "test_img/mask.jpg"; pictureBox3.Image = new Bitmap(image_path_mask); } } }
|