Commit aec75728 张东亮

paddle识别异常处理

1 个父辈 4ec07436
...@@ -143,25 +143,33 @@ namespace paddleOCR ...@@ -143,25 +143,33 @@ namespace paddleOCR
List<TextBlock> list = new List<TextBlock>(); List<TextBlock> list = new List<TextBlock>();
foreach (var item in dir.GetFiles()) foreach (var item in dir.GetFiles())
{ {
float score = 0f; try
var imagebyte = File.ReadAllBytes(item.FullName);
var res = engine.DetectText(imagebyte);
StringBuilder sb = new StringBuilder();
res.TextBlocks.ForEach(s => { score += s.Score; sb.AppendLine(s.Text); });
//foreach (var item in ocrResult.TextBlocks)
//{
// using (Graphics g = Graphics.FromImage(bitmap))
// {
// g.DrawPolygon(new Pen(Brushes.Red, 2), item.BoxPoints.Select(x => new PointF() { X = x.X, Y = x.Y }).ToArray());
// }
//}
log.Info($"【{item.FullName}】结果:【{score}】【{sb.ToString().Replace("\r", "").Replace("\n", ";")}】");
if (score > maxScore)
{ {
maxScore = score; float score = 0f;
list = res.TextBlocks; var imagebyte = File.ReadAllBytes(item.FullName);
var res = engine.DetectText(imagebyte);
StringBuilder sb = new StringBuilder();
res.TextBlocks.ForEach(s => { score += s.Score; sb.AppendLine(s.Text); });
//foreach (var item in ocrResult.TextBlocks)
//{
// using (Graphics g = Graphics.FromImage(bitmap))
// {
// g.DrawPolygon(new Pen(Brushes.Red, 2), item.BoxPoints.Select(x => new PointF() { X = x.X, Y = x.Y }).ToArray());
// }
//}
log.Info($"【{item.FullName}】结果:【{score}】【{sb.ToString().Replace("\r", "").Replace("\n", ";")}】");
if (score > maxScore)
{
maxScore = score;
list = res.TextBlocks;
}
}
catch (Exception ex)
{
log.Error($"{item.FullName}异常", ex);
} }
}
}
log.Info($"使用识别分数【{maxScore}】"); log.Info($"使用识别分数【{maxScore}】");
Dictionary<int, List<TextBlock>> lines = new Dictionary<int, List<TextBlock>>(); Dictionary<int, List<TextBlock>> lines = new Dictionary<int, List<TextBlock>>();
int line = 0, idx = 0, i = 1; int line = 0, idx = 0, i = 1;
...@@ -212,7 +220,7 @@ namespace paddleOCR ...@@ -212,7 +220,7 @@ namespace paddleOCR
} }
foreach (var lineTxt in lines) 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; }); 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) foreach (var text in lineTxt.Value)
{ {
...@@ -384,47 +392,47 @@ namespace paddleOCR ...@@ -384,47 +392,47 @@ namespace paddleOCR
} }
#region ocr文字统一一行 #region ocr文字统一一行
//矩形中心点的相差阈值 //矩形中心点的相差阈值
int threshold = int.Parse(ConfigHelper.Config.Get("paddleOcr_RowThreshold", "10")); int threshold = int.Parse(ConfigHelper.Config.Get("paddleOcr_RowThreshold", "10"));
List<StringIntPair> mergedTextlist = new List<StringIntPair>(); List<StringIntPair> mergedTextlist = new List<StringIntPair>();
double lastYAvg = 0; double lastYAvg = 0;
foreach (var entity in list) foreach (var entity in list)
{ {
double avgY = entity.BoxPoints.Average(a => a.Y); double avgY = entity.BoxPoints.Average(a => a.Y);
if (Math.Abs(lastYAvg - avgY)<threshold) if (Math.Abs(lastYAvg - avgY) < threshold)
{ {
mergedTextlist.Add(new StringIntPair(entity.Text, entity.BoxPoints[0].X)); mergedTextlist.Add(new StringIntPair(entity.Text, entity.BoxPoints[0].X));
} }
else else
{ {
if (mergedTextlist.Count!=0) if (mergedTextlist.Count != 0)
{ {
var stringdata = mergedTextlist.OrderBy(a => a.IntValue); var stringdata = mergedTextlist.OrderBy(a => a.IntValue);
string str = ""; string str = "";
foreach (var item in stringdata) foreach (var item in stringdata)
{ {
str+= item.StringValue+" "; str += item.StringValue + " ";
} }
stringLists.Add(str); stringLists.Add(str);
mergedTextlist.Clear(); mergedTextlist.Clear();
} }
mergedTextlist.Add(new StringIntPair(entity.Text, entity.BoxPoints[0].X)); mergedTextlist.Add(new StringIntPair(entity.Text, entity.BoxPoints[0].X));
} }
lastYAvg = avgY; lastYAvg = avgY;
} }
if (mergedTextlist.Count != 0) if (mergedTextlist.Count != 0)
{ {
var stringdata = mergedTextlist.OrderBy(a => a.IntValue); var stringdata = mergedTextlist.OrderBy(a => a.IntValue);
string str = ""; string str = "";
foreach (var item in stringdata) foreach (var item in stringdata)
{ {
str += item.StringValue+" "; str += item.StringValue + " ";
} }
stringLists.Add(str); stringLists.Add(str);
mergedTextlist.Clear(); mergedTextlist.Clear();
} }
log.Info($"OCRHandleRewrite:矩形中心点相差阈值为:{threshold}"); log.Info($"OCRHandleRewrite:矩形中心点相差阈值为:{threshold}");
#endregion #endregion
log.Info($"OCRHandleRewrite:使用识别分数【{maxScore}】"); log.Info($"OCRHandleRewrite:使用识别分数【{maxScore}】");
} }
log.Info($"OCRHandleRewrite:OCR识别完成"); log.Info($"OCRHandleRewrite:OCR识别完成");
sw.Stop(); sw.Stop();
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!