C# Yolo+Onnx 号牌识别

参考

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

效果

C# Yolo+Onnx 号牌识别_第1张图片

C# Yolo+Onnx 号牌识别_第2张图片

C# Yolo+Onnx 号牌识别_第3张图片

C# Yolo+Onnx 号牌识别_第4张图片

 项目

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

C# Yolo+Onnx 号牌识别_第5张图片

 部分代码

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

        }

        /// 
        /// 图片拼接
        /// 
        /// 图片1
        /// 图片2
        /// 拼接后的图片
        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下载

你可能感兴趣的:(OpenCV,C#,AI,YOLO,C#yolo号牌识别)