C# OpenVINO 直接读取百度模型实现印章检测

发布时间:2023年12月17日

目录

效果

模型信息

项目

代码

下载

其他


C# OpenVINO 直接读取百度模型实现印章检测

效果

模型信息

Inputs
-------------------------
name:scale_factor
tensor:F32[?, 2]

name:image
tensor:F32[?, 3, 608, 608]

name:im_shape
tensor:F32[?, 2]

---------------------------------------------------------------

Outputs
-------------------------
name:multiclass_nms3_0.tmp_0
tensor:F32[?, 6]

name:multiclass_nms3_0.tmp_2
tensor:I32[?]

---------------------------------------------------------------

项目

代码

using OpenCvSharp;
using Sdcb.OpenVINO;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
?
namespace OpenVINO_Det_物体检测
{
? ? public partial class Form1 : Form
? ? {
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
?
? ? ? ? string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
? ? ? ? string image_path = "";
? ? ? ? string startupPath;
? ? ? ? string model_path;
? ? ? ? Mat src;
? ? ? ? string[] dicts;
?
? ? ? ? StringBuilder sb = new StringBuilder();
?
? ? ? ? float confidence = 0.75f;
?
? ? ? ? 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 = "";
? ? ? ? ? ? src = new Mat(image_path);
? ? ? ? ? ? pictureBox2.Image = null;
? ? ? ? }
?
? ? ? ? unsafe private void button2_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (pictureBox1.Image == null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
?
? ? ? ? ? ? pictureBox2.Image = null;
? ? ? ? ? ? textBox1.Text = "";
? ? ? ? ? ? sb.Clear();
?
? ? ? ? ? ? src = new Mat(image_path);
? ? ? ? ? ? Mat result_image = src.Clone();
?
? ? ? ? ? ? model_path = "model/model.pdmodel";
? ? ? ? ? ? Model rawModel = OVCore.Shared.ReadModel(model_path);
?
? ? ? ? ? ? int inpHeight = 608;
? ? ? ? ? ? int inpWidth = 608;
?
? ? ? ? ? ? var ad = OVCore.Shared.AvailableDevices;
? ? ? ? ? ? Console.WriteLine("可用设备");
? ? ? ? ? ? foreach (var item in ad)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine(item);
? ? ? ? ? ? }
?
? ? ? ? ? ? CompiledModel cm = OVCore.Shared.CompileModel(rawModel, "CPU");
? ? ? ? ? ? InferRequest ir = cm.CreateInferRequest();
?
? ? ? ? ? ? Stopwatch stopwatch = new Stopwatch();
?
? ? ? ? ? ? Shape inputShape = new Shape(1, 608, 608);
? ? ? ? ? ? Size2f sizeRatio = new Size2f(1f * src.Width / inputShape[2], 1f * src.Height / inputShape[1]);
? ? ? ? ? ? Cv2.CvtColor(src, src, ColorConversionCodes.BGR2RGB);
?
? ? ? ? ? ? Point2f scaleRate = new Point2f(1f * inpWidth / src.Width, 1f * inpHeight / src.Height);
?
? ? ? ? ? ? Cv2.Resize(src, src, new OpenCvSharp.Size(), scaleRate.X, scaleRate.Y);
?
? ? ? ? ? ? Common.Normalize(src);
?
? ? ? ? ? ? float[] input_tensor_data = Common.ExtractMat(src);
?
? ? ? ? ? ? /*
? ? ? ? ? ? ?scale_factor ? 1,2
? ? ? ? ? ? ?image ? ? ? ? ?1,3,608,608
? ? ? ? ? ? ?im_shape ? ? ? 1,2?
? ? ? ? ? ? ?*/
? ? ? ? ? ? Tensor input_scale_factor = Tensor.FromArray(new float[] { scaleRate.Y, scaleRate.X }, new Shape(1, 2));
? ? ? ? ? ? Tensor input_image = Tensor.FromArray(input_tensor_data, new Shape(1, 3, 608, 608));
? ? ? ? ? ? Tensor input_im_shape = Tensor.FromArray(new float[] { 608, 608 }, new Shape(1, 2));
?
? ? ? ? ? ? ir.Inputs[0] = input_scale_factor;
? ? ? ? ? ? ir.Inputs[1] = input_image;
? ? ? ? ? ? ir.Inputs[2] = input_im_shape;
?
? ? ? ? ? ? double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
? ? ? ? ? ? stopwatch.Restart();
?
? ? ? ? ? ? ir.Run();
?
? ? ? ? ? ? double inferTime = stopwatch.Elapsed.TotalMilliseconds;
? ? ? ? ? ? stopwatch.Restart();
?
? ? ? ? ? ? Tensor output_0 = ir.Outputs[0];
?
? ? ? ? ? ? int num = (int)output_0.Shape.Dimensions[0];
?
? ? ? ? ? ? float[] output_0_array = output_0.GetData<float>().ToArray();
?
? ? ? ? ? ? for (int j = 0; j < num; j++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? int num12 = (int)Math.Round(output_0_array[j * 6]);
? ? ? ? ? ? ? ? float score = output_0_array[1 + j * 6];
?
? ? ? ? ? ? ? ? if (score > this.confidence)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? int num13 = (int)(output_0_array[2 + j * 6]);
? ? ? ? ? ? ? ? ? ? int num14 = (int)(output_0_array[3 + j * 6]);
? ? ? ? ? ? ? ? ? ? int num15 = (int)(output_0_array[4 + j * 6]);
? ? ? ? ? ? ? ? ? ? int num16 = (int)(output_0_array[5 + j * 6]);
?
? ? ? ? ? ? ? ? ? ? string ClassName = dicts[num12];
? ? ? ? ? ? ? ? ? ? Rect r = Rect.FromLTRB(num13, num14, num15, num16);
? ? ? ? ? ? ? ? ? ? sb.AppendLine($"{ClassName}:{score:P0}");
? ? ? ? ? ? ? ? ? ? Cv2.PutText(result_image, $"{ClassName}:{score:P0}", new OpenCvSharp.Point(r.TopLeft.X, r.TopLeft.Y - 10), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
? ? ? ? ? ? ? ? ? ? Cv2.Rectangle(result_image, r, Scalar.Red, thickness: 2);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
?
? ? ? ? ? ? double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
? ? ? ? ? ? stopwatch.Stop();
? ? ? ? ? ? double totalTime = preprocessTime + inferTime + postprocessTime;
?
? ? ? ? ? ? pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
?
? ? ? ? ? ? sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
? ? ? ? ? ? sb.AppendLine($"Infer: {inferTime:F2}ms");
? ? ? ? ? ? sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
? ? ? ? ? ? sb.AppendLine($"Total: {totalTime:F2}ms");
?
? ? ? ? ? ? textBox1.Text = sb.ToString();
?
? ? ? ? }
?
? ? ? ? private void Form1_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? startupPath = Application.StartupPath;
?
? ? ? ? ? ? string classer_path = "lable.txt";
? ? ? ? ? ? List<string> str = new List<string>();
? ? ? ? ? ? StreamReader sr = new StreamReader(classer_path);
? ? ? ? ? ? string line;
? ? ? ? ? ? while ((line = sr.ReadLine()) != null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? str.Add(line);
? ? ? ? ? ? }
? ? ? ? ? ? dicts = str.ToArray();
?
? ? ? ? ? ? image_path = "test_img/1.jpg";
? ? ? ? ? ? pictureBox1.Image = new Bitmap(image_path);
? ? ? ? }
? ? }
}

