C# OpenVINO实现图片旋转角度检测

 更新时间:2024年02月05日 10:14:50   作者:天天代码码天天  
这篇文章主要为大家详细介绍了C# OpenVINO如何实现图片旋转角度检测,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

效果

项目

代码

using OpenCvSharp;
using Sdcb.OpenVINO;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
 
namespace C__OpenVINO_图片旋转角度检测
{
    public partial class Form1 : Form
    {
        Bitmap bmp;
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string img = "";
        float rotateThreshold = 0.50f;
        InputShape defaultShape = new InputShape(3, 224, 224);
        string model_path;
        CompiledModel cm;
        InferRequest ir;
 
        StringBuilder sb = new StringBuilder();
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            model_path = "models/inference.pdmodel";
            Model rawModel = OVCore.Shared.ReadModel(model_path);
 
            var ad = OVCore.Shared.AvailableDevices;
            Console.WriteLine("可用设备");
            foreach (var item in ad)
            {
                Console.WriteLine(item);
            }
 
            cm = OVCore.Shared.CompileModel(rawModel, "CPU");
            ir = cm.CreateInferRequest();
 
            img = "1.jpg";
            bmp = new Bitmap(img);
            pictureBox1.Image = new Bitmap(img);
 
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
 
            pictureBox1.Image = null;
 
            img = ofd.FileName;
            bmp = new Bitmap(img);
            pictureBox1.Image = new Bitmap(img);
            textBox1.Text = "";
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            if (bmp == null)
            {
                return;
            }
 
            var mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);
            Cv2.CvtColor(mat, mat, ColorConversionCodes.RGBA2RGB);
            Cv2.Rotate(mat, mat, RotateFlags.Rotate90Clockwise);
            var bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
            pictureBox1.Image = bitmap;
        }
 
        private void button4_Click(object sender, EventArgs e)
        {
            if (bmp == null)
            {
                return;
            }
 
            var mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);
            Cv2.CvtColor(mat, mat, ColorConversionCodes.RGBA2RGB);
            Cv2.Rotate(mat, mat, RotateFlags.Rotate180);
            var bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
            pictureBox1.Image = bitmap;
        }
 
        private void button5_Click(object sender, EventArgs e)
        {
            if (bmp == null)
            {
                return;
            }
 
            var mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);
            Cv2.CvtColor(mat, mat, ColorConversionCodes.RGBA2RGB);
            Cv2.Rotate(mat, mat, RotateFlags.Rotate90Counterclockwise);
            var bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
            pictureBox1.Image = bitmap;
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            if (img == "") { return; }
 
            textBox1.Text = "";
            sb.Clear();
 
            Mat src = OpenCvSharp.Extensions.BitmapConverter.ToMat(new Bitmap(pictureBox1.Image));
            Cv2.CvtColor(src, src, ColorConversionCodes.RGBA2RGB);//mat转三通道mat
 
            Stopwatch stopwatch = new Stopwatch();
 
            Mat resized = Common.ResizePadding(src, defaultShape);
            Mat normalized = Common.Normalize(resized);
 
            float[] input_tensor_data = Common.ExtractMat(normalized);
 
            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;
            }
 
            string result = r.ToString();
 
            result = result + " (" + max.ToString("P2")+")";
 
            double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Stop();
            double totalTime = preprocessTime + inferTime + postprocessTime;
 
            sb.AppendLine("结果:" + result);
            sb.AppendLine();
            sb.AppendLine("Scores: [" + String.Join(", ", softmax) + "]");
            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();
 
        }
 
    }
 
    public readonly struct InputShape
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="InputShape"/> struct.
        /// </summary>
        /// <param name="channel">The number of color channels in the input image.</param>
        /// <param name="width">The width of the input image in pixels.</param>
        /// <param name="height">The height of the input image in pixels.</param>
        public InputShape(int channel, int width, int height)
        {
            Channel = channel;
            Height = height;
            Width = width;
        }
 
        /// <summary>
        /// Gets the number of color channels in the input image.
        /// </summary>
        public int Channel { get; }
 
        /// <summary>
        /// Gets the height of the input image in pixels.
        /// </summary>
        public int Height { get; }
 
        /// <summary>
        /// Gets the width of the input image in pixels.
        /// </summary>
        public int Width { get; }
    }
 
    /// <summary>
    /// Enum representing the degrees of rotation.
    /// </summary>
    public enum RotationDegree
    {
        /// <summary>
        /// Represents the 0-degree rotation angle.
        /// </summary>
        _0,
        /// <summary>
        /// Represents the 90-degree rotation angle.
        /// </summary>
        _90,
        /// <summary>
        /// Represents the 180-degree rotation angle.
        /// </summary>
        _180,
        /// <summary>
        /// Represents the 270-degree rotation angle.
        /// </summary>
        _270,
    }
}

以上就是C# OpenVINO实现图片旋转角度检测的详细内容,更多关于C# OpenVINO图片旋转角度检测的资料请关注脚本之家其它相关文章!

相关文章

  • C#中事件只能在内部调用的原因分析

    C#中事件只能在内部调用的原因分析

    事件(Event) 基本上说是一个用户操作,如按键、点击、鼠标移动等等,或者是一些提示信息,如系统生成的通知。应用程序需要在事件发生时响应事件,这篇文章主要介绍了C#中事件为什么只能在内部调用,需要的朋友可以参考下
    2021-11-11
  • DevExpress实现TreeList向上递归获取符合条件的父节点

    DevExpress实现TreeList向上递归获取符合条件的父节点

    这篇文章主要介绍了DevExpress实现TreeList向上递归获取符合条件的父节点,需要的朋友可以参考下
    2014-08-08
  • C#实现简易计算器功能(1)(窗体应用)

    C#实现简易计算器功能(1)(窗体应用)

    这篇文章主要为大家详细介绍了C#实现简易计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • c# 如何用组合替代继承

    c# 如何用组合替代继承

    这篇文章主要介绍了c# 如何用组合替代继承,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-02-02
  • C#使用NAudio实现监听系统声音

    C#使用NAudio实现监听系统声音

    这篇文章主要为大家详细介绍了C#如何使用NAudio实现监听系统声音并屏蔽麦克风其他声音,文中的示例代码讲解详细,有需要的小伙伴可以参考下
    2024-02-02
  • 深入c# Func委托的详解

    深入c# Func委托的详解

    本篇文章是对c#中的Func委托进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • C#窗体读取EXCEL并存入SQL数据库的方法

    C#窗体读取EXCEL并存入SQL数据库的方法

    这篇文章主要介绍了C#窗体读取EXCEL并存入SQL数据库的方法,实例简述了实现读取excel及写入SQL数据库的原理与技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-01-01
  • C#使用Spire.Doc删除Word中表格的常用方法

    C#使用Spire.Doc删除Word中表格的常用方法

    在处理Word文档自动化时,经常需要动态删除特定表格使用Spire.Doc,只需几行C#代码即可精准定位并删除指定表格,无需手动操作,极大提升文档处理效率,所以本文给大家介绍了如何使用 Spire.Doc 删除 Word 中的表格,需要的朋友可以参考下
    2025-08-08
  • C# Sqlite数据库的搭建及使用技巧

    C# Sqlite数据库的搭建及使用技巧

    这篇文章主要介绍了C# Sqlite数据库的搭建及使用技巧,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下
    2022-08-08
  • C#获取网页HTML源码实例

    C#获取网页HTML源码实例

    这篇文章主要介绍了C#获取网页HTML源码的方法,是非常实用的技巧,需要的朋友可以参考下
    2014-10-10

最新评论