C# OpenVINO 直接读取百度模型实现图片旋转角度检测

目录

效果

模型信息

代码

下载


C# OpenVINO 直接读取百度模型实现图片旋转角度检测

效果

模型信息

Inputs
-------------------------
name:x
tensor:F32[?, 3, 224, 224]

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

Outputs
-------------------------
name:softmax_1.tmp_0
tensor:F32[?, 4]

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

项目

代码

using OpenCvSharp;
using Sdcb.OpenVINO;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
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;
        Bitmap bmp;
        Mat src;

        StringBuilder sb = new StringBuilder();

        float rotateThreshold = 0.50f;

        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);
        }

        unsafe private void button2_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                return;
            }

            textBox1.Text = "";
            sb.Clear();

            src = OpenCvSharp.Extensions.BitmapConverter.ToMat(new Bitmap(pictureBox1.Image));

            model_path = "model/inference.pdmodel";
            Model rawModel = OVCore.Shared.ReadModel(model_path);

            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();

            Cv2.CvtColor(src, src, ColorConversionCodes.BGR2RGB);

            Mat resized = Common.ResizePadding(src, 224,224);

            Mat normalized = Common.Normalize(resized);

            float[] input_tensor_data = Common.ExtractMat(normalized);

            /*
             name:x
             tensor:F32[?, 3, 224, 224]
             */
            Tensor input_x = Tensor.FromArray(input_tensor_data, new Shape(1, 3, 224, 224));

            ir.Inputs[0] = input_x;

            double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();

            ir.Run();

            double inferTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();

            Tensor output_0 = ir.Outputs[0];

            RotationDegree r = RotationDegree._0;
            float[] softmax = output_0.GetData<float>().ToArray();
            float max = softmax.Max();
            int maxIndex = Array.IndexOf(softmax, max);
            if (max > rotateThreshold)
            {
                r = (RotationDegree)maxIndex;
            }

            double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Stop();
            double totalTime = preprocessTime + inferTime + postprocessTime;

            sb.AppendLine("图片旋转角度:" + r.ToString());
            sb.AppendLine();
            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;

            image_path = "test_img/1.jpg";
            bmp = new Bitmap(image_path);
            pictureBox1.Image = new Bitmap(image_path);
        }
    }
}

using OpenCvSharp;
using Sdcb.OpenVINO;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
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;
        Bitmap bmp;
        Mat src;

        StringBuilder sb = new StringBuilder();

        float rotateThreshold = 0.50f;

        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);
        }

        unsafe private void button2_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                return;
            }

            textBox1.Text = "";
            sb.Clear();

            src = OpenCvSharp.Extensions.BitmapConverter.ToMat(new Bitmap(pictureBox1.Image));

            model_path = "model/inference.pdmodel";
            Model rawModel = OVCore.Shared.ReadModel(model_path);

            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();

            Cv2.CvtColor(src, src, ColorConversionCodes.BGR2RGB);

            Mat resized = Common.ResizePadding(src, 224,224);

            Mat normalized = Common.Normalize(resized);

            float[] input_tensor_data = Common.ExtractMat(normalized);

            /*
             name:x
             tensor:F32[?, 3, 224, 224]
             */
            Tensor input_x = Tensor.FromArray(input_tensor_data, new Shape(1, 3, 224, 224));

            ir.Inputs[0] = input_x;

            double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();

            ir.Run();

            double inferTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();

            Tensor output_0 = ir.Outputs[0];

            RotationDegree r = RotationDegree._0;
            float[] softmax = output_0.GetData<float>().ToArray();
            float max = softmax.Max();
            int maxIndex = Array.IndexOf(softmax, max);
            if (max > rotateThreshold)
            {
                r = (RotationDegree)maxIndex;
            }

            double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Stop();
            double totalTime = preprocessTime + inferTime + postprocessTime;

            sb.AppendLine("图片旋转角度:" + r.ToString());
            sb.AppendLine();
            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;

            image_path = "test_img/1.jpg";
            bmp = new Bitmap(image_path);
            pictureBox1.Image = new Bitmap(image_path);
        }
    }
}

下载

源码下载

猜你喜欢

转载自blog.csdn.net/lw112190/article/details/135033152
今日推荐