Skip to content
切换导航条
切换导航条
当前项目
正在载入...
登录
张东亮
/
NS100
转到一个项目
切换导航栏
切换导航栏固定状态
项目
群组
代码片段
帮助
项目
活动
版本库
流水线
图表
问题
0
合并请求
0
维基
网络
创建新的问题
作业
提交
问题看板
文件
提交
网络
比较
分支
标签
Commit aec75728
由
张东亮
编写于
2023-11-22 10:17:31 +0800
浏览文件
选项
浏览文件
标签
下载
电子邮件补丁
差异文件
paddle识别异常处理
1 个父辈
4ec07436
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
38 行增加
和
30 行删除
paddleOCR/PaddleSharpAPI.cs
paddleOCR/PaddleSharpAPI.cs
查看文件 @
aec7572
...
...
@@ -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!
Cancel
请
注册
或
登录
后发表评论