Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Serving
提交
d8978e9c
S
Serving
项目概览
PaddlePaddle
/
Serving
1 年多 前同步成功
通知
186
Star
833
Fork
253
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
105
列表
看板
标记
里程碑
合并请求
10
Wiki
2
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Serving
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
105
Issue
105
列表
看板
标记
里程碑
合并请求
10
合并请求
10
Pages
分析
分析
仓库分析
DevOps
Wiki
2
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
d8978e9c
编写于
7月 16, 2020
作者:
D
dyning
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support det/rec client only
上级
9f04ddc4
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
255 addition
and
0 deletion
+255
-0
python/examples/ocr/det_debugger_server.py
python/examples/ocr/det_debugger_server.py
+71
-0
python/examples/ocr/det_web_server.py
python/examples/ocr/det_web_server.py
+72
-0
python/examples/ocr/rec_web_client.py
python/examples/ocr/rec_web_client.py
+41
-0
python/examples/ocr/rec_web_server.py
python/examples/ocr/rec_web_server.py
+71
-0
未找到文件。
python/examples/ocr/det_debugger_server.py
0 → 100644
浏览文件 @
d8978e9c
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
paddle_serving_client
import
Client
import
cv2
import
sys
import
numpy
as
np
import
os
from
paddle_serving_client
import
Client
from
paddle_serving_app.reader
import
Sequential
,
ResizeByFactor
from
paddle_serving_app.reader
import
Div
,
Normalize
,
Transpose
from
paddle_serving_app.reader
import
DBPostProcess
,
FilterBoxes
from
paddle_serving_server_gpu.web_service
import
WebService
import
time
import
re
import
base64
class
OCRService
(
WebService
):
def
init_det
(
self
):
self
.
det_preprocess
=
Sequential
([
ResizeByFactor
(
32
,
960
),
Div
(
255
),
Normalize
([
0.485
,
0.456
,
0.406
],
[
0.229
,
0.224
,
0.225
]),
Transpose
(
(
2
,
0
,
1
))
])
self
.
filter_func
=
FilterBoxes
(
10
,
10
)
self
.
post_func
=
DBPostProcess
({
"thresh"
:
0.3
,
"box_thresh"
:
0.5
,
"max_candidates"
:
1000
,
"unclip_ratio"
:
1.5
,
"min_size"
:
3
})
def
preprocess
(
self
,
feed
=
[],
fetch
=
[]):
data
=
base64
.
b64decode
(
feed
[
0
][
"image"
].
encode
(
'utf8'
))
data
=
np
.
fromstring
(
data
,
np
.
uint8
)
im
=
cv2
.
imdecode
(
data
,
cv2
.
IMREAD_COLOR
)
self
.
ori_h
,
self
.
ori_w
,
_
=
im
.
shape
det_img
=
self
.
det_preprocess
(
im
)
_
,
self
.
new_h
,
self
.
new_w
=
det_img
.
shape
return
{
"image"
:
det_img
[
np
.
newaxis
,
:].
copy
()},
[
"concat_1.tmp_0"
]
def
postprocess
(
self
,
feed
=
{},
fetch
=
[],
fetch_map
=
None
):
det_out
=
fetch_map
[
"concat_1.tmp_0"
]
ratio_list
=
[
float
(
self
.
new_h
)
/
self
.
ori_h
,
float
(
self
.
new_w
)
/
self
.
ori_w
]
dt_boxes_list
=
self
.
post_func
(
det_out
,
[
ratio_list
])
dt_boxes
=
self
.
filter_func
(
dt_boxes_list
[
0
],
[
self
.
ori_h
,
self
.
ori_w
])
return
{
"dt_boxes"
:
dt_boxes
.
tolist
()}
ocr_service
=
OCRService
(
name
=
"ocr"
)
ocr_service
.
load_model_config
(
"ocr_det_model"
)
ocr_service
.
set_gpus
(
"0"
)
ocr_service
.
prepare_server
(
workdir
=
"workdir"
,
port
=
8900
,
device
=
"gpu"
,
gpuid
=
0
)
ocr_service
.
init_det
()
ocr_service
.
run_debugger_service
()
ocr_service
.
run_web_service
()
python/examples/ocr/det_web_server.py
0 → 100644
浏览文件 @
d8978e9c
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
paddle_serving_client
import
Client
import
cv2
import
sys
import
numpy
as
np
import
os
from
paddle_serving_client
import
Client
from
paddle_serving_app.reader
import
Sequential
,
ResizeByFactor
from
paddle_serving_app.reader
import
Div
,
Normalize
,
Transpose
from
paddle_serving_app.reader
import
DBPostProcess
,
FilterBoxes
from
paddle_serving_server_gpu.web_service
import
WebService
import
time
import
re
import
base64
class
OCRService
(
WebService
):
def
init_det
(
self
):
self
.
det_preprocess
=
Sequential
([
ResizeByFactor
(
32
,
960
),
Div
(
255
),
Normalize
([
0.485
,
0.456
,
0.406
],
[
0.229
,
0.224
,
0.225
]),
Transpose
(
(
2
,
0
,
1
))
])
self
.
filter_func
=
FilterBoxes
(
10
,
10
)
self
.
post_func
=
DBPostProcess
({
"thresh"
:
0.3
,
"box_thresh"
:
0.5
,
"max_candidates"
:
1000
,
"unclip_ratio"
:
1.5
,
"min_size"
:
3
})
def
preprocess
(
self
,
feed
=
[],
fetch
=
[]):
data
=
base64
.
b64decode
(
feed
[
0
][
"image"
].
encode
(
'utf8'
))
data
=
np
.
fromstring
(
data
,
np
.
uint8
)
im
=
cv2
.
imdecode
(
data
,
cv2
.
IMREAD_COLOR
)
self
.
ori_h
,
self
.
ori_w
,
_
=
im
.
shape
det_img
=
self
.
det_preprocess
(
im
)
_
,
self
.
new_h
,
self
.
new_w
=
det_img
.
shape
print
(
det_img
)
return
{
"image"
:
det_img
},
[
"concat_1.tmp_0"
]
def
postprocess
(
self
,
feed
=
{},
fetch
=
[],
fetch_map
=
None
):
det_out
=
fetch_map
[
"concat_1.tmp_0"
]
ratio_list
=
[
float
(
self
.
new_h
)
/
self
.
ori_h
,
float
(
self
.
new_w
)
/
self
.
ori_w
]
dt_boxes_list
=
self
.
post_func
(
det_out
,
[
ratio_list
])
dt_boxes
=
self
.
filter_func
(
dt_boxes_list
[
0
],
[
self
.
ori_h
,
self
.
ori_w
])
return
{
"dt_boxes"
:
dt_boxes
.
tolist
()}
ocr_service
=
OCRService
(
name
=
"ocr"
)
ocr_service
.
load_model_config
(
"ocr_det_model"
)
ocr_service
.
set_gpus
(
"0"
)
ocr_service
.
prepare_server
(
workdir
=
"workdir"
,
port
=
8900
,
device
=
"gpu"
,
gpuid
=
0
)
ocr_service
.
init_det
()
ocr_service
.
run_rpc_service
()
ocr_service
.
run_web_service
()
python/examples/ocr/rec_web_client.py
0 → 100644
浏览文件 @
d8978e9c
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -*- coding: utf-8 -*-
import
requests
import
json
import
cv2
import
base64
import
os
,
sys
import
time
def
cv2_to_base64
(
image
):
#data = cv2.imencode('.jpg', image)[1]
return
base64
.
b64encode
(
image
).
decode
(
'utf8'
)
#data.tostring()).decode('utf8')
headers
=
{
"Content-type"
:
"application/json"
}
url
=
"http://127.0.0.1:8900/ocr/prediction"
test_img_dir
=
"rec_img/"
for
img_file
in
os
.
listdir
(
test_img_dir
):
with
open
(
os
.
path
.
join
(
test_img_dir
,
img_file
),
'rb'
)
as
file
:
image_data1
=
file
.
read
()
image
=
cv2_to_base64
(
image_data1
)
#data = {"feed": [{"image": image}], "fetch": ["res"]}
data
=
{
"feed"
:
[{
"image"
:
image
}]
*
3
,
"fetch"
:
[
"res"
]}
r
=
requests
.
post
(
url
=
url
,
headers
=
headers
,
data
=
json
.
dumps
(
data
))
print
(
r
.
json
())
python/examples/ocr/rec_web_server.py
0 → 100644
浏览文件 @
d8978e9c
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
paddle_serving_client
import
Client
from
paddle_serving_app.reader
import
OCRReader
import
cv2
import
sys
import
numpy
as
np
import
os
from
paddle_serving_client
import
Client
from
paddle_serving_app.reader
import
Sequential
,
URL2Image
,
ResizeByFactor
from
paddle_serving_app.reader
import
Div
,
Normalize
,
Transpose
from
paddle_serving_app.reader
import
DBPostProcess
,
FilterBoxes
,
GetRotateCropImage
,
SortedBoxes
from
paddle_serving_server_gpu.web_service
import
WebService
import
time
import
re
import
base64
class
OCRService
(
WebService
):
def
init_rec
(
self
):
self
.
ocr_reader
=
OCRReader
()
def
preprocess
(
self
,
feed
=
[],
fetch
=
[]):
# TODO: to handle batch rec images
img_list
=
[]
for
feed_data
in
feed
:
data
=
base64
.
b64decode
(
feed_data
[
"image"
].
encode
(
'utf8'
))
data
=
np
.
fromstring
(
data
,
np
.
uint8
)
im
=
cv2
.
imdecode
(
data
,
cv2
.
IMREAD_COLOR
)
img_list
.
append
(
im
)
feed_list
=
[]
max_wh_ratio
=
0
for
i
,
boximg
in
enumerate
(
img_list
):
h
,
w
=
boximg
.
shape
[
0
:
2
]
wh_ratio
=
w
*
1.0
/
h
max_wh_ratio
=
max
(
max_wh_ratio
,
wh_ratio
)
for
img
in
img_list
:
norm_img
=
self
.
ocr_reader
.
resize_norm_img
(
img
,
max_wh_ratio
)
feed
=
{
"image"
:
norm_img
}
feed_list
.
append
(
feed
)
fetch
=
[
"ctc_greedy_decoder_0.tmp_0"
,
"softmax_0.tmp_0"
]
return
feed_list
,
fetch
def
postprocess
(
self
,
feed
=
{},
fetch
=
[],
fetch_map
=
None
):
rec_res
=
self
.
ocr_reader
.
postprocess
(
fetch_map
,
with_score
=
True
)
res_lst
=
[]
for
res
in
rec_res
:
res_lst
.
append
(
res
[
0
])
res
=
{
"res"
:
res_lst
}
return
res
ocr_service
=
OCRService
(
name
=
"ocr"
)
ocr_service
.
load_model_config
(
"ocr_rec_model"
)
ocr_service
.
set_gpus
(
"0"
)
ocr_service
.
init_rec
()
ocr_service
.
prepare_server
(
workdir
=
"workdir"
,
port
=
8900
,
device
=
"gpu"
,
gpuid
=
0
)
ocr_service
.
run_rpc_service
()
ocr_service
.
run_web_service
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录