ocr s,h,v

This commit is contained in:
2025-11-04 11:42:27 +08:00
parent 6c3d42328f
commit 42c72fabb0
5 changed files with 4944 additions and 4791 deletions

View File

@@ -161,7 +161,7 @@
// lbl_R
//
this.lbl_R.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lbl_R.Location = new System.Drawing.Point(508, 272);
this.lbl_R.Location = new System.Drawing.Point(507, 284);
this.lbl_R.Name = "lbl_R";
this.lbl_R.Size = new System.Drawing.Size(129, 29);
this.lbl_R.TabIndex = 5;
@@ -171,7 +171,7 @@
// lbl_Y
//
this.lbl_Y.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lbl_Y.Location = new System.Drawing.Point(253, 272);
this.lbl_Y.Location = new System.Drawing.Point(252, 284);
this.lbl_Y.Name = "lbl_Y";
this.lbl_Y.Size = new System.Drawing.Size(129, 29);
this.lbl_Y.TabIndex = 6;
@@ -181,7 +181,7 @@
// lbl_X
//
this.lbl_X.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lbl_X.Location = new System.Drawing.Point(12, 272);
this.lbl_X.Location = new System.Drawing.Point(11, 284);
this.lbl_X.Name = "lbl_X";
this.lbl_X.Size = new System.Drawing.Size(129, 29);
this.lbl_X.TabIndex = 7;
@@ -204,7 +204,7 @@
this.lbl_XShow.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.lbl_XShow.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lbl_XShow.Font = new System.Drawing.Font("宋体", 12F);
this.lbl_XShow.Location = new System.Drawing.Point(130, 272);
this.lbl_XShow.Location = new System.Drawing.Point(129, 284);
this.lbl_XShow.Name = "lbl_XShow";
this.lbl_XShow.Size = new System.Drawing.Size(117, 29);
this.lbl_XShow.TabIndex = 11;
@@ -215,7 +215,7 @@
this.lbl_YShow.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.lbl_YShow.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lbl_YShow.Font = new System.Drawing.Font("宋体", 12F);
this.lbl_YShow.Location = new System.Drawing.Point(371, 271);
this.lbl_YShow.Location = new System.Drawing.Point(370, 283);
this.lbl_YShow.Name = "lbl_YShow";
this.lbl_YShow.Size = new System.Drawing.Size(117, 29);
this.lbl_YShow.TabIndex = 12;
@@ -226,7 +226,7 @@
this.lbl_RShow.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.lbl_RShow.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lbl_RShow.Font = new System.Drawing.Font("宋体", 12F);
this.lbl_RShow.Location = new System.Drawing.Point(626, 272);
this.lbl_RShow.Location = new System.Drawing.Point(625, 284);
this.lbl_RShow.Name = "lbl_RShow";
this.lbl_RShow.Size = new System.Drawing.Size(117, 29);
this.lbl_RShow.TabIndex = 13;
@@ -235,7 +235,7 @@
// lbl_readOcrResult
//
this.lbl_readOcrResult.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lbl_readOcrResult.Location = new System.Drawing.Point(15, 309);
this.lbl_readOcrResult.Location = new System.Drawing.Point(14, 321);
this.lbl_readOcrResult.Name = "lbl_readOcrResult";
this.lbl_readOcrResult.Size = new System.Drawing.Size(127, 29);
this.lbl_readOcrResult.TabIndex = 14;
@@ -496,10 +496,10 @@
// txt_readOcrResultShow
//
this.txt_readOcrResultShow.Font = new System.Drawing.Font("宋体", 12F);
this.txt_readOcrResultShow.Location = new System.Drawing.Point(18, 341);
this.txt_readOcrResultShow.Location = new System.Drawing.Point(17, 353);
this.txt_readOcrResultShow.Multiline = true;
this.txt_readOcrResultShow.Name = "txt_readOcrResultShow";
this.txt_readOcrResultShow.Size = new System.Drawing.Size(772, 41);
this.txt_readOcrResultShow.Size = new System.Drawing.Size(772, 69);
this.txt_readOcrResultShow.TabIndex = 24;
this.txt_readOcrResultShow.Text = "等待读取字符...";
//
@@ -1033,7 +1033,7 @@
this.button1.Enabled = false;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button1.Location = new System.Drawing.Point(371, 207);
this.button1.Location = new System.Drawing.Point(370, 219);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(149, 46);
this.button1.TabIndex = 27;
@@ -1057,7 +1057,7 @@
this.btn_StarDet_manual.BackColor = System.Drawing.Color.LimeGreen;
this.btn_StarDet_manual.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btn_StarDet_manual.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btn_StarDet_manual.Location = new System.Drawing.Point(191, 207);
this.btn_StarDet_manual.Location = new System.Drawing.Point(190, 219);
this.btn_StarDet_manual.Name = "btn_StarDet_manual";
this.btn_StarDet_manual.Size = new System.Drawing.Size(149, 46);
this.btn_StarDet_manual.TabIndex = 25;

