Commit aec75728 张东亮

paddle识别异常处理

1 个父辈 4ec07436
......@@ -143,25 +143,33 @@ namespace paddleOCR
List<TextBlock> list = new List<TextBlock>();
foreach (var item in dir.GetFiles())
{
float score = 0f;
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)
try
{
maxScore = score;
list = res.TextBlocks;
float score = 0f;
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}】");
Dictionary<int, List<TextBlock>> lines = new Dictionary<int, List<TextBlock>>();
int line = 0, idx = 0, i = 1;
......@@ -212,7 +220,7 @@ namespace paddleOCR
}
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)
{
......@@ -384,47 +392,47 @@ namespace paddleOCR
}
#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>();
double lastYAvg = 0;
foreach (var entity in list)
{
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));
}
else
{
if (mergedTextlist.Count!=0)
if (mergedTextlist.Count != 0)
{
var stringdata = mergedTextlist.OrderBy(a => a.IntValue);
string str = "";
foreach (var item in stringdata)
{
str+= item.StringValue+" ";
foreach (var item in stringdata)
{
str += item.StringValue + " ";
}
stringLists.Add(str);
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)
{
var stringdata = mergedTextlist.OrderBy(a => a.IntValue);
string str = "";
foreach (var item in stringdata)
{
str += item.StringValue+" ";
{
str += item.StringValue + " ";
}
stringLists.Add(str);
mergedTextlist.Clear();
}
}
log.Info($"OCRHandleRewrite:矩形中心点相差阈值为:{threshold}");
#endregion
log.Info($"OCRHandleRewrite:使用识别分数【{maxScore}】");
log.Info($"OCRHandleRewrite:使用识别分数【{maxScore}】");
}
log.Info($"OCRHandleRewrite:OCR识别完成");
sw.Stop();
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!