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,
    }
}

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转三通道matStopwatch 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,}
}

下载 

源码下载

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/455134.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[软件工具]文档页数统计工具软件pdf统计页数word统计页数ppt统计页数图文打印店快速报价工具

文档页数统计工具软件——打印方面好帮手 在信息化时代&#xff0c;文档已成为我们工作、学习、生活中不可或缺的一部分。无论是学术论文、商业报告&#xff0c;还是个人日记&#xff0c;都需要我们对其进行有效的管理。而在这个过程中&#xff0c;文档页数统计工具软件就显得…

tee漏洞学习-翻译-2:探索 Qualcomm TrustZone的实现

原文&#xff1a;http://bits-please.blogspot.com/2015/08/exploring-qualcomms-trustzone.html 获取 TrustZone image 从两个不同的位置提取image 从手机设备本身从google factory image 已经root的Nexus 5设备&#xff0c;image存储在eMMC芯片上&#xff0c;并且eMMC芯片…

【网站项目】039菜匣子优选生鲜电商系统

&#x1f64a;作者简介&#xff1a;拥有多年开发工作经验&#xff0c;分享技术代码帮助学生学习&#xff0c;独立完成自己的项目或者毕业设计。 代码可以私聊博主获取。&#x1f339;赠送计算机毕业设计600个选题excel文件&#xff0c;帮助大学选题。赠送开题报告模板&#xff…

LeetCode、790. 多米诺和托米诺平铺【中等,二维DP,可转一维】

文章目录 前言LeetCode、790. 多米诺和托米诺平铺【中等&#xff0c;二维DP&#xff0c;可转一维】题目与分类思路二维解法二维转一维 资料获取 前言 博主介绍&#xff1a;✌目前全网粉丝2W&#xff0c;csdn博客专家、Java领域优质创作者&#xff0c;博客之星、阿里云平台优质…

数据库学习笔记2024/2/5

2. SQL 全称 Structured Query Language&#xff0c;结构化查询语言。操作关系型数据库的编程语言&#xff0c;定义了 一套操作关系型数据库统一标准 2.1 SQL通用语法 在学习具体的SQL语句之前&#xff0c;先来了解一下SQL语言的通用语法。 1). SQL语句可以单行或多行书写&…

MySQL学习一、库和表的基础操作

目录 一、常用数据类型 1.数值类型 2.字符串类型 3.日期类型 ​二、数据库的基础操作 三、表的基础操作 一、常用数据类型 1.数值类型 数值类型可以指定为无符号&#xff08;unsigned &#xff09;&#xff0c;但不建议取 2.字符串类型 3.日期类型 二、数据库的基础操作…

CentOS7搭建k8s-v1.28.6集群详情

文章目录 1.灌装集群节点操作系统1.1 设置hosts1.2 设置nameserver1.3 关闭防火墙1.4 关闭Selinux1.5 关闭Swap分区1.6 时间同步1.7 调整内核参数1.8 系统内核升级 2.安装Docker2.1 卸载旧Docker2.2 配置Docker软件源2.3 安装Docker 3.部署Kubernets集群3.1 设置 K8s 软件源3.2…

Node.js JSON Schema Ajv依赖库逐步介绍验证类型和中文错误提示

在构建应用程序时&#xff0c;数据的有效性是至关重要的。为了确保传入的数据符合预期的格式和规范&#xff0c;我们可以使用 Ajv&#xff08;Another JSON Schema Validator&#xff09;进行验证。在这篇博文中&#xff0c;我们将从头开始学习 Ajv&#xff0c;逐步介绍验证类型…

如何在Windows系统上部署docker

上次在Windows系统上部署成功Ubuntu系统&#xff0c;这次准备在Windows上部署docker desktop应用 这个应用软件类似于虚拟机&#xff0c;可以在该应用软件上部署多个镜像容器。其最直观的表现就是可以借用Windows和Ubuntu终端来访问docker“模拟的系统”。 Docker简介 Docke…

OCR升级版 — 微调EasyOCR实战

OCR是从图像中提取文本的有价值工具。然而&#xff0c;有时您使用的OCR在特定需求上的表现不如您所希望的那样好。如果您面临这样的问题&#xff0c;微调OCR引擎是解决的一种方法。在本教程中&#xff0c;我将向您展示如何微调EasyOCR&#xff0c;这是一个免费、开源的OCR引擎&…

Golang-Map有序输出——使用orderedmap库实现

前言 工作中遇到一个问题&#xff1a;需要导出一个MySQL表格&#xff0c;表格内容由sql查询得来。但现在发现&#xff0c;所导出的表格中&#xff0c;各列的顺序不确定。多次导出&#xff0c; 每一次的序列顺序也是不定的。 因此确定是后端&#xff0c;Map使用相关导致的问题。…

安卓动态链接库文件体积优化探索实践

背景介绍 应用安装包的体积影响着用户下载量、安装时长、用户磁盘占用量等多个方面&#xff0c;据Google Play统计&#xff0c;应用体积每增加6MB&#xff0c;安装的转化率将下降1%。 安装包的体积受诸多方面影响&#xff0c;针对dex、资源文件、so文件都有不同的优化策略&…