• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

C# Yolo+Onnx 号牌识别

武飞扬头像
lw
帮助5

参考

github.com/missxingwu/…
https://github.com/ivilson/Yolov7net
github.com/we0091234/C…

效果

学新通

学新通

学新通

学新通

 项目

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

            }

        }

    }
}

Demo下载

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhaafi
系列文章
更多 icon
同类精品
更多 icon
继续加载