View File

@@ -22,6 +22,7 @@ using Cognex.VisionPro.ToolGroup;
using log4net;
using LogShowLib;
using OfficeOpenXml;
using OpenCvSharp;
using PaddleOCRSharp;
using System;
using System.Collections.Generic;
@@ -83,7 +84,7 @@ namespace TetraPackOCR
/// <summary>
/// 共印依据 QSV Design, Layers, Colours,产品规格
/// </summary>
string Sequence, QSV, Design, Layers, Colours, ProductStandard, ord;
string Sequence, QSV, Colours, ProductStandard, ord;
/// <summary>
/// 幅数 包材宽 梯度 X距离 Y距离
/// </summary>
@@ -104,7 +105,7 @@ namespace TetraPackOCR
private string[] verocr = new string[20];
bool Coprinted_ordeFlag = false; //是否为共印订单旗帜
//bool PlcContinueFlag = false; //PLC状态旗帜
bool PlcContinueFlag = false; //PLC状态旗帜
int mMatchingStr = 0;//接收当前OCR拍照位置
List<float> ocrAcc = new List<float>();
@@ -189,7 +190,6 @@ namespace TetraPackOCR
foreach (var iteam in tsk)
{
iteam.Start();
}
TaskFactory tskFactory = new TaskFactory();
@@ -267,6 +267,7 @@ namespace TetraPackOCR
{
try
{
cam0Opened = false;
DeviceList = Enumerator.EnumerateDevices(); //发现设备,搜索所有大华相机
// m_dev_cam0 = Enumerator.GetDeviceByIndex(0);//通过索引获取
@@ -328,15 +329,20 @@ namespace TetraPackOCR
{
// 开启采集失败
log.Error("开启采集失败");
return;
}
cam0Opened = true;
log.Info("OCR相机加载完毕");
}
catch (Exception ex)
{
cam0Opened = false;
log.Error("OCR相机加载失败");
m_dev_cam0 = null;
}
}
private bool cam0Opened = false;
private bool cam1Opened = false;
#endregion
#region 1
@@ -388,6 +394,7 @@ namespace TetraPackOCR
{
try
{
cam1Opened = false;
DeviceList = Enumerator.EnumerateDevices(); //发现设备,搜索所有大华相机
//m_dev_cam1 = Enumerator.GetDeviceByIndex(1);//通过索引获取
@@ -447,14 +454,16 @@ namespace TetraPackOCR
m_dev_cam1.TriggerSet.Open(TriggerSourceEnum.Software);
if (!m_dev_cam1.GrabUsingGrabLoopThread())
{
// 开启采集失败
log.Error("开启采集失败");
return;
}
cam1Opened = true;
log.Info("定位相机加载完毕");
}
catch (Exception ex)
{
cam1Opened = false;
log.Error("定位相机加载失败");
m_dev_cam1 = null;
}
@@ -607,6 +616,7 @@ namespace TetraPackOCR
//初始化OCR
Engine = new PaddleOCREngine(config, OcrParameter);
log.Info("OCR模型初始化完成");
}
catch (Exception ex)
{
@@ -651,6 +661,8 @@ namespace TetraPackOCR
}
private bool orderLoaded = false;
private Dictionary<int, List<string>> ocrTextRequest = new Dictionary<int, List<string>>();
private Dictionary<int, string> ocrTextDesign = new Dictionary<int, string>();
private Dictionary<string, bool> ocrTextResult = new Dictionary<string, bool>();
/// <summary>
/// 此按钮事件主要是获取excel表格内的数据
/// 包括定位偏移量,需要验证的字符
@@ -667,6 +679,7 @@ namespace TetraPackOCR
btn_StarDet_manual.BackColor = Color.LightGray;
listBox1.Items.Clear();
ocrTextRequest.Clear();
ocrTextDesign.Clear();
ClearData();
ClearDataShow();
this.Invoke(new Action(() =>
@@ -717,6 +730,7 @@ namespace TetraPackOCR
sheet1 = package.Workbook.Worksheets["P2生成数据"];
sheet2 = package.Workbook.Worksheets["QSV对应产品规格和梯度"];
sheet3 = package.Workbook.Worksheets["产品规格对应排布方式"];
string FirstLayers = "";
//根据订单遍历sheet1 找Sequence QSV NumOFLanes 字符
for (int i = 1; i < sheet1.Dimension.Rows; i++)
{
@@ -731,8 +745,7 @@ namespace TetraPackOCR
NumberOfLanes = Convert.ToInt32(sheet1.Cells[i, 5].Value.ToString());
log.Info("当前订单Number Of Lanes:" + NumberOfLanes);
this.lbl_NOFShow.Text = sheet1.Cells[i, 5].Value.ToString();
Design = sheet1.Cells[i, 7].Value.ToString();
Layers = sheet1.Cells[i, 8].Value.ToString();
FirstLayers = sheet1.Cells[i, 8].Value.ToString();
Colours = sheet1.Cells[i, 9].Value.ToString();
int num = 0;
@@ -749,6 +762,7 @@ namespace TetraPackOCR
{
int lanInt = Convert.ToInt32(lan);
ocrTextRequest[lanInt] = ExecelGetOcrText(design, layers);
ocrTextDesign[lanInt] = design;
}
listBox1.Items.Add(sheet1.Cells[i + k, 6].Value.ToString());
listBox1.Items.Add(string.Join(",", ocrTextRequest[Convert.ToInt32(tmp[0])]));
@@ -796,7 +810,7 @@ namespace TetraPackOCR
{
if (sheet3.Cells[n, 1].Value.ToString() == ProductStandard)
{
string str = sheet3.Cells[n + Layers.Split(',').Length - 1, 4].Value.ToString();
string str = sheet3.Cells[n + FirstLayers.Split(',').Length - 1, 4].Value.ToString();
string[] x_y = str.Split(',');
string[] X = x_y[0].Split(':');
string[] Y = x_y[1].Split(':');
@@ -839,6 +853,7 @@ namespace TetraPackOCR
sheet1 = package.Workbook.Worksheets["P2生成数据"];
sheet2 = package.Workbook.Worksheets["QSV对应产品规格和梯度"];
sheet3 = package.Workbook.Worksheets["产品规格对应排布方式"];
string firstLayers = "";
//根据订单遍历sheet1 找QSV NumOFLanes 字符
for (int i = 1; i < sheet1.Dimension.Rows; i++)
{
@@ -854,13 +869,14 @@ namespace TetraPackOCR
this.lbl_NOFShow.Text = sheet1.Cells[i, 5].Value.ToString();
//获取字符 并显示在界面还需处理先预留在这
Design = sheet1.Cells[i, 7].Value.ToString();
Layers = sheet1.Cells[i, 8].Value.ToString();
string design = sheet1.Cells[i, 7].Value.ToString();
firstLayers = sheet1.Cells[i, 8].Value.ToString();
Colours = sheet1.Cells[i, 9].Value.ToString();
for (int lan = 1; lan <= NumberOfLanes; lan++)
{
ocrTextRequest[lan] = ExecelGetOcrText(Design, Layers);
ocrTextRequest[lan] = ExecelGetOcrText(design, firstLayers);
ocrTextDesign[lan] = design;
}
listBox1.Items.Add(sheet1.Cells[i, 6].Value.ToString());
listBox1.Items.Add(string.Join(",", ocrTextRequest[1]));
@@ -903,7 +919,7 @@ namespace TetraPackOCR
{
if (sheet3.Cells[n, 1].Value.ToString() == ProductStandard)
{
string str = sheet3.Cells[n + Layers.Split(',').Length - 1, 4].Value.ToString();
string str = sheet3.Cells[n + firstLayers.Split(',').Length - 1, 4].Value.ToString();
string[] x_y = str.Split(',');
string[] X = x_y[0].Split(':');
string[] Y = x_y[1].Split(':');
@@ -1109,8 +1125,22 @@ namespace TetraPackOCR
m_dev_cam1.ExecuteSoftwareTrigger();
log.Info("手动触发定位");
}
private bool autorunFlag = false;
private void btn_StarDet_manual_Click(object sender, EventArgs e)
{
if (cam0Opened == false || cam1Opened == false)
{
MessageBox.Show("相机未打开,无法进行检测,请检查相机连接及状态!");
return;
}
if (PlcContinueFlag == false)
{
MessageBox.Show("PLC未处于运行状态无法进行检测请检查PLC连接及状态");
return;
}
autorunFlag = check_Autorun.Checked;
btn_StarDet_manual.Enabled = false;
button1.Enabled = true;
btn_StarDet_manual.BackColor = Color.LightGray;
@@ -1168,7 +1198,7 @@ namespace TetraPackOCR
void ClearData()
{
QSV = null; Design = null; Layers = null; Colours = null; ProductStandard = null; ord = null;
QSV = null; Colours = null; ProductStandard = null; ord = null;
NumberOfLanes = 0; width = 0; Gradient = 0; DistX = 0; DistY = 0;
}
/// <summary>
@@ -1402,46 +1432,7 @@ namespace TetraPackOCR
return sortedPoints;
}
//ai suggest① 先按 y 排序 → ② 一次扫描把相邻且 Δy<50 的点归并成同一行 → ③ 每行内部按 x 排序 → ④ 按行收集。
//List<Point> paixu(List<Point> points)
//{
// const int rowDistance = 50;
// if (points.Count == 0) return new List<Point>();
// // 1. 先按纵坐标排序
// var src = points.OrderBy(p => p.y).ThenBy(p => p.x).ToList();
// var rows = new List<List<Point>>();
// List<Point> curRow = new List<Point> { src[0] };
// float baseY = src[0].y;
// // 2. 一次扫描分好行
// for (int i = 1; i < src.Count; i++)
// {
// if (Math.Abs(src[i].y - baseY) < rowDistance)
// {
// curRow.Add(src[i]);
// }
// else
// {
// rows.Add(curRow);
// curRow = new List<Point> { src[i] };
// baseY = src[i].y;
// }
// }
// rows.Add(curRow); // 别忘了最后一行
// // 3. 行内再按 x 排一次(虽然 OrderBy 已做过,保险)
// foreach (var row in rows)
// row.Sort((a, b) => a.x.CompareTo(b.x));
// // 4. 按行先后汇总
// var sorted = new List<Point>();
// foreach (var row in rows)
// sorted.AddRange(row);
// return sorted;
//}
#endregion
#region OCR结果处理
@@ -1454,7 +1445,17 @@ namespace TetraPackOCR
Invoke(new OcrResultDelegate(OCRResult), new object[] { bmp });
return;
}
ocrTextResult = new Dictionary<string, bool>();
if (!ocrTextRequest.ContainsKey(mMatchingStr))
{
log.Error($"未知列{mMatchingStr},舍弃图片");
NoticePLCCompleteOCR();
return;
}
foreach (var item in ocrTextRequest[mMatchingStr])
{
ocrTextResult[item] = false;
}
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bytes = ms.GetBuffer();
@@ -1476,89 +1477,228 @@ namespace TetraPackOCR
CutPicture(SaveImageFileOCR + '\\' + strtimef + '\\' + strTime + ".bmp", 600, 1800, 3200, 1600);
//List<PointD> temps = new List<PointD>();
if (Engine == null)
{
log.Error("Engine未初始化");
NoticePLCCompleteOCR();
return;
}
GetOCRImage getOCRImage = new GetOCRImage();
GetOCRImage.FileTimeInfo file = getOCRImage.GetLatesFileImageName(SaveImageFileOCR + '\\' + strtimef, ".bmp");
if (file != null)
{
byte[] ocrimagebyte = File.ReadAllBytes(file.FileName);
Bitmap Bmp = new Bitmap(new MemoryStream(ocrimagebyte));
List<string> lastocr = new List<string>();
OCRResult ocrResult = Engine.DetectText(ocrimagebyte);
// log.Debug("读取原始数据1" + ocrResult.JsonText);
List<Point> pointsList = new List<Point>();
string[] sd = Design.Split('-');
foreach (var item in ocrResult.TextBlocks) //将检测框绘制在图片上
Mat bgr = Cv2.ImRead(file.FileName, ImreadModes.Color); // 读入 BGR
if (bgr.Empty())
{
if (item.Text.Contains(sd[1]))
{
using (Graphics g = Graphics.FromImage(Bmp))
{
g.DrawPolygon(new Pen(Brushes.Red, 2), item.BoxPoints.Select(x => new PointF() { X = x.X, Y = x.Y }).ToArray());
}
//lastocr.Add(item.Text);
log.Error("OCR图片转换失败");
NoticePLCCompleteOCR();
return;
}
Mat hsv = new Mat();
Cv2.CvtColor(bgr, hsv, ColorConversionCodes.BGR2HSV); // 1. 转 HSV
Mat[] channels = Cv2.Split(hsv); // 2. 拆通道
Mat h = channels[0]; // Hue 0-180
Mat s = channels[1]; // Saturation 0-255
Mat v = channels[2]; // Value 0-255
pointsList.Add(new Point(item.BoxPoints[0].X, item.BoxPoints[0].Y, item.Text));
OCRTextResult hResult = null;
OCRTextResult sResult = null;
OCRTextResult vResult = null;
bool matchOK = false;
PointF[] pointsTotal = null;
sResult = OCRBytes(s.ImEncode(".png"));
if (sResult == null)
{
log.Error("s通道OCR字符识别失败");
NoticePLCCompleteOCR();
return;
}
txt_readOcrResultShow.Text = sResult.text;
log.Info("字符读取结果:" + sResult.text);
matchOK = sResult.match;
pointsTotal = sResult.points;
if (!matchOK)
{
hResult = OCRBytes(h.ImEncode(".png"));
if (hResult == null)
{
log.Error("h通道OCR字符识别失败");
NoticePLCCompleteOCR();
return;
}
txt_readOcrResultShow.Text += "\n" + hResult.text;
log.Info("字符读取结果:" + hResult.text);
matchOK = hResult.match;
if (hResult.points != null && hResult.points.Length > 0)
{
List<PointF> pp1 = hResult.points.ToList();
List<PointF> pp = pointsTotal.ToList();
pp.AddRange(pp1);
pointsTotal = pp.Distinct().ToArray();
}
}
if (!matchOK)
{
vResult = OCRBytes(v.ImEncode(".png"));
if (vResult == null)
{
log.Error("v通道OCR字符识别失败");
NoticePLCCompleteOCR();
return;
}
txt_readOcrResultShow.Text += "\n" + vResult.text;
log.Info("字符读取结果:" + vResult.text);
matchOK = vResult.match;
if (vResult.points != null && vResult.points.Length > 0)
{
List<PointF> pp1 = vResult.points.ToList();
List<PointF> pp = pointsTotal.ToList();
pp.AddRange(pp1);
pointsTotal = pp.Distinct().ToArray();
}
}
List<Point> paixujeguo = paixu(pointsList);
foreach (var it in paixujeguo)
if (pointsTotal != null && pointsTotal.Length > 0)
{
lastocr.Add(it.txt);
byte[] ocrimagebyte = File.ReadAllBytes(file.FileName);
Bitmap Bmp = new Bitmap(new MemoryStream(ocrimagebyte));
using (Graphics g = Graphics.FromImage(Bmp))
{
g.DrawPolygon(new Pen(Brushes.Red, 2), pointsTotal.Select(x => new PointF() { X = x.X, Y = x.Y }).ToArray());
}
Ocr_picBox.BackgroundImage = null;
Ocr_picBox.BackgroundImage = Bmp;
int m1 = ocrTextResult.Select(x => x.Value == true).Count();
log.Info($"OCR识别完成匹配成功{m1}个字符,匹配失败{ocrTextResult.Count - m1}个字符");
ShowTheLansRs(mMatchingStr, (float)m1 / (float)ocrTextResult.Count);
}
//byte[] ocrimagebyte = File.ReadAllBytes(file.FileName);
//Bitmap Bmp = new Bitmap(new MemoryStream(ocrimagebyte));
//List<string> lastocr = new List<string>();
//OCRResult ocrResult = Engine.DetectText(ocrimagebyte);
//// log.Debug("读取原始数据1" + ocrResult.JsonText);
//List<Point> pointsList = new List<Point>();
//string[] sd = Design.Split('-');
//foreach (var item in ocrResult.TextBlocks) //将检测框绘制在图片上
//{
// if (item.Text.Contains(sd[1]))
// {
// using (Graphics g = Graphics.FromImage(Bmp))
// {
// g.DrawPolygon(new Pen(Brushes.Red, 2), item.BoxPoints.Select(x => new PointF() { X = x.X, Y = x.Y }).ToArray());
// }
// //lastocr.Add(item.Text);
// pointsList.Add(new Point(item.BoxPoints[0].X, item.BoxPoints[0].Y, item.Text));
// }
//}
//List<Point> paixujeguo = paixu(pointsList);
//foreach (var it in paixujeguo)
//{
// lastocr.Add(it.txt);
//}
//对图像显示区更新
Ocr_picBox.BackgroundImage = null;
Ocr_picBox.BackgroundImage = Bmp;
txt_readOcrResultShow.Clear();
////对图像显示区更新
//Ocr_picBox.BackgroundImage = null;
//Ocr_picBox.BackgroundImage = Bmp;
if (!Coprinted_ordeFlag) //判断是否为共印订单
{
txt_readOcrResultShow.Text = GETOCR(lastocr, Design);
log.Info("字符读取结果:" + GETOCR(lastocr, Design));
//txt_readOcrResultShow.Clear();
int distance = CalculateAcc(ExecelGetOcrDealWithForShow(Design, Layers), GETOCR(lastocr, Design));
float maxLength = Math.Max(ExecelGetOcrDealWithForShow(Design, Layers).Length, GETOCR(lastocr, Design).Length);
float strSimilarity = (maxLength - distance) / maxLength;
ShowTheLansRs(mMatchingStr, strSimilarity);
}
else
{
txt_readOcrResultShow.Text = GETOCR(lastocr, Design);
log.Info("字符读取结果:" + GETOCR(lastocr, Design));
//if (!Coprinted_ordeFlag) //判断是否为共印订单
//{
// txt_readOcrResultShow.Text = GETOCR(lastocr, Design);
// log.Info("字符读取结果:" + GETOCR(lastocr, Design));
int distance = CalculateAcc(verocr[mMatchingStr], GETOCR(lastocr, Design));
float maxLength = Math.Max(verocr[mMatchingStr].Length, GETOCR(lastocr, Design).Length);
float strSimilarity = (maxLength - distance) / maxLength;
// int distance = CalculateAcc(ExecelGetOcrDealWithForShow(Design, Layers), GETOCR(lastocr, Design));
// float maxLength = Math.Max(ExecelGetOcrDealWithForShow(Design, Layers).Length, GETOCR(lastocr, Design).Length);
// float strSimilarity = (maxLength - distance) / maxLength;
// ShowTheLansRs(mMatchingStr, strSimilarity);
//}
//else
//{
// txt_readOcrResultShow.Text = GETOCR(lastocr, Design);
// log.Info("字符读取结果:" + GETOCR(lastocr, Design));
ShowTheLansRs(mMatchingStr, strSimilarity);
// int distance = CalculateAcc(verocr[mMatchingStr], GETOCR(lastocr, Design));
// float maxLength = Math.Max(verocr[mMatchingStr].Length, GETOCR(lastocr, Design).Length);
// float strSimilarity = (maxLength - distance) / maxLength;
}
// ShowTheLansRs(mMatchingStr, strSimilarity);
if (this.check_Autorun.Checked == false)
{
byte[] datas = DataConverter.FloatToByte(0.0f);
//}
cc24?.NotifyCamAcqComplete(1); //通知PLC OCR相机已完成采集
cc24?.NotifyCamInspectionComplete(1, datas);
}
NoticePLCCompleteOCR();
}
}
catch (Exception ex)
{
log.Error(ex.Message + "未检测到字符");
if (this.check_Autorun.Checked == false)
NoticePLCCompleteOCR();
}
}
private void NoticePLCCompleteOCR()
{
if (autorunFlag == false)
{
byte[] datax = DataConverter.FloatToByte(0.0f, true);
cc24?.NotifyCamInspectionComplete(1, datax);
cc24?.NotifyCamAcqComplete(1);
}
}
private OCRTextResult OCRBytes(byte[] ocrimagebyte)
{
try
{
OCRTextResult result = new OCRTextResult();
List<string> lastocr = new List<string>();
OCRResult ocrResult = Engine.DetectText(ocrimagebyte);
List<Point> pointsList = new List<Point>();
string[] sd = ocrTextDesign[mMatchingStr].Split('-');
foreach (var item in ocrResult.TextBlocks) //将检测框绘制在图片上
{
byte[] datax = DataConverter.FloatToByte(0.0f, true);
cc24?.NotifyCamInspectionComplete(1, datax);
cc24?.NotifyCamAcqComplete(1);
if (item.Text.Contains(sd[1]))
{
result.points = item.BoxPoints.Select(x => new PointF() { X = x.X, Y = x.Y }).ToArray();
pointsList.Add(new Point(item.BoxPoints[0].X, item.BoxPoints[0].Y, item.Text));
}
}
List<Point> paixujeguo = paixu(pointsList);
foreach (var it in paixujeguo)
{
lastocr.Add(it.txt);
}
result.text = GETOCR(lastocr, ocrTextDesign[mMatchingStr]);
foreach (var item in ocrTextResult)
{
if (result.text.Contains(item.Key))
ocrTextResult[item.Key] = true;
}
result.match = true;
foreach (var item in ocrTextResult)
{
if (!item.Value)
result.match = false;
}
return result;
}
catch (Exception ex)
{
log.Error(ex.Message);
return null;
}
}
@@ -1952,7 +2092,6 @@ namespace TetraPackOCR
}
ocrAcc.Clear();
}
}
#endregion
@@ -2026,13 +2165,13 @@ namespace TetraPackOCR
}
if (e.ProtocolStatus == CogNdmConnectionStatusConstants.Connected)
{
//PlcContinueFlag = true;
PlcContinueFlag = true;
log.Info("PLC已连接PC,可以进行相关操作");
ttls_PCLStatusShow.Visible = true;
}
else if (e.ProtocolStatus != CogNdmConnectionStatusConstants.Connecting)
{
//PlcContinueFlag = false;
PlcContinueFlag = false;
log.Info("PLC已断开PC请查看相关设备是否连接");
ttls_PCLStatusShow.Visible = false;
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TetraParkOCR
{
internal class OCRTextResult
{
public bool match;
public string text;
public PointF[] points;
}
}

View File

@@ -297,6 +297,7 @@
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="HPoint.cs" />
<Compile Include="OCRTextResult.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">