看到这个文章时候请注意这个不涉及到车牌检测,这个仅仅是车牌颜色和车牌号识别,如果想涉及到车牌检测可以参考这个博客:[C#]winform部署yolov7+CRNN实现车牌颜色识别车牌号检测识别_c# yolo 车牌识别-CSDN博客
【训练源码】
https://github.com/we0091234/crnn_plate_recognition/tree/plate_color
车牌识别+车牌颜色用一个模型如图所示,右边是我们已经训练好的车牌字符识别模型,只需训练左边边颜色分支即可。模型结构:

训练颜色分支
- 
下载车牌颜色数据集,或者准备自己的数据集
 - 
准备你训练好的车牌字符识别模型
 - 
训练
python train_fix_color.py --weights saved_model/plate_rec.pth --train_path datasets/train --val_path datasets/val --model_path color_model结果保存再color_model文件夹中
 
测试demo
python demo_plate_color.py --model_path saved_model/plate_rec_color.pth --image_path images/test.jpg
        
结果是:

导出onnx
python export.py --weights saved_model/plate_rec_color.pth --save_path saved_model/plate_rec_color.onnx  --simplify
        onnx 推理
python onnx_infer.py --onnx_file saved_model/plate_rec_color.onnx  --image_path images/test.jpg
        【界面展示】

【效果展示】


特别注意:双层车牌需要自己提前分割合并,就是把上下2行文字合并为一行图片然后推理就可以识别颜色,否则可能识别出来的颜色不对。
【测试环境】
vs2019 netframework4.7.2 opencvsharp4.8.0 onnxruntime1.16
【部分实现代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;
namespace FIRC
{
    public partial class Form1 : Form
    {
        Bitmap bmp = null;
       PlateManager pm = new PlateManager();
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "图文件(*.*)|*.jpg;*.png;*.jpeg;*.bmp";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
              
                if(bmp!=null)
                {
                    bmp.Dispose();
                }
                bmp = new Bitmap(openFileDialog.FileName);
                pictureBox1.Image = bmp;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if(pictureBox1.Image==null)
            {
                return;
            }
            Stopwatch sw = new Stopwatch();
            sw.Start();
            var result = pm.Inference(bmp);
            sw.Stop();
            this.Text = "耗时" + sw.Elapsed.TotalSeconds + "秒";
            textBox1.Text = "车牌颜色:" + result.CarColor + "\r\n车牌号:" + result.CarNumber;
      
           
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            pm.LoadWeights();
        }
        private void btn_video_Click(object sender, EventArgs e)
        {
           
         
        }
    }
}
        【视频演示】
【源码下载】