using OpenCvSharp;
using Sdcb.OpenVINO;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
 
namespace OpenVINO_Det_物体检测
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        string startupPath;
        string model_path;
        Mat src;
        string[] dicts;
 
        StringBuilder sb = new StringBuilder();
 
        float confidence = 0.75f;
 
        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 = "";
            src = new Mat(image_path);
            pictureBox2.Image = null;
        }
 
        unsafe private void button2_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                return;
            }
 
            pictureBox2.Image = null;
            textBox1.Text = "";
            sb.Clear();
 
            src = new Mat(image_path);
            Mat result_image = src.Clone();
 
            model_path = "model/model.pdmodel";
            Model rawModel = OVCore.Shared.ReadModel(model_path);
 
            int inpHeight = 608;
            int inpWidth = 608;
 
            var ad = OVCore.Shared.AvailableDevices;
            Console.WriteLine("可用设备");
            foreach (var item in ad)
            {
                Console.WriteLine(item);
            }
 
            CompiledModel cm = OVCore.Shared.CompileModel(rawModel, "CPU");
            InferRequest ir = cm.CreateInferRequest();
 
            Stopwatch stopwatch = new Stopwatch();
 
            Shape inputShape = new Shape(1, 608, 608);
            Size2f sizeRatio = new Size2f(1f * src.Width / inputShape[2], 1f * src.Height / inputShape[1]);
            Cv2.CvtColor(src, src, ColorConversionCodes.BGR2RGB);
 
            Point2f scaleRate = new Point2f(1f * inpWidth / src.Width, 1f * inpHeight / src.Height);
 
            Cv2.Resize(src, src, new OpenCvSharp.Size(), scaleRate.X, scaleRate.Y);
 
            Common.Normalize(src);
 
            float[] input_tensor_data = Common.ExtractMat(src);
 
            /*
             scale_factor   1,2
             image          1,3,608,608
             im_shape       1,2 
             */
            Tensor input_scale_factor = Tensor.FromArray(new float[] { scaleRate.Y, scaleRate.X }, new Shape(1, 2));
            Tensor input_image = Tensor.FromArray(input_tensor_data, new Shape(1, 3, 608, 608));
            Tensor input_im_shape = Tensor.FromArray(new float[] { 608, 608 }, new Shape(1, 2));
 
            ir.Inputs[0] = input_scale_factor;
            ir.Inputs[1] = input_image;
            ir.Inputs[2] = input_im_shape;
 
            double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();
 
            ir.Run();
 
            double inferTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();
 
            Tensor output_0 = ir.Outputs[0];
 
            int num = (int)output_0.Shape.Dimensions[0];
 
            float[] output_0_array = output_0.GetData<float>().ToArray();
 
            for (int j = 0; j < num; j++)
            {
                int num12 = (int)Math.Round(output_0_array[j * 6]);
                float score = output_0_array[1 + j * 6];
 
                if (score > this.confidence)
                {
                    int num13 = (int)(output_0_array[2 + j * 6]);
                    int num14 = (int)(output_0_array[3 + j * 6]);
                    int num15 = (int)(output_0_array[4 + j * 6]);
                    int num16 = (int)(output_0_array[5 + j * 6]);
 
                    string ClassName = dicts[num12];
                    Rect r = Rect.FromLTRB(num13, num14, num15, num16);
                    sb.AppendLine($"{ClassName}:{score:P0}");
                    Cv2.PutText(result_image, $"{ClassName}:{score:P0}", new OpenCvSharp.Point(r.TopLeft.X, r.TopLeft.Y - 10), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                    Cv2.Rectangle(result_image, r, Scalar.Red, thickness: 2);
                }
            }
 
            double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Stop();
            double totalTime = preprocessTime + inferTime + postprocessTime;
 
            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
 
            sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
            sb.AppendLine($"Infer: {inferTime:F2}ms");
            sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
            sb.AppendLine($"Total: {totalTime:F2}ms");
 
            textBox1.Text = sb.ToString();
 
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = Application.StartupPath;
 
            string classer_path = "lable.txt";
            List<string> str = new List<string>();
            StreamReader sr = new StreamReader(classer_path);
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                str.Add(line);
            }
            dicts = str.ToArray();
 
            image_path = "test_img/1.jpg";
            pictureBox1.Image = new Bitmap(image_path);
        }
    }
}

下载

源码下载

其他

C# PaddleDetection yolo 印章检测-CSDN博客

C# Onnx Yolov8 Detect 印章 指纹捺印 检测-CSDN博客

文章来源:https://blog.csdn.net/weixin_46771779/article/details/135001052
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。