首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

C# OpenVINO 图片旋转角度检测

  • 25-02-19 03:01
  • 4651
  • 6636
blog.csdn.net

目录

效果

项目

代码

下载 


效果

项目

代码

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().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
    {
        ///


        /// Initializes a new instance of the struct.
        ///

        /// The number of color channels in the input image.
        /// The width of the input image in pixels.
        /// The height of the input image in pixels.
        public InputShape(int channel, int width, int height)
        {
            Channel = channel;
            Height = height;
            Width = width;
        }

        ///


        /// Gets the number of color channels in the input image.
        ///

        public int Channel { get; }

        ///


        /// Gets the height of the input image in pixels.
        ///

        public int Height { get; }

        ///


        /// Gets the width of the input image in pixels.
        ///

        public int Width { get; }
    }

    ///


    /// Enum representing the degrees of rotation.
    ///

    public enum RotationDegree
    {
        ///
        /// Represents the 0-degree rotation angle.
        ///

        _0,
        ///
        /// Represents the 90-degree rotation angle.
        ///

        _90,
        ///
        /// Represents the 180-degree rotation angle.
        ///

        _180,
        ///
        /// Represents the 270-degree rotation angle.
        ///

        _270,
    }
}

  1. using OpenCvSharp;
  2. using Sdcb.OpenVINO;
  3. using System;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. namespace C__OpenVINO_图片旋转角度检测
  12. {
  13. public partial class Form1 : Form
  14. {
  15. Bitmap bmp;
  16. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  17. string img = "";
  18. float rotateThreshold = 0.50f;
  19. InputShape defaultShape = new InputShape(3, 224, 224);
  20. string model_path;
  21. CompiledModel cm;
  22. InferRequest ir;
  23. StringBuilder sb = new StringBuilder();
  24. public Form1()
  25. {
  26. InitializeComponent();
  27. }
  28. private void Form1_Load(object sender, EventArgs e)
  29. {
  30. model_path = "models/inference.pdmodel";
  31. Model rawModel = OVCore.Shared.ReadModel(model_path);
  32. var ad = OVCore.Shared.AvailableDevices;
  33. Console.WriteLine("可用设备");
  34. foreach (var item in ad)
  35. {
  36. Console.WriteLine(item);
  37. }
  38. cm = OVCore.Shared.CompileModel(rawModel, "CPU");
  39. ir = cm.CreateInferRequest();
  40. img = "1.jpg";
  41. bmp = new Bitmap(img);
  42. pictureBox1.Image = new Bitmap(img);
  43. }
  44. private void button1_Click(object sender, EventArgs e)
  45. {
  46. OpenFileDialog ofd = new OpenFileDialog();
  47. ofd.Filter = fileFilter;
  48. if (ofd.ShowDialog() != DialogResult.OK) return;
  49. pictureBox1.Image = null;
  50. img = ofd.FileName;
  51. bmp = new Bitmap(img);
  52. pictureBox1.Image = new Bitmap(img);
  53. textBox1.Text = "";
  54. }
  55. private void button3_Click(object sender, EventArgs e)
  56. {
  57. if (bmp == null)
  58. {
  59. return;
  60. }
  61. var mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);
  62. Cv2.CvtColor(mat, mat, ColorConversionCodes.RGBA2RGB);
  63. Cv2.Rotate(mat, mat, RotateFlags.Rotate90Clockwise);
  64. var bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
  65. pictureBox1.Image = bitmap;
  66. }
  67. private void button4_Click(object sender, EventArgs e)
  68. {
  69. if (bmp == null)
  70. {
  71. return;
  72. }
  73. var mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);
  74. Cv2.CvtColor(mat, mat, ColorConversionCodes.RGBA2RGB);
  75. Cv2.Rotate(mat, mat, RotateFlags.Rotate180);
  76. var bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
  77. pictureBox1.Image = bitmap;
  78. }
  79. private void button5_Click(object sender, EventArgs e)
  80. {
  81. if (bmp == null)
  82. {
  83. return;
  84. }
  85. var mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);
  86. Cv2.CvtColor(mat, mat, ColorConversionCodes.RGBA2RGB);
  87. Cv2.Rotate(mat, mat, RotateFlags.Rotate90Counterclockwise);
  88. var bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
  89. pictureBox1.Image = bitmap;
  90. }
  91. private void button2_Click(object sender, EventArgs e)
  92. {
  93. if (img == "") { return; }
  94. textBox1.Text = "";
  95. sb.Clear();
  96. Mat src = OpenCvSharp.Extensions.BitmapConverter.ToMat(new Bitmap(pictureBox1.Image));
  97. Cv2.CvtColor(src, src, ColorConversionCodes.RGBA2RGB);//mat转三通道mat
  98. Stopwatch stopwatch = new Stopwatch();
  99. Mat resized = Common.ResizePadding(src, defaultShape);
  100. Mat normalized = Common.Normalize(resized);
  101. float[] input_tensor_data = Common.ExtractMat(normalized);
  102. Tensor input_x = Tensor.FromArray(input_tensor_data, new Shape(1, 3, 224, 224));
  103. ir.Inputs[0] = input_x;
  104. double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
  105. stopwatch.Restart();
  106. ir.Run();
  107. double inferTime = stopwatch.Elapsed.TotalMilliseconds;
  108. stopwatch.Restart();
  109. Tensor output_0 = ir.Outputs[0];
  110. RotationDegree r = RotationDegree._0;
  111. float[] softmax = output_0.GetData<float>().ToArray();
  112. float max = softmax.Max();
  113. int maxIndex = Array.IndexOf(softmax, max);
  114. if (max > rotateThreshold)
  115. {
  116. r = (RotationDegree)maxIndex;
  117. }
  118. string result = r.ToString();
  119. result = result + " (" + max.ToString("P2")+")";
  120. double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
  121. stopwatch.Stop();
  122. double totalTime = preprocessTime + inferTime + postprocessTime;
  123. sb.AppendLine("结果:" + result);
  124. sb.AppendLine();
  125. sb.AppendLine("Scores: [" + String.Join(", ", softmax) + "]");
  126. sb.AppendLine();
  127. sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
  128. sb.AppendLine($"Infer: {inferTime:F2}ms");
  129. sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
  130. sb.AppendLine($"Total: {totalTime:F2}ms");
  131. textBox1.Text = sb.ToString();
  132. }
  133. }
  134. public readonly struct InputShape
  135. {
  136. /// <summary>
  137. /// Initializes a new instance of the <see cref="InputShape"/> struct.
  138. /// </summary>
  139. /// <param name="channel">The number of color channels in the input image.</param>
  140. /// <param name="width">The width of the input image in pixels.</param>
  141. /// <param name="height">The height of the input image in pixels.</param>
  142. public InputShape(int channel, int width, int height)
  143. {
  144. Channel = channel;
  145. Height = height;
  146. Width = width;
  147. }
  148. /// <summary>
  149. /// Gets the number of color channels in the input image.
  150. /// </summary>
  151. public int Channel { get; }
  152. /// <summary>
  153. /// Gets the height of the input image in pixels.
  154. /// </summary>
  155. public int Height { get; }
  156. /// <summary>
  157. /// Gets the width of the input image in pixels.
  158. /// </summary>
  159. public int Width { get; }
  160. }
  161. /// <summary>
  162. /// Enum representing the degrees of rotation.
  163. /// </summary>
  164. public enum RotationDegree
  165. {
  166. /// <summary>
  167. /// Represents the 0-degree rotation angle.
  168. /// </summary>
  169. _0,
  170. /// <summary>
  171. /// Represents the 90-degree rotation angle.
  172. /// </summary>
  173. _90,
  174. /// <summary>
  175. /// Represents the 180-degree rotation angle.
  176. /// </summary>
  177. _180,
  178. /// <summary>
  179. /// Represents the 270-degree rotation angle.
  180. /// </summary>
  181. _270,
  182. }
  183. }

下载 

源码下载

天天代码码天天
微信公众号
.NET 人工智能实践
注:本文转载自blog.csdn.net的天天代码码天天的文章"https://lw112190.blog.csdn.net/article/details/136036233"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top