Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleHub
提交
aa9f30fa
P
PaddleHub
项目概览
PaddlePaddle
/
PaddleHub
接近 2 年 前同步成功
通知
284
Star
12117
Fork
2091
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
200
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleHub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
200
Issue
200
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
aa9f30fa
编写于
11月 04, 2022
作者:
jm_12138
提交者:
GitHub
11月 04, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update multi_languages_ocr_db_crnn (#2034)
* update multi_languages_ocr_db_crnn * fix a version bug
上级
4889b32a
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
124 addition
and
33 deletion
+124
-33
modules/image/text_recognition/multi_languages_ocr_db_crnn/README.md
...ge/text_recognition/multi_languages_ocr_db_crnn/README.md
+6
-1
modules/image/text_recognition/multi_languages_ocr_db_crnn/module.py
...ge/text_recognition/multi_languages_ocr_db_crnn/module.py
+21
-32
modules/image/text_recognition/multi_languages_ocr_db_crnn/test.py
...mage/text_recognition/multi_languages_ocr_db_crnn/test.py
+97
-0
未找到文件。
modules/image/text_recognition/multi_languages_ocr_db_crnn/README.md
浏览文件 @
aa9f30fa
...
...
@@ -218,6 +218,11 @@
*
1.0.0
初始发布
*
1.1.0
移除 Fluid API
-
```shell
$ hub install multi_languages_ocr_db_crnn==1.
0
.0
$ hub install multi_languages_ocr_db_crnn==1.
1
.0
```
modules/image/text_recognition/multi_languages_ocr_db_crnn/module.py
浏览文件 @
aa9f30fa
import
argparse
import
sys
import
os
import
ast
import
paddle
import
paddle.static
import
paddle2onnx
import
paddle2onnx
as
p2o
import
paddle.fluid
as
fluid
from
paddleocr
import
PaddleOCR
from
paddleocr.ppocr.utils.logging
import
get_logger
from
paddleocr.tools.infer.utility
import
base64_to_cv2
...
...
@@ -17,7 +16,7 @@ from .utils import read_images, save_result_image, mkdir
@
moduleinfo
(
name
=
"multi_languages_ocr_db_crnn"
,
version
=
"1.
0
.0"
,
version
=
"1.
1
.0"
,
summary
=
"ocr service"
,
author
=
"PaddlePaddle"
,
type
=
"cv/text_recognition"
)
...
...
@@ -45,23 +44,21 @@ class MultiLangOCR:
"""
self
.
lang
=
lang
self
.
logger
=
get_logger
()
argc
=
len
(
sys
.
argv
)
if
argc
==
1
or
argc
>
1
and
sys
.
argv
[
1
]
==
'serving'
:
self
.
det
=
det
self
.
rec
=
rec
self
.
use_angle_cls
=
use_angle_cls
self
.
engine
=
PaddleOCR
(
lang
=
lang
,
det
=
det
,
rec
=
rec
,
use_angle_cls
=
use_angle_cls
,
enable_mkldnn
=
enable_mkldnn
,
use_gpu
=
use_gpu
,
det_db_box_thresh
=
box_thresh
,
cls_thresh
=
angle_classification_thresh
)
self
.
det_model_dir
=
self
.
engine
.
text_detector
.
args
.
det_model_dir
self
.
rec_model_dir
=
self
.
engine
.
text_detector
.
args
.
rec_model_dir
self
.
cls_model_dir
=
self
.
engine
.
text_detector
.
args
.
cls_model_dir
self
.
det
=
det
self
.
rec
=
rec
self
.
use_angle_cls
=
use_angle_cls
self
.
engine
=
PaddleOCR
(
lang
=
lang
,
det
=
det
,
rec
=
rec
,
use_angle_cls
=
use_angle_cls
,
enable_mkldnn
=
enable_mkldnn
,
use_gpu
=
use_gpu
,
det_db_box_thresh
=
box_thresh
,
cls_thresh
=
angle_classification_thresh
)
self
.
det_model_dir
=
self
.
engine
.
text_detector
.
args
.
det_model_dir
self
.
rec_model_dir
=
self
.
engine
.
text_detector
.
args
.
rec_model_dir
self
.
cls_model_dir
=
self
.
engine
.
text_detector
.
args
.
cls_model_dir
def
recognize_text
(
self
,
images
=
[],
paths
=
[],
output_dir
=
'ocr_result'
,
visualization
=
False
):
"""
...
...
@@ -189,7 +186,7 @@ class MultiLangOCR:
opset_version(int): operator set
'''
v0
,
v1
,
v2
=
paddle2onnx
.
__version__
.
split
(
'.'
)
if
int
(
v1
)
<
9
:
if
int
(
v
0
)
==
0
and
int
(
v
1
)
<
9
:
raise
ImportError
(
"paddle2onnx>=0.9.0 is required"
)
if
input_shape_dict
is
not
None
and
not
isinstance
(
input_shape_dict
,
dict
):
...
...
@@ -200,19 +197,11 @@ class MultiLangOCR:
path_dict
=
{
"det"
:
self
.
det_model_dir
,
"rec"
:
self
.
rec_model_dir
,
"cls"
:
self
.
cls_model_dir
}
for
(
key
,
path
)
in
path_dict
.
items
():
model_filename
=
'inference.pdmodel'
params_filename
=
'inference.pdiparams'
save_file
=
os
.
path
.
join
(
dirname
,
'{}_{}.onnx'
.
format
(
self
.
name
,
key
))
# convert model save with 'paddle.fluid.io.save_inference_model'
if
hasattr
(
paddle
,
'enable_static'
):
paddle
.
enable_static
()
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
if
model_filename
is
None
and
params_filename
is
None
:
[
program
,
feed_var_names
,
fetch_vars
]
=
fluid
.
io
.
load_inference_model
(
path
,
exe
)
else
:
[
program
,
feed_var_names
,
fetch_vars
]
=
fluid
.
io
.
load_inference_model
(
path
,
exe
,
model_filename
=
model_filename
,
params_filename
=
params_filename
)
exe
=
paddle
.
static
.
Executor
(
paddle
.
CPUPlace
())
[
program
,
feed_var_names
,
fetch_vars
]
=
paddle
.
static
.
load_inference_model
(
os
.
path
.
join
(
path
,
'inference'
),
exe
)
onnx_proto
=
p2o
.
run_convert
(
program
,
input_shape_dict
=
input_shape_dict
,
opset_version
=
opset_version
)
mkdir
(
save_file
)
...
...
modules/image/text_recognition/multi_languages_ocr_db_crnn/test.py
0 → 100644
浏览文件 @
aa9f30fa
import
os
import
shutil
import
unittest
import
cv2
import
requests
import
paddlehub
as
hub
os
.
environ
[
'CUDA_VISIBLE_DEVICES'
]
=
'0'
class
TestHubModule
(
unittest
.
TestCase
):
@
classmethod
def
setUpClass
(
cls
)
->
None
:
img_url
=
'https://unsplash.com/photos/KTzZVDjUsXw/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8MzM3fHx0ZXh0fGVufDB8fHx8MTY2MzUxMTExMQ&force=true&w=640'
if
not
os
.
path
.
exists
(
'tests'
):
os
.
makedirs
(
'tests'
)
response
=
requests
.
get
(
img_url
)
assert
response
.
status_code
==
200
,
'Network Error.'
with
open
(
'tests/test.jpg'
,
'wb'
)
as
f
:
f
.
write
(
response
.
content
)
cls
.
module
=
hub
.
Module
(
name
=
"multi_languages_ocr_db_crnn"
)
@
classmethod
def
tearDownClass
(
cls
)
->
None
:
shutil
.
rmtree
(
'tests'
)
shutil
.
rmtree
(
'onnx'
)
shutil
.
rmtree
(
'ocr_result'
)
def
test_recognize_text1
(
self
):
results
=
self
.
module
.
recognize_text
(
paths
=
[
'tests/test.jpg'
],
visualization
=
False
,
)
self
.
assertEqual
(
results
[
0
][
'data'
],
[
{
'text'
:
'GIVE.'
,
'confidence'
:
0.9509806632995605
,
'text_box_position'
:
[[
283
,
162
],
[
352
,
162
],
[
352
,
202
],
[
283
,
202
]]
},
{
'text'
:
'THANKS'
,
'confidence'
:
0.9943129420280457
,
'text_box_position'
:
[[
261
,
202
],
[
376
,
202
],
[
376
,
239
],
[
261
,
239
]]
}])
def
test_recognize_text2
(
self
):
results
=
self
.
module
.
recognize_text
(
images
=
[
cv2
.
imread
(
'tests/test.jpg'
)],
visualization
=
False
,
)
self
.
assertEqual
(
results
[
0
][
'data'
],
[
{
'text'
:
'GIVE.'
,
'confidence'
:
0.9509806632995605
,
'text_box_position'
:
[[
283
,
162
],
[
352
,
162
],
[
352
,
202
],
[
283
,
202
]]
},
{
'text'
:
'THANKS'
,
'confidence'
:
0.9943129420280457
,
'text_box_position'
:
[[
261
,
202
],
[
376
,
202
],
[
376
,
239
],
[
261
,
239
]]
}])
def
test_recognize_text3
(
self
):
results
=
self
.
module
.
recognize_text
(
images
=
[
cv2
.
imread
(
'tests/test.jpg'
)],
visualization
=
True
,
)
self
.
assertEqual
(
results
[
0
][
'data'
],
[
{
'text'
:
'GIVE.'
,
'confidence'
:
0.9509806632995605
,
'text_box_position'
:
[[
283
,
162
],
[
352
,
162
],
[
352
,
202
],
[
283
,
202
]]
},
{
'text'
:
'THANKS'
,
'confidence'
:
0.9943129420280457
,
'text_box_position'
:
[[
261
,
202
],
[
376
,
202
],
[
376
,
239
],
[
261
,
239
]]
}])
def
test_recognize_text4
(
self
):
self
.
assertRaises
(
AttributeError
,
self
.
module
.
recognize_text
,
images
=
[
'tests/test.jpg'
]
)
def
test_recognize_text5
(
self
):
self
.
assertRaises
(
AssertionError
,
self
.
module
.
recognize_text
,
paths
=
[
'no.jpg'
]
)
def
test_export_onnx_model
(
self
):
self
.
module
.
export_onnx_model
(
dirname
=
'onnx'
,
input_shape_dict
=
None
,
opset_version
=
10
)
self
.
assertTrue
(
os
.
path
.
isfile
(
'onnx/multi_languages_ocr_db_crnn_cls.onnx'
))
self
.
assertTrue
(
os
.
path
.
isfile
(
'onnx/multi_languages_ocr_db_crnn_det.onnx'
))
self
.
assertTrue
(
os
.
path
.
isfile
(
'onnx/multi_languages_ocr_db_crnn_rec.onnx'
))
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录