C# Yolo+Onnx 号牌识别

该代码示例展示了如何利用Yolov5模型和OnnxRuntime在.NET环境中实现车牌检测,以及对检测到的车牌进行颜色和号码识别。用户可以选择图片,然后程序会显示识别出的车牌并提供相关信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

参考

效果

项目

代码

下载 


参考

https://github.com/missxingwu/net_yolov5_plate
https://github.com/ivilson/Yolov7net
https://github.com/we0091234/Chinese_license_plate_detection_recognition

效果

项目

VS2022+.net 4.8+OpenCvSharp4+Microsoft.ML.OnnxRuntime

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Onnx_号牌识别
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Yolov5 yolo;
        Yolov5Ocr yoloOcr;

        private void Form1_Load(object sender, EventArgs e)
        {
            yolo = new Yolov5("./assets/plate_detect_v7.onnx", inputName: "input");
            yolo.SetupYoloDefaultLabels();

            yoloOcr = new Yolov5Ocr("./assets/plate_rec_color.onnx", inputName: "images");
            yoloOcr.SetupYoloDefaultLabels();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string imgPath = "";

        private void button2_Click(object sender, EventArgs e)
        {
            pictureBox2.Image = null;
            textBox1.Text = "";

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            imgPath = ofd.FileName;
            pictureBox1.Image = new Bitmap(imgPath);

        }

        /// <summary>
        /// 图片拼接
        /// </summary>
        /// <param name="sourceImg">图片1</param>
        /// <param name="newImg">图片2</param>
        /// <returns>拼接后的图片</returns>
        Bitmap JoinImage(Image sourceImg, Image newImg)
        {
            int imgHeight = 0, imgWidth = 0;

            imgWidth = sourceImg.Width + newImg.Width;
            imgHeight = sourceImg.Height > newImg.Height ? sourceImg.Height : newImg.Height;

            Bitmap joinedBitmap = new Bitmap(imgWidth, imgHeight);
            using (Graphics graph = Graphics.FromImage(joinedBitmap))
            {
                graph.DrawImage(sourceImg, 0, 0, sourceImg.Width, sourceImg.Height);
                graph.DrawImage(newImg, sourceImg.Width, 0, newImg.Width, newImg.Height);
            }
            return joinedBitmap;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (imgPath == "")
            {
                return;
            }

            var image = System.Drawing.Image.FromFile(imgPath);
            var predictions = yolo.Predict(image);

            if (predictions.Count < 1) return;

            var graphics = Graphics.FromImage(image);
            int num = 0;
            foreach (var prediction in predictions) // iterate predictions to draw results
            {
                num++;
                double score = Math.Round(prediction.Score, 2);
                var labelRect = prediction.Rectangle;
                var twoLayers = (labelRect.Height / labelRect.Width) > 0.5;
                //定义截取矩形
                System.Drawing.Rectangle cropArea = new System.Drawing.Rectangle((int)labelRect.X < 0 ? 0 : (int)labelRect.X, (int)labelRect.Y < 0 ? 0 : (int)labelRect.Y, (int)labelRect.Width, (int)labelRect.Height);
                //定义Bitmap对象
                System.Drawing.Bitmap bmpImage = new System.Drawing.Bitmap(image);
                //进行裁剪
                System.Drawing.Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);

                if (twoLayers)
                {
                    var img_upper_H = labelRect.Height / 2;
                    var width = (int)labelRect.Width + 1;
                    var height = (int)img_upper_H;
                    Bitmap resultBitmap = new Bitmap(width, height);
                    using (Graphics g = Graphics.FromImage(resultBitmap))
                    {
                        Rectangle resultRectangle = new Rectangle(0, 0, width, height);
                        Rectangle sourceRectangle = new Rectangle(0, 0, width, height);
                        g.DrawImage(bmpCrop, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);
                    }

                    Bitmap resultBitmap1 = new Bitmap(width, height);
                    using (Graphics g = Graphics.FromImage(resultBitmap1))
                    {
                        Rectangle resultRectangle = new Rectangle(0, 0, width, height);
                        Rectangle sourceRectangle = new Rectangle(0, height, width, height);
                        g.DrawImage(bmpCrop, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);
                    }
                    bmpCrop = JoinImage(resultBitmap, resultBitmap1);
                }

                pictureBox2.Image = bmpCrop;

                var backtxt = "";
                var yoloOcrpredictions = yoloOcr.Predict(bmpCrop);
                if (yoloOcrpredictions.Length > 0)
                {
                    backtxt = "车牌颜色:" + yoloOcrpredictions[1] + "\r\n车牌号:" + yoloOcrpredictions[0];
                }

                //graphics.DrawRectangles(new Pen(prediction.Label.Color, 1), new[] { prediction.Rectangle });
                //var (x, y) = (prediction.Rectangle.X, prediction.Rectangle.Y + (labelRect.Height / 2));
                //graphics.DrawString($"{prediction.Label.Name} ({score})({backtxt})",
                //                new Font("Consolas", 14, GraphicsUnit.Pixel), new SolidBrush(Color.Red),
                //                new PointF(0, y));

                textBox1.Text = String.Format("{0}\r\nscore:{1}\r\n{2}", prediction.Label.Name, score, backtxt);

            }

        }

    }
}
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Onnx_号牌识别
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Yolov5 yolo;
        Yolov5Ocr yoloOcr;

        private void Form1_Load(object sender, EventArgs e)
        {
            yolo = new Yolov5("./assets/plate_detect_v7.onnx", inputName: "input");
            yolo.SetupYoloDefaultLabels();

            yoloOcr = new Yolov5Ocr("./assets/plate_rec_color.onnx", inputName: "images");
            yoloOcr.SetupYoloDefaultLabels();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string imgPath = "";

        private void button2_Click(object sender, EventArgs e)
        {
            pictureBox2.Image = null;
            textBox1.Text = "";

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            imgPath = ofd.FileName;
            pictureBox1.Image = new Bitmap(imgPath);

        }

        /// <summary>
        /// 图片拼接
        /// </summary>
        /// <param name="sourceImg">图片1</param>
        /// <param name="newImg">图片2</param>
        /// <returns>拼接后的图片</returns>
        Bitmap JoinImage(Image sourceImg, Image newImg)
        {
            int imgHeight = 0, imgWidth = 0;

            imgWidth = sourceImg.Width + newImg.Width;
            imgHeight = sourceImg.Height > newImg.Height ? sourceImg.Height : newImg.Height;

            Bitmap joinedBitmap = new Bitmap(imgWidth, imgHeight);
            using (Graphics graph = Graphics.FromImage(joinedBitmap))
            {
                graph.DrawImage(sourceImg, 0, 0, sourceImg.Width, sourceImg.Height);
                graph.DrawImage(newImg, sourceImg.Width, 0, newImg.Width, newImg.Height);
            }
            return joinedBitmap;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (imgPath == "")
            {
                return;
            }

            var image = System.Drawing.Image.FromFile(imgPath);
            var predictions = yolo.Predict(image);

            if (predictions.Count < 1) return;

            var graphics = Graphics.FromImage(image);
            int num = 0;
            foreach (var prediction in predictions) // iterate predictions to draw results
            {
                num++;
                double score = Math.Round(prediction.Score, 2);
                var labelRect = prediction.Rectangle;
                var twoLayers = (labelRect.Height / labelRect.Width) > 0.5;
                //定义截取矩形
                System.Drawing.Rectangle cropArea = new System.Drawing.Rectangle((int)labelRect.X < 0 ? 0 : (int)labelRect.X, (int)labelRect.Y < 0 ? 0 : (int)labelRect.Y, (int)labelRect.Width, (int)labelRect.Height);
                //定义Bitmap对象
                System.Drawing.Bitmap bmpImage = new System.Drawing.Bitmap(image);
                //进行裁剪
                System.Drawing.Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);

                if (twoLayers)
                {
                    var img_upper_H = labelRect.Height / 2;
                    var width = (int)labelRect.Width + 1;
                    var height = (int)img_upper_H;
                    Bitmap resultBitmap = new Bitmap(width, height);
                    using (Graphics g = Graphics.FromImage(resultBitmap))
                    {
                        Rectangle resultRectangle = new Rectangle(0, 0, width, height);
                        Rectangle sourceRectangle = new Rectangle(0, 0, width, height);
                        g.DrawImage(bmpCrop, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);
                    }

                    Bitmap resultBitmap1 = new Bitmap(width, height);
                    using (Graphics g = Graphics.FromImage(resultBitmap1))
                    {
                        Rectangle resultRectangle = new Rectangle(0, 0, width, height);
                        Rectangle sourceRectangle = new Rectangle(0, height, width, height);
                        g.DrawImage(bmpCrop, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);
                    }
                    bmpCrop = JoinImage(resultBitmap, resultBitmap1);
                }

                pictureBox2.Image = bmpCrop;

                var backtxt = "";
                var yoloOcrpredictions = yoloOcr.Predict(bmpCrop);
                if (yoloOcrpredictions.Length > 0)
                {
                    backtxt = "车牌颜色:" + yoloOcrpredictions[1] + "\r\n车牌号:" + yoloOcrpredictions[0];
                }

                //graphics.DrawRectangles(new Pen(prediction.Label.Color, 1), new[] { prediction.Rectangle });
                //var (x, y) = (prediction.Rectangle.X, prediction.Rectangle.Y + (labelRect.Height / 2));
                //graphics.DrawString($"{prediction.Label.Name} ({score})({backtxt})",
                //                new Font("Consolas", 14, GraphicsUnit.Pixel), new SolidBrush(Color.Red),
                //                new PointF(0, y));

                textBox1.Text = String.Format("{0}\r\nscore:{1}\r\n{2}", prediction.Label.Name, score, backtxt);

            }

        }

    }
}

下载 

Demo下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天代码码天天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值