C# PaddleInference 图片旋转角度检测

这篇具有很好参考价值的文章主要介绍了C# PaddleInference 图片旋转角度检测。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

效果

项目

代码

下载 


效果

C# PaddleInference 图片旋转角度检测,AI,OpenCV,C#,c#,开发语言,C# 图片旋转角度检测

项目

 VS2022+.net4.8+ OpenCvSharp4+Sdcb.PaddleInference

C# PaddleInference 图片旋转角度检测,AI,OpenCV,C#,c#,开发语言,C# 图片旋转角度检测

代码

using OpenCvSharp;
using Sdcb.PaddleInference;
using Sdcb.PaddleInference.Native;
using System;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace PaddleInference_图片旋转角度检测
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Bitmap bmp;
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string img = "";
        string startupPath = "";

        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;

        PaddlePredictor predictor;
        float rotateThreshold = 0.50f;
        InputShape defaultShape = new InputShape(3, 224, 224);

        private unsafe void Form1_Load(object sender, EventArgs e)
        {
            startupPath = Application.StartupPath;

            IntPtr _ptr = PaddleNative.PD_ConfigCreate();

            Encoding PaddleEncoding = Environment.OSVersion.Platform == PlatformID.Win32NT ? Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.ANSICodePage) : Encoding.UTF8;

            //设置推理模型路径
            String programPath = Application.StartupPath + "\\models\\inference.pdmodel";
            String paramsPath = Application.StartupPath + "\\models\\inference.pdiparams";

            byte[] programBytes = PaddleEncoding.GetBytes(programPath);
            byte[] paramsBytes = PaddleEncoding.GetBytes(paramsPath);
            fixed (byte* programPtr = programBytes)
            fixed (byte* paramsPtr = paramsBytes)

            PaddleNative.PD_ConfigSetModel(_ptr, (IntPtr)programPtr, (IntPtr)paramsPtr);
            PaddleNative.PD_ConfigEnableMKLDNN(_ptr);

            predictor = new PaddlePredictor(PaddleNative.PD_PredictorCreate(_ptr));
        }


        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 button2_Click(object sender, EventArgs e)
        {
            if (img == "") { return; }
            Mat src = OpenCvSharp.Extensions.BitmapConverter.ToMat(new Bitmap(pictureBox1.Image));
            Cv2.CvtColor(src, src, ColorConversionCodes.RGBA2RGB);//mat转三通道mat
            dt1 = DateTime.Now;

            Mat resized = ResizePadding(src, defaultShape);
            Mat normalized = Normalize(resized);

            using (PaddleTensor input = predictor.GetInputTensor(predictor.InputNames[0]))
            {
                input.Shape = new[] { 1, 3, normalized.Rows, normalized.Cols };
                float[] data = ExtractMat(normalized);
                input.SetData(data);
            }

            normalized.Dispose();
            resized.Dispose();

            if (!predictor.Run())
            {
                throw new Exception("PaddlePredictor(Classifier) run failed.");
            }

            RotationDegree r = RotationDegree._0;

            using (PaddleTensor output = predictor.GetOutputTensor(predictor.OutputNames[0]))
            {
                float[] softmax = output.GetData<float>();
                float max = softmax.Max();
                int maxIndex = Array.IndexOf(softmax, max);
                if (max > rotateThreshold)
                {
                    r = (RotationDegree)maxIndex;
                }
            }

            dt2 = DateTime.Now;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("图片旋转角度:" + r.ToString());
            sb.AppendLine("--------------------");
            sb.AppendLine("耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
            textBox1.Text = sb.ToString();
        }

        private float[] ExtractMat(Mat src)
        {
            int rows = src.Rows;
            int cols = src.Cols;
            float[] result = new float[rows * cols * 3];
            GCHandle resultHandle = default;
            try
            {
                resultHandle = GCHandle.Alloc(result, GCHandleType.Pinned);
                IntPtr resultPtr = resultHandle.AddrOfPinnedObject();
                for (int i = 0; i < src.Channels(); ++i)
                {
                    Mat dest = new Mat(rows, cols, MatType.CV_32FC1, resultPtr + i * rows * cols * sizeof(float));
                    Cv2.ExtractChannel(src, dest, i);
                    dest.Dispose();
                }
            }
            finally
            {
                resultHandle.Free();
            }
            return result;
        }

        private Mat ResizePadding(Mat src, InputShape shape)
        {
            OpenCvSharp.Size srcSize = src.Size();
            Mat roi = srcSize.Width / srcSize.Height > shape.Width / shape.Height ?
                src[0, srcSize.Height, 0, (int)Math.Floor(1.0 * srcSize.Height * shape.Width / shape.Height)] :
                src.Clone();
            double scaleRate = 1.0 * shape.Height / srcSize.Height;
            Mat resized = roi.Resize(new OpenCvSharp.Size(Math.Floor(roi.Width * scaleRate), shape.Height));
            if (resized.Width < shape.Width)
            {
                Cv2.CopyMakeBorder(resized, resized, 0, 0, 0, shape.Width - resized.Width, BorderTypes.Constant, Scalar.Black);
            }
            roi.Dispose();
            return resized;
        }

        private Mat Normalize(Mat src)
        {
            Mat normalized = new Mat();
            src.ConvertTo(normalized, MatType.CV_32FC3, 1.0 / 255);
            Mat[] bgr = normalized.Split();
            float[] scales = new[] { 2.0f, 2.0f, 2.0f };
            float[] means = new[] { 0.5f, 0.5f, 0.5f };
            for (int i = 0; i < bgr.Length; ++i)
            {
                bgr[i].ConvertTo(bgr[i], MatType.CV_32FC1, 1.0 * scales[i], (0.0 - means[i]) * scales[i]);
            }

            normalized.Dispose();

            Mat dest = new Mat();
            Cv2.Merge(bgr, dest);

            foreach (Mat channel in bgr)
            {
                channel.Dispose();
            }

            return dest;
        }

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

    }

    /// <summary>
    /// Represents the shape of input data for a rotation detection model.
    /// </summary>
    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.PaddleInference;
