Commit 25815d1d 张东亮

ocr标签顺序修改

1 个父辈 90c1bd42
...@@ -447,11 +447,10 @@ namespace BLL ...@@ -447,11 +447,10 @@ namespace BLL
ocrlist.Add(o); ocrlist.Add(o);
//ocrcode.Add(code[j]); //ocrcode.Add(code[j]);
} }
int ocrCount= ocrlist.Count;
if (ocrlist.Count > 0) if (ocrlist.Count > 0)
{ {
LogNet.log.Info($"{mateTemp[index].Name} 开始OCR匹配"); LogNet.log.Info($"{mateTemp[index].Name} 开始OCR匹配");
for (int i = 0; i < ocrlist.Count; i++) for (int i = 0; i < ocrlist.Count; i++)
{ {
List<MaterialCodeMatch> codeMatch = mateTemp[index].Match.FindAll(match => match.CodeID == ocrlist[i].ID); List<MaterialCodeMatch> codeMatch = mateTemp[index].Match.FindAll(match => match.CodeID == ocrlist[i].ID);
...@@ -468,6 +467,7 @@ namespace BLL ...@@ -468,6 +467,7 @@ namespace BLL
if (!keyword.ContainsKey(key)) if (!keyword.ContainsKey(key))
{ {
keyword.Add(key, matchKey[key]); keyword.Add(key, matchKey[key]);
ocrCount--;
LogNet.log.Info($"{mateTemp[index].Name} OCR匹配 [{key}={matchKey[key]}]"); LogNet.log.Info($"{mateTemp[index].Name} OCR匹配 [{key}={matchKey[key]}]");
} }
} }
...@@ -475,6 +475,10 @@ namespace BLL ...@@ -475,6 +475,10 @@ namespace BLL
} }
} }
} }
if(ocrCount==0&& !isMatch)
{
isMatch = true;
}
LogNet.log.Info($"{mateTemp[index].Name} 结束OCR匹配 "); LogNet.log.Info($"{mateTemp[index].Name} 结束OCR匹配 ");
} }
} }
......
...@@ -40,40 +40,39 @@ ...@@ -40,40 +40,39 @@
// //
this.textBox1.Dock = System.Windows.Forms.DockStyle.Top; this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.textBox1.Location = new System.Drawing.Point(0, 0); this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4); this.textBox1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.textBox1.Name = "textBox1"; this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(1006, 38); this.textBox1.Size = new System.Drawing.Size(575, 25);
this.textBox1.TabIndex = 0; this.textBox1.TabIndex = 0;
// //
// button1 // button1
// //
this.button1.Location = new System.Drawing.Point(14, 59); this.button1.Location = new System.Drawing.Point(8, 33);
this.button1.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4); this.button1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.button1.Name = "button1"; this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(196, 53); this.button1.Size = new System.Drawing.Size(112, 29);
this.button1.TabIndex = 1; this.button1.TabIndex = 1;
this.button1.Text = "打开"; this.button1.Text = "open";
this.button1.UseVisualStyleBackColor = true; this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click); this.button1.Click += new System.EventHandler(this.button1_Click);
// //
// label1 // label1
// //
this.label1.AutoSize = true; this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(486, 84); this.label1.Location = new System.Drawing.Point(278, 47);
this.label1.Margin = new System.Windows.Forms.Padding(5, 0, 5, 0);
this.label1.Name = "label1"; this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 28); this.label1.Size = new System.Drawing.Size(55, 15);
this.label1.TabIndex = 7; this.label1.TabIndex = 7;
this.label1.Text = "label1"; this.label1.Text = "label1";
// //
// button4 // button4
// //
this.button4.Location = new System.Drawing.Point(253, 59); this.button4.Location = new System.Drawing.Point(145, 33);
this.button4.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4); this.button4.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.button4.Name = "button4"; this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(196, 53); this.button4.Size = new System.Drawing.Size(112, 29);
this.button4.TabIndex = 8; this.button4.TabIndex = 8;
this.button4.Text = "ocr识别"; this.button4.Text = "recognize";
this.button4.UseVisualStyleBackColor = true; this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click); this.button4.Click += new System.EventHandler(this.button4_Click);
// //
...@@ -81,24 +80,25 @@ ...@@ -81,24 +80,25 @@
// //
this.listBox1.Dock = System.Windows.Forms.DockStyle.Bottom; this.listBox1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.listBox1.FormattingEnabled = true; this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 27; this.listBox1.ItemHeight = 15;
this.listBox1.Location = new System.Drawing.Point(0, 150); this.listBox1.Location = new System.Drawing.Point(0, 82);
this.listBox1.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
this.listBox1.Name = "listBox1"; this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(1006, 571); this.listBox1.Size = new System.Drawing.Size(575, 319);
this.listBox1.TabIndex = 9; this.listBox1.TabIndex = 9;
// //
// Paddle // Paddle
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(14F, 27F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1006, 721); this.ClientSize = new System.Drawing.Size(575, 401);
this.Controls.Add(this.listBox1); this.Controls.Add(this.listBox1);
this.Controls.Add(this.button4); this.Controls.Add(this.button4);
this.Controls.Add(this.label1); this.Controls.Add(this.label1);
this.Controls.Add(this.button1); this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1); this.Controls.Add(this.textBox1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4); this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.Name = "Paddle"; this.Name = "Paddle";
this.Text = "PaddleOcr"; this.Text = "PaddleOcr";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
......
using log4net; using log4net;
using log4net.Util;
using PaddleOCRSharp; using PaddleOCRSharp;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text; using System.Text;
namespace paddleOCR namespace paddleOCR
...@@ -79,7 +83,7 @@ namespace paddleOCR ...@@ -79,7 +83,7 @@ namespace paddleOCR
/// ///
{ {
RotateBox container = new RotateBox(); RotateBox container = new RotateBox();
flag = VisionAPI.eyemNNInstanceSegment(image,0.15f, ref container, out tpDstImg); flag = VisionAPI.eyemNNInstanceSegment(image, 0.15f, ref container, out tpDstImg);
log.Info($"标签分割完成,rtncode={flag}"); log.Info($"标签分割完成,rtncode={flag}");
Bitmap b = VisionAPI.eyemCvtToBitmap(tpDstImg); Bitmap b = VisionAPI.eyemCvtToBitmap(tpDstImg);
log.Info($"转换为Bitmap"); log.Info($"转换为Bitmap");
...@@ -132,24 +136,81 @@ namespace paddleOCR ...@@ -132,24 +136,81 @@ namespace paddleOCR
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
foreach (var dir in directoryInfo.GetDirectories()) foreach (var dir in directoryInfo.GetDirectories())
{ {
Dictionary<float,StringBuilder> pairs = new Dictionary<float, StringBuilder>(); float maxScore = 0f;
List<TextBlock> list = new List<TextBlock>();
foreach (var item in dir.GetFiles()) foreach (var item in dir.GetFiles())
{ {
float score = 0f; float score = 0f;
StringBuilder sb = new StringBuilder();
var imagebyte = File.ReadAllBytes(item.FullName); var imagebyte = File.ReadAllBytes(item.FullName);
var res = engine.DetectText(imagebyte); var res = engine.DetectText(imagebyte);
foreach (var text in res.TextBlocks) StringBuilder sb = new StringBuilder();
res.TextBlocks.ForEach(s => { score += s.Score; sb.AppendLine(s.Text); });
log.Info($"【{item.FullName}】识别结果:【{score}】【{sb.ToString().Replace("\r", "").Replace("\n", ";")}】");
if (score > maxScore)
{ {
score += text.Score; maxScore = score;
sb.AppendLine(text.Text); list = res.TextBlocks;
} }
pairs.Add(score, sb);
log.Info($"【{item.FullName}】识别结果:【{score}】【{sb.ToString().Replace("\r","").Replace("\n",";")}】");
} }
float key = pairs.Keys.Max(); log.Info($"使用识别分数【{maxScore}】");
stringBuilder.Append(pairs[key].ToString()); Dictionary<int, List<TextBlock>> lines = new Dictionary<int, List<TextBlock>>();
log.Info($"使用识别分数【{key}】"); int line = 0, idx = 0, i = 1;
while (idx < list.Count)
{
while (i < list.Count)
{
if (IsInSameLine(list[idx], list[i]))
{
if (lines.ContainsKey(line))
{
lines[line].Add(list[i]);
}
else
{
lines.Add(line, new List<TextBlock> { list[i] });
}
i++;
}
else
{
if (lines.ContainsKey(line))
{
lines[line].Add(list[idx]);
}
else
{
lines[line] = new List<TextBlock> { list[idx] };
}
line++;
idx = i;
i++;
}
}
if (i >= list.Count)
{
if (lines.ContainsKey(line))
{
lines[line].Add(list[idx]);
}
else
{
lines[line] = new List<TextBlock> { list[idx] };
}
break;
}
}
foreach (var lineTxt in lines)
{
lineTxt.Value.Sort(delegate (TextBlock x1, TextBlock x2) { if (x1.BoxPoints[0].X > x2.BoxPoints[0].X) return 1; else return -1; });
foreach (var text in lineTxt.Value)
{
log.Info($"行{lineTxt.Key}【{text.BoxPoints[0]}】【{text.BoxPoints[3]}】【{text.Text}】");
stringBuilder.Append(text.Text);
}
stringBuilder.AppendLine();
}
} }
log.Info($"OCR识别完成"); log.Info($"OCR识别完成");
sw.Stop(); sw.Stop();
...@@ -167,5 +228,41 @@ namespace paddleOCR ...@@ -167,5 +228,41 @@ namespace paddleOCR
log.Info("识别结果:" + result); log.Info("识别结果:" + result);
return true; return true;
} }
/// <summary>
/// 同一行间隔小于多少像素可以合并
/// </summary>
//int intervalPixel = ConfigHelper.Config.Get("IntervalPixelInSameLine", 60);
static bool IsInSameLine(TextBlock t1, TextBlock t2)
{
//同一行间隔小于多少像素可以合并
int intervalPixel = ConfigHelper.Config.Get("IntervalPixelInSameLine", 60);
int p1y1 = t1.BoxPoints[0].Y;
int p1y2 = t1.BoxPoints[3].Y;
int p2y1 = t2.BoxPoints[0].Y;
int p2y2 = t2.BoxPoints[3].Y;
int p1x0 = t1.BoxPoints[0].X;
int p1x1 = t1.BoxPoints[1].X;
int p2x0 = t2.BoxPoints[0].X;
int p2x1 = t2.BoxPoints[1].X;
if (p1y1 > p2y2 || p1y2 < p2y1) { return false; }
else
{
if(p1x0<p2x0)//t1在左侧
{
if (Math.Abs(p1x1 - p2x0) > intervalPixel) return false;
else
return true;
}
else
{
if (Math.Abs(p2x1 - p1x0) > intervalPixel) return false;
else
return true;
}
}
}
} }
} }
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!