C# Yolo+Onnx 号牌识别
参考
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);
}
}
}
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfhaafi
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13