using Sdcb.PaddleInference.Native;
using System;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace PaddleInference_图片旋转角度检测
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Bitmap bmp;
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string img = "";
        string startupPath = "";

        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;

        PaddlePredictor predictor;
        float rotateThreshold = 0.50f;
        InputShape defaultShape = new InputShape(3, 224, 224);

        private unsafe void Form1_Load(object sender, EventArgs e)
        {
            startupPath = Application.StartupPath;

            IntPtr _ptr = PaddleNative.PD_ConfigCreate();

            Encoding PaddleEncoding = Environment.OSVersion.Platform == PlatformID.Win32NT ? Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.ANSICodePage) : Encoding.UTF8;

            //设置推理模型路径
            String programPath = Application.StartupPath + "\\models\\inference.pdmodel";
            String paramsPath = Application.StartupPath + "\\models\\inference.pdiparams";

            byte[] programBytes = PaddleEncoding.GetBytes(programPath);
            byte[] paramsBytes = PaddleEncoding.GetBytes(paramsPath);
            fixed (byte* programPtr = programBytes)
            fixed (byte* paramsPtr = paramsBytes)

            PaddleNative.PD_ConfigSetModel(_ptr, (IntPtr)programPtr, (IntPtr)paramsPtr);
            PaddleNative.PD_ConfigEnableMKLDNN(_ptr);

            predictor = new PaddlePredictor(PaddleNative.PD_PredictorCreate(_ptr));
        }


        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 button2_Click(object sender, EventArgs e)
        {
            if (img == "") { return; }
            Mat src = OpenCvSharp.Extensions.BitmapConverter.ToMat(new Bitmap(pictureBox1.Image));
            Cv2.CvtColor(src, src, ColorConversionCodes.RGBA2RGB);//mat转三通道mat
            dt1 = DateTime.Now;

            Mat resized = ResizePadding(src, defaultShape);
            Mat normalized = Normalize(resized);

            using (PaddleTensor input = predictor.GetInputTensor(predictor.InputNames[0]))
            {
                input.Shape = new[] { 1, 3, normalized.Rows, normalized.Cols };
                float[] data = ExtractMat(normalized);
                input.SetData(data);
            }

            normalized.Dispose();
            resized.Dispose();

            if (!predictor.Run())
            {
                throw new Exception("PaddlePredictor(Classifier) run failed.");
            }

            RotationDegree r = RotationDegree._0;

            using (PaddleTensor output = predictor.GetOutputTensor(predictor.OutputNames[0]))
            {
                float[] softmax = output.GetData<float>();
                float max = softmax.Max();
                int maxIndex = Array.IndexOf(softmax, max);
                if (max > rotateThreshold)
                {
                    r = (RotationDegree)maxIndex;
                }
            }

            dt2 = DateTime.Now;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("图片旋转角度:" + r.ToString());
            sb.AppendLine("--------------------");
            sb.AppendLine("耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
            textBox1.Text = sb.ToString();
        }

        private float[] ExtractMat(Mat src)
        {
            int rows = src.Rows;
            int cols = src.Cols;
            float[] result = new float[rows * cols * 3];
            GCHandle resultHandle = default;
            try
            {
                resultHandle = GCHandle.Alloc(result, GCHandleType.Pinned);
                IntPtr resultPtr = resultHandle.AddrOfPinnedObject();
                for (int i = 0; i < src.Channels(); ++i)
                {
                    Mat dest = new Mat(rows, cols, MatType.CV_32FC1, resultPtr + i * rows * cols * sizeof(float));
                    Cv2.ExtractChannel(src, dest, i);
                    dest.Dispose();
                }
            }
            finally
            {
                resultHandle.Free();
            }
            return result;
        }

        private Mat ResizePadding(Mat src, InputShape shape)
        {
            OpenCvSharp.Size srcSize = src.Size();
            Mat roi = srcSize.Width / srcSize.Height > shape.Width / shape.Height ?
                src[0, srcSize.Height, 0, (int)Math.Floor(1.0 * srcSize.Height * shape.Width / shape.Height)] :
                src.Clone();
            double scaleRate = 1.0 * shape.Height / srcSize.Height;
            Mat resized = roi.Resize(new OpenCvSharp.Size(Math.Floor(roi.Width * scaleRate), shape.Height));
            if (resized.Width < shape.Width)
            {
                Cv2.CopyMakeBorder(resized, resized, 0, 0, 0, shape.Width - resized.Width, BorderTypes.Constant, Scalar.Black);
            }
            roi.Dispose();
            return resized;
        }

        private Mat Normalize(Mat src)
        {
            Mat normalized = new Mat();
            src.ConvertTo(normalized, MatType.CV_32FC3, 1.0 / 255);
            Mat[] bgr = normalized.Split();
            float[] scales = new[] { 2.0f, 2.0f, 2.0f };
            float[] means = new[] { 0.5f, 0.5f, 0.5f };
            for (int i = 0; i < bgr.Length; ++i)
            {
                bgr[i].ConvertTo(bgr[i], MatType.CV_32FC1, 1.0 * scales[i], (0.0 - means[i]) * scales[i]);
            }

            normalized.Dispose();

            Mat dest = new Mat();
            Cv2.Merge(bgr, dest);

            foreach (Mat channel in bgr)
            {
                channel.Dispose();
            }

            return dest;
        }

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

    }

    /// <summary>
    /// Represents the shape of input data for a rotation detection model.
    /// </summary>
    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,
    }

}

