Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleOCR
提交
ae09ef60
P
PaddleOCR
项目概览
PaddlePaddle
/
PaddleOCR
大约 1 年 前同步成功
通知
1528
Star
32962
Fork
6643
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
108
列表
看板
标记
里程碑
合并请求
7
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleOCR
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
108
Issue
108
列表
看板
标记
里程碑
合并请求
7
合并请求
7
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
ae09ef60
编写于
9月 07, 2021
作者:
A
andyjpaddle
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix code style
上级
8123688a
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
34 addition
and
28 deletion
+34
-28
ppocr/postprocess/rec_postprocess.py
ppocr/postprocess/rec_postprocess.py
+34
-28
未找到文件。
ppocr/postprocess/rec_postprocess.py
浏览文件 @
ae09ef60
...
@@ -172,15 +172,15 @@ class NRTRLabelDecode(BaseRecLabelDecode):
...
@@ -172,15 +172,15 @@ class NRTRLabelDecode(BaseRecLabelDecode):
if
preds
.
dtype
==
paddle
.
int64
:
if
preds
.
dtype
==
paddle
.
int64
:
if
isinstance
(
preds
,
paddle
.
Tensor
):
if
isinstance
(
preds
,
paddle
.
Tensor
):
preds
=
preds
.
numpy
()
preds
=
preds
.
numpy
()
if
preds
[
0
][
0
]
==
2
:
if
preds
[
0
][
0
]
==
2
:
preds_idx
=
preds
[:,
1
:]
preds_idx
=
preds
[:,
1
:]
else
:
else
:
preds_idx
=
preds
preds_idx
=
preds
text
=
self
.
decode
(
preds_idx
)
text
=
self
.
decode
(
preds_idx
)
if
label
is
None
:
if
label
is
None
:
return
text
return
text
label
=
self
.
decode
(
label
[:,
1
:])
label
=
self
.
decode
(
label
[:,
1
:])
else
:
else
:
if
isinstance
(
preds
,
paddle
.
Tensor
):
if
isinstance
(
preds
,
paddle
.
Tensor
):
preds
=
preds
.
numpy
()
preds
=
preds
.
numpy
()
...
@@ -189,11 +189,11 @@ class NRTRLabelDecode(BaseRecLabelDecode):
...
@@ -189,11 +189,11 @@ class NRTRLabelDecode(BaseRecLabelDecode):
text
=
self
.
decode
(
preds_idx
,
preds_prob
,
is_remove_duplicate
=
False
)
text
=
self
.
decode
(
preds_idx
,
preds_prob
,
is_remove_duplicate
=
False
)
if
label
is
None
:
if
label
is
None
:
return
text
return
text
label
=
self
.
decode
(
label
[:,
1
:])
label
=
self
.
decode
(
label
[:,
1
:])
return
text
,
label
return
text
,
label
def
add_special_char
(
self
,
dict_character
):
def
add_special_char
(
self
,
dict_character
):
dict_character
=
[
'blank'
,
'<unk>'
,
'<s>'
,
'</s>'
]
+
dict_character
dict_character
=
[
'blank'
,
'<unk>'
,
'<s>'
,
'</s>'
]
+
dict_character
return
dict_character
return
dict_character
def
decode
(
self
,
text_index
,
text_prob
=
None
,
is_remove_duplicate
=
False
):
def
decode
(
self
,
text_index
,
text_prob
=
None
,
is_remove_duplicate
=
False
):
...
@@ -207,7 +207,8 @@ class NRTRLabelDecode(BaseRecLabelDecode):
...
@@ -207,7 +207,8 @@ class NRTRLabelDecode(BaseRecLabelDecode):
if
text_index
[
batch_idx
][
idx
]
==
3
:
# end
if
text_index
[
batch_idx
][
idx
]
==
3
:
# end
break
break
try
:
try
:
char_list
.
append
(
self
.
character
[
int
(
text_index
[
batch_idx
][
idx
])])
char_list
.
append
(
self
.
character
[
int
(
text_index
[
batch_idx
][
idx
])])
except
:
except
:
continue
continue
if
text_prob
is
not
None
:
if
text_prob
is
not
None
:
...
@@ -219,7 +220,6 @@ class NRTRLabelDecode(BaseRecLabelDecode):
...
@@ -219,7 +220,6 @@ class NRTRLabelDecode(BaseRecLabelDecode):
return
result_list
return
result_list
class
AttnLabelDecode
(
BaseRecLabelDecode
):
class
AttnLabelDecode
(
BaseRecLabelDecode
):
""" Convert between text-label and text-index """
""" Convert between text-label and text-index """
...
@@ -257,7 +257,8 @@ class AttnLabelDecode(BaseRecLabelDecode):
...
@@ -257,7 +257,8 @@ class AttnLabelDecode(BaseRecLabelDecode):
if
idx
>
0
and
text_index
[
batch_idx
][
idx
-
1
]
==
text_index
[
if
idx
>
0
and
text_index
[
batch_idx
][
idx
-
1
]
==
text_index
[
batch_idx
][
idx
]:
batch_idx
][
idx
]:
continue
continue
char_list
.
append
(
self
.
character
[
int
(
text_index
[
batch_idx
][
idx
])])
char_list
.
append
(
self
.
character
[
int
(
text_index
[
batch_idx
][
idx
])])
if
text_prob
is
not
None
:
if
text_prob
is
not
None
:
conf_list
.
append
(
text_prob
[
batch_idx
][
idx
])
conf_list
.
append
(
text_prob
[
batch_idx
][
idx
])
else
:
else
:
...
@@ -387,10 +388,9 @@ class SRNLabelDecode(BaseRecLabelDecode):
...
@@ -387,10 +388,9 @@ class SRNLabelDecode(BaseRecLabelDecode):
class
TableLabelDecode
(
object
):
class
TableLabelDecode
(
object
):
""" """
""" """
def
__init__
(
self
,
def
__init__
(
self
,
character_dict_path
,
**
kwargs
):
character_dict_path
,
list_character
,
list_elem
=
self
.
load_char_elem_dict
(
**
kwargs
):
character_dict_path
)
list_character
,
list_elem
=
self
.
load_char_elem_dict
(
character_dict_path
)
list_character
=
self
.
add_special_char
(
list_character
)
list_character
=
self
.
add_special_char
(
list_character
)
list_elem
=
self
.
add_special_char
(
list_elem
)
list_elem
=
self
.
add_special_char
(
list_elem
)
self
.
dict_character
=
{}
self
.
dict_character
=
{}
...
@@ -409,7 +409,8 @@ class TableLabelDecode(object):
...
@@ -409,7 +409,8 @@ class TableLabelDecode(object):
list_elem
=
[]
list_elem
=
[]
with
open
(
character_dict_path
,
"rb"
)
as
fin
:
with
open
(
character_dict_path
,
"rb"
)
as
fin
:
lines
=
fin
.
readlines
()
lines
=
fin
.
readlines
()
substr
=
lines
[
0
].
decode
(
'utf-8'
).
strip
(
"
\n
"
).
strip
(
"
\r\n
"
).
split
(
"
\t
"
)
substr
=
lines
[
0
].
decode
(
'utf-8'
).
strip
(
"
\n
"
).
strip
(
"
\r\n
"
).
split
(
"
\t
"
)
character_num
=
int
(
substr
[
0
])
character_num
=
int
(
substr
[
0
])
elem_num
=
int
(
substr
[
1
])
elem_num
=
int
(
substr
[
1
])
for
cno
in
range
(
1
,
1
+
character_num
):
for
cno
in
range
(
1
,
1
+
character_num
):
...
@@ -429,14 +430,14 @@ class TableLabelDecode(object):
...
@@ -429,14 +430,14 @@ class TableLabelDecode(object):
def
__call__
(
self
,
preds
):
def
__call__
(
self
,
preds
):
structure_probs
=
preds
[
'structure_probs'
]
structure_probs
=
preds
[
'structure_probs'
]
loc_preds
=
preds
[
'loc_preds'
]
loc_preds
=
preds
[
'loc_preds'
]
if
isinstance
(
structure_probs
,
paddle
.
Tensor
):
if
isinstance
(
structure_probs
,
paddle
.
Tensor
):
structure_probs
=
structure_probs
.
numpy
()
structure_probs
=
structure_probs
.
numpy
()
if
isinstance
(
loc_preds
,
paddle
.
Tensor
):
if
isinstance
(
loc_preds
,
paddle
.
Tensor
):
loc_preds
=
loc_preds
.
numpy
()
loc_preds
=
loc_preds
.
numpy
()
structure_idx
=
structure_probs
.
argmax
(
axis
=
2
)
structure_idx
=
structure_probs
.
argmax
(
axis
=
2
)
structure_probs
=
structure_probs
.
max
(
axis
=
2
)
structure_probs
=
structure_probs
.
max
(
axis
=
2
)
structure_str
,
structure_pos
,
result_score_list
,
result_elem_idx_list
=
self
.
decode
(
structure_idx
,
structure_str
,
structure_pos
,
result_score_list
,
result_elem_idx_list
=
self
.
decode
(
structure_probs
,
'elem'
)
structure_idx
,
structure_probs
,
'elem'
)
res_html_code_list
=
[]
res_html_code_list
=
[]
res_loc_list
=
[]
res_loc_list
=
[]
batch_num
=
len
(
structure_str
)
batch_num
=
len
(
structure_str
)
...
@@ -451,8 +452,13 @@ class TableLabelDecode(object):
...
@@ -451,8 +452,13 @@ class TableLabelDecode(object):
res_loc
=
np
.
array
(
res_loc
)
res_loc
=
np
.
array
(
res_loc
)
res_html_code_list
.
append
(
res_html_code
)
res_html_code_list
.
append
(
res_html_code
)
res_loc_list
.
append
(
res_loc
)
res_loc_list
.
append
(
res_loc
)
return
{
'res_html_code'
:
res_html_code_list
,
'res_loc'
:
res_loc_list
,
'res_score_list'
:
result_score_list
,
return
{
'res_elem_idx_list'
:
result_elem_idx_list
,
'structure_str_list'
:
structure_str
}
'res_html_code'
:
res_html_code_list
,
'res_loc'
:
res_loc_list
,
'res_score_list'
:
result_score_list
,
'res_elem_idx_list'
:
result_elem_idx_list
,
'structure_str_list'
:
structure_str
}
def
decode
(
self
,
text_index
,
structure_probs
,
char_or_elem
):
def
decode
(
self
,
text_index
,
structure_probs
,
char_or_elem
):
"""convert text-label into text-index.
"""convert text-label into text-index.
...
@@ -558,7 +564,7 @@ class SARLabelDecode(BaseRecLabelDecode):
...
@@ -558,7 +564,7 @@ class SARLabelDecode(BaseRecLabelDecode):
if
text_index
[
batch_idx
][
idx
]
in
ignored_tokens
:
if
text_index
[
batch_idx
][
idx
]
in
ignored_tokens
:
continue
continue
if
int
(
text_index
[
batch_idx
][
idx
])
==
int
(
self
.
end_idx
):
if
int
(
text_index
[
batch_idx
][
idx
])
==
int
(
self
.
end_idx
):
if
text_prob
is
None
and
idx
==
0
:
if
text_prob
is
None
and
idx
==
0
:
continue
continue
else
:
else
:
break
break
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录