下载 

Demo下载文章来源地址https://www.toymoban.com/news/detail-535342.html

到了这里,关于C# PaddleInference 图片旋转角度检测的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Opencv】PIL Opencv 向图片写入文字并旋转文字,Opencv图片旋转不截断,Opencv图片旋转不裁剪

    刚性变换: 只有物体的位置(平移变换)和朝向(旋转变换)发生改变,而形状不变,得到的变换称为刚性变换。刚性变换是最一般的变换。 使用透视变换,文字会扭曲失真。刚性变换就不会。 一些介绍: https://blog.csdn.net/liuweiyuxiang/article/details/86510191 创建一张空图写文字: 旋转

    2024年02月11日
    浏览(51)
  • opencv人与摄像头距离、角度检测

    参考: https://chtseng.wordpress.com/2018/09/18/%E5%A6%82%E4%BD%95%E4%BC%B0%E7%AE%97%E5%89%8D%E6%96%B9%E4%BA%BA%E7%89%A9%E7%9A%84%E8%B7%9D%E9%9B%A2/ https://blog.csdn.net/captain5339/article/details/128857313

    2024年02月14日
    浏览(41)
  • C# PaddleInference OCR 验证码识别

    目录 说明 效果 项目 测试图片 代码 下载  C# PaddleInference OCR 验证码识别 自己训练的模型,只针对测试图片类型,准确率99%    VS2022+.net4.8+OpenCvSharp4+Sdcb.PaddleInference using OpenCvSharp; using Sdcb.PaddleInference.Native; using Sdcb.PaddleInference; using System; using System.Collections.Generic; using System.

    2024年02月16日
    浏览(34)
  • C#实现图片对比-支持图片旋转

    虽然已经正式转JAVA了,但最近发现一个特别好的开源项目masuit,不仅提供很多简便的功能,还有图像的一些特殊操作功能。 之前我们比较图片应该都是使用的openCV,不过这个masuit,看上去也不错,而且代码使用简单,因此强烈推荐。 下面就实现一个简单图像对比。 首先添加

    2024年03月09日
    浏览(67)
  • OpenCV 图片旋转

    cv2.getRotationMatrix2D 获得仿射变化矩阵 cv2.warpAffine 进行仿射变化 参数说明: center 表示 中间点的位置 , -5 表示 逆时针 旋转5度, 1 表示进行等比列的 缩放 返回值: rot_mat 仿射变化矩阵 例如: 参数说明: img 表示输入的图片, rot_mat 表示仿射变化矩阵, (image.shape[1], image.sh

    2024年02月05日
    浏览(30)
  • [C++]使用纯opencv部署yolov8旋转框目标检测

    【官方框架地址】 https://github.com/ultralytics/ultralytics 【算法介绍】 YOLOv8是一种先进的对象检测算法,它通过单个神经网络实现了快速的物体检测。其中,旋转框检测是YOLOv8的一项重要特性,它可以有效地检测出不同方向和角度的物体。 旋转框检测的原理是通过预测物体的边界

    2024年04月26日
    浏览(35)
  • OPENCV C++(七)霍夫线检测+找出轮廓和外接矩形+改进旋转

    霍夫线检测  定义存放输出线的向量 此向量输出有距离,角度 因为检测的原理就是在变换霍夫空间里面去检测的,这里可以理解为极坐标 第3个参数是距离精度 第四个参数是角度精度,第五个是阈值,只有点超过90个才算一条线 在图中画线操作: 这里是画线操作  概率霍夫

    2024年02月13日
    浏览(47)
  • [C#]winform部署官方yolov8-obb旋转框检测的onnx模型

    【官方框架地址】 https://github.com/ultralytics/ultralytics 【算法介绍】 Yolov8-obb(You Only Look Once version 8 with Oriented Bounding Boxes)是一种先进的对象检测算法,它在传统的Yolov3和Yolov4基础上进行了优化,加入了OBB(Oriented Bounding Box)旋转框检测,能够更精确地检测并定位出目标物体的

    2024年01月20日
    浏览(85)
  • 图像旋转角度计算并旋转

    使用两张测试图片如下:   对于lena的图像测试结果如下:   另一张测试图片结果如下:    也可以使用下面代码进行测试: lena结果如下: 美女图片测试结果: 说明: 以上代码仅仅是讲解介绍了图像旋转的计算及矫正原理,实际上准确度受不同图像的影响较大,不过里面使用的相

    2024年01月25日
    浏览(45)
  • 【ArkTS】鸿蒙开发 在用户界面点击图片实现图片旋转和图片缩小动画

    为了实现图片点击旋转、缩放、位移等功能,我主要应用了多态样式:stateStyles()属性和动画animation()属性,具体用法可以参考官网给出的说明: stateStyles()属性: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V2/arkts-statestyles-0000001482592098-V2 animation()属性: https://devel

    2024年04月23日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包