commit
ec68899cc7
|
@ -107,16 +107,18 @@ class OCRService(WebService):
|
||||||
if ".lod" in x:
|
if ".lod" in x:
|
||||||
self.tmp_args[x] = fetch_map[x]
|
self.tmp_args[x] = fetch_map[x]
|
||||||
_, rec_res = self.text_classifier.postprocess(outputs, self.tmp_args)
|
_, rec_res = self.text_classifier.postprocess(outputs, self.tmp_args)
|
||||||
res = {
|
res = []
|
||||||
"pred_text": [x[0] for x in rec_res],
|
for i in range(len(rec_res)):
|
||||||
"score": [str(x[1]) for x in rec_res]
|
res.append({
|
||||||
}
|
"direction": rec_res[i][0],
|
||||||
|
"confidence": float(rec_res[i][1])
|
||||||
|
})
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
ocr_service = OCRService(name="ocr")
|
ocr_service = OCRService(name="ocr")
|
||||||
ocr_service.load_model_config(global_args.cls_model_dir)
|
ocr_service.load_model_config(global_args.cls_server_dir)
|
||||||
ocr_service.init_rec()
|
ocr_service.init_rec()
|
||||||
if global_args.use_gpu:
|
if global_args.use_gpu:
|
||||||
ocr_service.prepare_server(
|
ocr_service.prepare_server(
|
||||||
|
|
|
@ -113,16 +113,18 @@ class OCRService(WebService):
|
||||||
if ".lod" in x:
|
if ".lod" in x:
|
||||||
self.tmp_args[x] = fetch_map[x]
|
self.tmp_args[x] = fetch_map[x]
|
||||||
_, rec_res = self.text_classifier.postprocess(outputs, self.tmp_args)
|
_, rec_res = self.text_classifier.postprocess(outputs, self.tmp_args)
|
||||||
res = {
|
res = []
|
||||||
"direction": [x[0] for x in rec_res],
|
for i in range(len(rec_res)):
|
||||||
"score": [str(x[1]) for x in rec_res]
|
res.append({
|
||||||
}
|
"direction": rec_res[i][0],
|
||||||
|
"confidence": float(rec_res[i][1])
|
||||||
|
})
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
ocr_service = OCRService(name="ocr")
|
ocr_service = OCRService(name="ocr")
|
||||||
ocr_service.load_model_config(global_args.cls_model_dir)
|
ocr_service.load_model_config(global_args.cls_server_dir)
|
||||||
ocr_service.init_rec()
|
ocr_service.init_rec()
|
||||||
if global_args.use_gpu:
|
if global_args.use_gpu:
|
||||||
ocr_service.prepare_server(
|
ocr_service.prepare_server(
|
||||||
|
|
|
@ -90,13 +90,15 @@ class DetService(WebService):
|
||||||
|
|
||||||
def postprocess(self, feed={}, fetch=[], fetch_map=None):
|
def postprocess(self, feed={}, fetch=[], fetch_map=None):
|
||||||
outputs = [fetch_map[x] for x in fetch]
|
outputs = [fetch_map[x] for x in fetch]
|
||||||
res = self.text_detector.postprocess(outputs, self.tmp_args)
|
det_res = self.text_detector.postprocess(outputs, self.tmp_args)
|
||||||
return {"boxes": res.tolist()}
|
res = []
|
||||||
|
for i in range(len(det_res)):
|
||||||
|
res.append({"text_region": det_res[i].tolist()})
|
||||||
|
return res
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
ocr_service = DetService(name="ocr")
|
ocr_service = DetService(name="ocr")
|
||||||
ocr_service.load_model_config(global_args.det_model_dir)
|
ocr_service.load_model_config(global_args.det_server_dir)
|
||||||
ocr_service.init_det()
|
ocr_service.init_det()
|
||||||
if global_args.use_gpu:
|
if global_args.use_gpu:
|
||||||
ocr_service.prepare_server(
|
ocr_service.prepare_server(
|
||||||
|
|
|
@ -89,13 +89,15 @@ class DetService(WebService):
|
||||||
|
|
||||||
def postprocess(self, feed={}, fetch=[], fetch_map=None):
|
def postprocess(self, feed={}, fetch=[], fetch_map=None):
|
||||||
outputs = [fetch_map[x] for x in fetch]
|
outputs = [fetch_map[x] for x in fetch]
|
||||||
res = self.text_detector.postprocess(outputs, self.tmp_args)
|
det_res = self.text_detector.postprocess(outputs, self.tmp_args)
|
||||||
return {"boxes": res.tolist()}
|
res = []
|
||||||
|
for i in range(len(det_res)):
|
||||||
|
res.append({"text_region": det_res[i].tolist()})
|
||||||
|
return res
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
ocr_service = DetService(name="ocr")
|
ocr_service = DetService(name="ocr")
|
||||||
ocr_service.load_model_config(global_args.det_model_dir)
|
ocr_service.load_model_config(global_args.det_server_dir)
|
||||||
ocr_service.init_det()
|
ocr_service.init_det()
|
||||||
if global_args.use_gpu:
|
if global_args.use_gpu:
|
||||||
ocr_service.prepare_server(
|
ocr_service.prepare_server(
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
# 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.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import argparse
|
||||||
|
from paddle_serving_client.io import inference_model_to_serving
|
||||||
|
def parse_args():
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("--model_dir", type=str)
|
||||||
|
parser.add_argument("--server_dir", type=str, default="serving_server_dir")
|
||||||
|
parser.add_argument("--client_dir", type=str, default="serving_client_dir")
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
args = parse_args()
|
||||||
|
inference_model_dir = args.model_dir
|
||||||
|
serving_client_dir = os.path.join(args.model_dir, args.server_dir)
|
||||||
|
serving_server_dir = os.path.join(args.model_dir, args.client_dir)
|
||||||
|
feed_var_names, fetch_var_names = inference_model_to_serving(
|
||||||
|
inference_model_dir, serving_client_dir, serving_server_dir, model_filename="model", params_filename="params")
|
||||||
|
|
||||||
|
print("success!")
|
|
@ -44,11 +44,11 @@ class TextSystemHelper(TextSystem):
|
||||||
if self.use_angle_cls:
|
if self.use_angle_cls:
|
||||||
self.clas_client = Debugger()
|
self.clas_client = Debugger()
|
||||||
self.clas_client.load_model_config(
|
self.clas_client.load_model_config(
|
||||||
global_args.cls_model_dir, gpu=True, profile=False)
|
global_args.cls_server_dir, gpu=True, profile=False)
|
||||||
self.text_classifier = TextClassifierHelper(args)
|
self.text_classifier = TextClassifierHelper(args)
|
||||||
self.det_client = Debugger()
|
self.det_client = Debugger()
|
||||||
self.det_client.load_model_config(
|
self.det_client.load_model_config(
|
||||||
global_args.det_model_dir, gpu=True, profile=False)
|
global_args.det_server_dir, gpu=True, profile=False)
|
||||||
self.fetch = ["save_infer_model/scale_0.tmp_0", "save_infer_model/scale_1.tmp_0"]
|
self.fetch = ["save_infer_model/scale_0.tmp_0", "save_infer_model/scale_1.tmp_0"]
|
||||||
|
|
||||||
def preprocess(self, img):
|
def preprocess(self, img):
|
||||||
|
@ -101,17 +101,20 @@ class OCRService(WebService):
|
||||||
if ".lod" in x:
|
if ".lod" in x:
|
||||||
self.tmp_args[x] = fetch_map[x]
|
self.tmp_args[x] = fetch_map[x]
|
||||||
rec_res = self.text_system.postprocess(outputs, self.tmp_args)
|
rec_res = self.text_system.postprocess(outputs, self.tmp_args)
|
||||||
res = {
|
res = []
|
||||||
"pred_text": [x[0] for x in rec_res],
|
for i in range(len(rec_res)):
|
||||||
"score": [str(x[1]) for x in rec_res],
|
tmp_res = {
|
||||||
"pos": [x.tolist() for x in self.text_system.dt_boxes]
|
"text_region": self.text_system.dt_boxes[i].tolist(),
|
||||||
}
|
"text": rec_res[i][0],
|
||||||
|
"confidence": float(rec_res[i][1])
|
||||||
|
}
|
||||||
|
res.append(tmp_res)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
ocr_service = OCRService(name="ocr")
|
ocr_service = OCRService(name="ocr")
|
||||||
ocr_service.load_model_config(global_args.rec_model_dir)
|
ocr_service.load_model_config(global_args.rec_server_dir)
|
||||||
ocr_service.init_rec()
|
ocr_service.init_rec()
|
||||||
if global_args.use_gpu:
|
if global_args.use_gpu:
|
||||||
ocr_service.prepare_server(
|
ocr_service.prepare_server(
|
||||||
|
|
|
@ -42,12 +42,14 @@ class TextSystemHelper(TextSystem):
|
||||||
if self.use_angle_cls:
|
if self.use_angle_cls:
|
||||||
self.clas_client = Client()
|
self.clas_client = Client()
|
||||||
self.clas_client.load_client_config(
|
self.clas_client.load_client_config(
|
||||||
"cls_infer_client/serving_client_conf.prototxt")
|
os.path.join(args.cls_client_dir, "serving_client_conf.prototxt")
|
||||||
|
)
|
||||||
self.clas_client.connect(["127.0.0.1:9294"])
|
self.clas_client.connect(["127.0.0.1:9294"])
|
||||||
self.text_classifier = TextClassifierHelper(args)
|
self.text_classifier = TextClassifierHelper(args)
|
||||||
self.det_client = Client()
|
self.det_client = Client()
|
||||||
self.det_client.load_client_config(
|
self.det_client.load_client_config(
|
||||||
"det_infer_client/serving_client_conf.prototxt")
|
os.path.join(args.det_client_dir, "serving_client_conf.prototxt")
|
||||||
|
)
|
||||||
self.det_client.connect(["127.0.0.1:9293"])
|
self.det_client.connect(["127.0.0.1:9293"])
|
||||||
self.fetch = ["save_infer_model/scale_0.tmp_0", "save_infer_model/scale_1.tmp_0"]
|
self.fetch = ["save_infer_model/scale_0.tmp_0", "save_infer_model/scale_1.tmp_0"]
|
||||||
|
|
||||||
|
@ -101,17 +103,20 @@ class OCRService(WebService):
|
||||||
if ".lod" in x:
|
if ".lod" in x:
|
||||||
self.tmp_args[x] = fetch_map[x]
|
self.tmp_args[x] = fetch_map[x]
|
||||||
rec_res = self.text_system.postprocess(outputs, self.tmp_args)
|
rec_res = self.text_system.postprocess(outputs, self.tmp_args)
|
||||||
res = {
|
res = []
|
||||||
"pred_text": [x[0] for x in rec_res],
|
for i in range(len(rec_res)):
|
||||||
"score": [str(x[1]) for x in rec_res],
|
tmp_res = {
|
||||||
"pos": [x.tolist() for x in self.text_system.dt_boxes]
|
"text_region": self.text_system.dt_boxes[i].tolist(),
|
||||||
}
|
"text": rec_res[i][0],
|
||||||
|
"confidence": float(rec_res[i][1])
|
||||||
|
}
|
||||||
|
res.append(tmp_res)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
ocr_service = OCRService(name="ocr")
|
ocr_service = OCRService(name="ocr")
|
||||||
ocr_service.load_model_config(global_args.rec_model_dir)
|
ocr_service.load_model_config(global_args.rec_server_dir)
|
||||||
ocr_service.init_rec()
|
ocr_service.init_rec()
|
||||||
if global_args.use_gpu:
|
if global_args.use_gpu:
|
||||||
ocr_service.prepare_server(
|
ocr_service.prepare_server(
|
||||||
|
|
|
@ -1,40 +0,0 @@
|
||||||
# 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:9292/ocr/prediction"
|
|
||||||
test_img_dir = "../../doc/imgs/"
|
|
||||||
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"]}
|
|
||||||
r = requests.post(url=url, headers=headers, data=json.dumps(data))
|
|
||||||
rjson = r.json()
|
|
||||||
print(rjson)
|
|
|
@ -14,7 +14,8 @@ def read_params():
|
||||||
|
|
||||||
#params for text detector
|
#params for text detector
|
||||||
cfg.det_algorithm = "DB"
|
cfg.det_algorithm = "DB"
|
||||||
cfg.det_model_dir = "./det_infer_server/"
|
cfg.det_server_dir = "../../ch_lite/ch_ppocr_mobile_v1.1_det_infer/serving_server_dir"
|
||||||
|
cfg.det_client_dir = "../../ch_lite/ch_ppocr_mobile_v1.1_det_infer/serving_client_dir"
|
||||||
cfg.det_max_side_len = 960
|
cfg.det_max_side_len = 960
|
||||||
|
|
||||||
#DB parmas
|
#DB parmas
|
||||||
|
@ -29,19 +30,21 @@ def read_params():
|
||||||
|
|
||||||
#params for text recognizer
|
#params for text recognizer
|
||||||
cfg.rec_algorithm = "CRNN"
|
cfg.rec_algorithm = "CRNN"
|
||||||
cfg.rec_model_dir = "./rec_infer_server/"
|
cfg.rec_server_dir = "../../ch_lite/ch_ppocr_mobile_v1.1_rec_infer/serving_server_dir"
|
||||||
|
cfg.rec_client_dir = "../../ch_lite/ch_ppocr_mobile_v1.1_rec_infer/serving_client_dir"
|
||||||
|
|
||||||
cfg.rec_image_shape = "3, 32, 320"
|
cfg.rec_image_shape = "3, 32, 320"
|
||||||
cfg.rec_char_type = 'ch'
|
cfg.rec_char_type = 'ch'
|
||||||
cfg.rec_batch_num = 30
|
cfg.rec_batch_num = 30
|
||||||
cfg.max_text_length = 25
|
cfg.max_text_length = 25
|
||||||
|
|
||||||
cfg.rec_char_dict_path = "./ppocr_keys_v1.txt"
|
cfg.rec_char_dict_path = "../../ppocr/utils/ppocr_keys_v1.txt"
|
||||||
cfg.use_space_char = True
|
cfg.use_space_char = True
|
||||||
|
|
||||||
#params for text classifier
|
#params for text classifier
|
||||||
cfg.use_angle_cls = True
|
cfg.use_angle_cls = True
|
||||||
cfg.cls_model_dir = "./cls_infer_server/"
|
cfg.cls_server_dir = "../../ch_lite/ch_ppocr_mobile_v1.1_cls_infer/serving_server_dir"
|
||||||
|
cfg.cls_client_dir = "../../ch_lite/ch_ppocr_mobile_v1.1_cls_infer/serving_client_dir"
|
||||||
cfg.cls_image_shape = "3, 48, 192"
|
cfg.cls_image_shape = "3, 48, 192"
|
||||||
cfg.label_list = ['0', '180']
|
cfg.label_list = ['0', '180']
|
||||||
cfg.cls_batch_num = 30
|
cfg.cls_batch_num = 30
|
||||||
|
|
|
@ -0,0 +1,117 @@
|
||||||
|
# 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.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
__dir__ = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
sys.path.append(__dir__)
|
||||||
|
sys.path.append(os.path.abspath(os.path.join(__dir__, '..')))
|
||||||
|
sys.path.append(os.path.abspath(os.path.join(__dir__, '../../')))
|
||||||
|
|
||||||
|
from ppocr.utils.utility import initial_logger
|
||||||
|
logger = initial_logger()
|
||||||
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
import time
|
||||||
|
from PIL import Image
|
||||||
|
from ppocr.utils.utility import get_image_file_list
|
||||||
|
from tools.infer.utility import draw_ocr, draw_boxes
|
||||||
|
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
import base64
|
||||||
|
|
||||||
|
|
||||||
|
def cv2_to_base64(image):
|
||||||
|
return base64.b64encode(image).decode('utf8')
|
||||||
|
|
||||||
|
|
||||||
|
def draw_server_result(image_file, res):
|
||||||
|
img = cv2.imread(image_file)
|
||||||
|
image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
||||||
|
if len(res) == 0:
|
||||||
|
return np.array(image)
|
||||||
|
keys = res[0].keys()
|
||||||
|
if 'text_region' not in keys: # for rec or clas, draw function is invalid
|
||||||
|
logger.info("draw function is invalid for rec or clas!")
|
||||||
|
return None
|
||||||
|
elif 'text' not in keys: # for ocr_det
|
||||||
|
logger.info("draw text boxes only!")
|
||||||
|
boxes = []
|
||||||
|
for dno in range(len(res)):
|
||||||
|
boxes.append(res[dno]['text_region'])
|
||||||
|
boxes = np.array(boxes)
|
||||||
|
draw_img = draw_boxes(image, boxes)
|
||||||
|
return draw_img
|
||||||
|
else: # for ocr_system
|
||||||
|
logger.info("draw boxes and texts!")
|
||||||
|
boxes = []
|
||||||
|
texts = []
|
||||||
|
scores = []
|
||||||
|
for dno in range(len(res)):
|
||||||
|
boxes.append(res[dno]['text_region'])
|
||||||
|
texts.append(res[dno]['text'])
|
||||||
|
scores.append(res[dno]['confidence'])
|
||||||
|
boxes = np.array(boxes)
|
||||||
|
scores = np.array(scores)
|
||||||
|
draw_img = draw_ocr(image, boxes, texts, scores, drop_score=0.5, font_path="../../doc/simfang.ttf")
|
||||||
|
return draw_img
|
||||||
|
|
||||||
|
|
||||||
|
def main(image_path):
|
||||||
|
image_file_list = get_image_file_list(image_path)
|
||||||
|
is_visualize = True
|
||||||
|
headers = {"Content-type": "application/json"}
|
||||||
|
url = "http://127.0.0.1:9292/ocr/prediction"
|
||||||
|
cnt = 0
|
||||||
|
total_time = 0
|
||||||
|
for image_file in image_file_list:
|
||||||
|
img = open(image_file, 'rb').read()
|
||||||
|
if img is None:
|
||||||
|
logger.info("error in loading image:{}".format(image_file))
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 发送HTTP请求
|
||||||
|
starttime = time.time()
|
||||||
|
data = {"feed": [{"image": cv2_to_base64(img)}], "fetch": ["res"]}
|
||||||
|
r = requests.post(url=url, headers=headers, data=json.dumps(data))
|
||||||
|
elapse = time.time() - starttime
|
||||||
|
total_time += elapse
|
||||||
|
logger.info("Predict time of %s: %.3fs" % (image_file, elapse))
|
||||||
|
res = r.json()['result']
|
||||||
|
logger.info(res)
|
||||||
|
|
||||||
|
if is_visualize:
|
||||||
|
draw_img = draw_server_result(image_file, res)
|
||||||
|
if draw_img is not None:
|
||||||
|
draw_img_save = "./server_results/"
|
||||||
|
if not os.path.exists(draw_img_save):
|
||||||
|
os.makedirs(draw_img_save)
|
||||||
|
cv2.imwrite(
|
||||||
|
os.path.join(draw_img_save, os.path.basename(image_file)),
|
||||||
|
draw_img[:, :, ::-1])
|
||||||
|
logger.info("The visualized image saved in {}".format(
|
||||||
|
os.path.join(draw_img_save, os.path.basename(image_file))))
|
||||||
|
cnt += 1
|
||||||
|
if cnt % 100 == 0:
|
||||||
|
logger.info("{} processed".format(cnt))
|
||||||
|
logger.info("avg time cost: {}".format(float(total_time) / cnt))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
if len(sys.argv) != 2:
|
||||||
|
logger.info("Usage: %s image_path" % sys.argv[0])
|
||||||
|
else:
|
||||||
|
image_path = sys.argv[1]
|
||||||
|
main(image_path)
|
|
@ -0,0 +1,195 @@
|
||||||
|
[English](readme_en.md) | 简体中文
|
||||||
|
|
||||||
|
PaddleOCR提供2种服务部署方式:
|
||||||
|
- 基于PaddleHub Serving的部署:代码路径为"`./deploy/hubserving`",使用方法参考[文档](../hubserving/readme.md)。
|
||||||
|
- 基于PaddleServing的部署:代码路径为"`./deploy/pdserving`",按照本教程使用。
|
||||||
|
|
||||||
|
# Paddle Serving 服务部署
|
||||||
|
本教程将介绍基于[Paddle Serving](https://github.com/PaddlePaddle/Serving)部署PaddleOCR在线预测服务的详细步骤。
|
||||||
|
- [快速启动服务](#快速启动服务)
|
||||||
|
- [1. 准备环境](#准备环境)
|
||||||
|
- [2. 转换模型](#转换模型)
|
||||||
|
- [3. 启动服务](#启动服务)
|
||||||
|
- [发送预测请求](#发送预测请求)
|
||||||
|
|
||||||
|
pdserving服务部署目录下包括`检测`、`识别`、`2阶段串联`三种服务部署工具,请根据需求选择相应的服务。目录结构如下:
|
||||||
|
```
|
||||||
|
deploy/pdserving/
|
||||||
|
└─ det_local_server.py 快速版 检测 服务端
|
||||||
|
└─ det_rpc_server.py 标准版 检测 服务端
|
||||||
|
└─ rec_local_server.py 快速版 识别 服务端
|
||||||
|
└─ rec_rpc_server.py 标准版 识别 服务端
|
||||||
|
└─ ocr_local_server.py 快速版 串联 服务端
|
||||||
|
└─ ocr_rpc_server.py 标准版 串联 服务端
|
||||||
|
└─ ocr_web_client.py 客户端
|
||||||
|
└─ params.py 配置文件
|
||||||
|
```
|
||||||
|
|
||||||
|
<a name="快速启动服务"></a>
|
||||||
|
## 快速启动服务
|
||||||
|
|
||||||
|
<a name="准备环境"></a>
|
||||||
|
### 1. 准备环境
|
||||||
|
环境版本要求:
|
||||||
|
- **CUDA版本:9.X/10.X**
|
||||||
|
- **CUDNN版本:7.X**
|
||||||
|
- **操作系统版本:Linux/Windows**
|
||||||
|
- **Python版本: 2.7/3.5/3.6/3.7**
|
||||||
|
|
||||||
|
**Python操作指南:**
|
||||||
|
|
||||||
|
目前Serving用于OCR的部分功能还在测试当中,因此在这里我们给出[Servnig latest package](https://github.com/PaddlePaddle/Serving/blob/develop/doc/LATEST_PACKAGES.md)
|
||||||
|
大家根据自己的环境选择需要安装的whl包即可,例如以Python 3.6为例,执行下列命令:
|
||||||
|
```
|
||||||
|
# 安装服务端,CPU/GPU版本选择一个
|
||||||
|
# GPU版本服务端
|
||||||
|
# CUDA 9
|
||||||
|
python -m pip install -U https://paddle-serving.bj.bcebos.com/whl/paddle_serving_server_gpu-0.0.0.post9-py3-none-any.whl
|
||||||
|
# CUDA 10
|
||||||
|
python -m pip install -U https://paddle-serving.bj.bcebos.com/whl/paddle_serving_server_gpu-0.0.0.post10-py3-none-any.whl
|
||||||
|
# CPU版本服务端
|
||||||
|
python -m pip install -U https://paddle-serving.bj.bcebos.com/whl/paddle_serving_server-0.0.0-py3-none-any.whl
|
||||||
|
|
||||||
|
# 安装客户端和App包,CPU、GPU通用
|
||||||
|
python -m pip install -U https://paddle-serving.bj.bcebos.com/whl/paddle_serving_client-0.0.0-cp35-none-any.whl https://paddle-serving.bj.bcebos.com/whl/paddle_serving_app-0.0.0-py3-none-any.whl
|
||||||
|
|
||||||
|
# 安装其他依赖
|
||||||
|
pip3.6 install func-timeout
|
||||||
|
```
|
||||||
|
|
||||||
|
<a name="转换模型"></a>
|
||||||
|
## 2. 转换模型
|
||||||
|
|
||||||
|
Paddle Serving无法直接用训练模型(checkpoints 模型)或推理模型(inference 模型)进行部署。Serving模型由两个文件夹构成,用于存放客户端和服务端的配置。本节介绍如何将推理模型转换为Paddle Serving可部署的模型。
|
||||||
|
|
||||||
|
**以文本检测模型`ch_ppocr_mobile_v1.1_det_infer`为例,文本识别模型和方向分类器的转换同理。**
|
||||||
|
|
||||||
|
首先下载推理模型:
|
||||||
|
```shell
|
||||||
|
wget -P ./inference/ https://paddleocr.bj.bcebos.com/20-09-22/mobile/det/ch_ppocr_mobile_v1.1_det_infer.tar && tar xf ./inference/ch_ppocr_mobile_v1.1_det_infer.tar -C ./inference/
|
||||||
|
```
|
||||||
|
然后运行如下python脚本进行转换,其中,使用参数`model_dir`指定待转换的推理模型路径:
|
||||||
|
```
|
||||||
|
python deploy/pdserving/inference_to_serving.py --model_dir ./inference/ch_ppocr_mobile_v1.1_det_infer
|
||||||
|
```
|
||||||
|
最终会在`ch_ppocr_mobile_v1.1_det_infer`目录下生成客户端和服务端的模型配置,结构如下:
|
||||||
|
```
|
||||||
|
/ch_ppocr_mobile_v1.1_det_infer/
|
||||||
|
├── serving_client_dir # 客户端配置文件夹
|
||||||
|
└── serving_server_dir # 服务端配置文件夹
|
||||||
|
```
|
||||||
|
|
||||||
|
<a name="启动服务"></a>
|
||||||
|
## 3. 启动服务
|
||||||
|
|
||||||
|
启动服务可以根据实际需求选择启动`标准版`或者`快速版`,两种方式的对比如下表:
|
||||||
|
|
||||||
|
|版本|特点|适用场景|
|
||||||
|
|-|-|-|
|
||||||
|
|标准版|稳定性高,分布式部署|适用于吞吐量大,需要跨机房部署的情况,只能用于Linux平台|
|
||||||
|
|快速版|部署方便,预测速度快|适用于对预测速度要求高,迭代速度快的场景,可以支持Linux/Windows|
|
||||||
|
|
||||||
|
|
||||||
|
**step 1. 配置环境变量**
|
||||||
|
|
||||||
|
```
|
||||||
|
# 以下两步的顺序不能反
|
||||||
|
export PYTHONPATH=$PWD:$PYTHONPATH
|
||||||
|
cd deploy/pdserving
|
||||||
|
```
|
||||||
|
|
||||||
|
**step 2. 修改配置参数**
|
||||||
|
|
||||||
|
配置参数在`params.py`中,具体内容如下所示,可根据需要修改相关参数,如修改模型路径、修改后处理参数等。
|
||||||
|
|
||||||
|
```
|
||||||
|
def read_params():
|
||||||
|
cfg = Config()
|
||||||
|
#use gpu
|
||||||
|
cfg.use_gpu = False #是否使用GPU,False代表使用CPU
|
||||||
|
cfg.use_pdserving = True #使用paddle serving部署时必须为True
|
||||||
|
|
||||||
|
#params for text detector
|
||||||
|
cfg.det_algorithm = "DB"
|
||||||
|
cfg.det_server_dir = "../../inference/ch_ppocr_mobile_v1.1_det_infer/serving_server_dir"
|
||||||
|
cfg.det_client_dir = "../../inference/ch_ppocr_mobile_v1.1_det_infer/serving_client_dir"
|
||||||
|
cfg.det_max_side_len = 960
|
||||||
|
|
||||||
|
#DB parmas
|
||||||
|
cfg.det_db_thresh =0.3
|
||||||
|
cfg.det_db_box_thresh =0.5
|
||||||
|
cfg.det_db_unclip_ratio =2.0
|
||||||
|
|
||||||
|
#EAST parmas
|
||||||
|
cfg.det_east_score_thresh = 0.8
|
||||||
|
cfg.det_east_cover_thresh = 0.1
|
||||||
|
cfg.det_east_nms_thresh = 0.2
|
||||||
|
|
||||||
|
#params for text recognizer
|
||||||
|
cfg.rec_algorithm = "CRNN"
|
||||||
|
cfg.rec_server_dir = "../../inference/ch_ppocr_mobile_v1.1_rec_infer/serving_server_dir"
|
||||||
|
cfg.rec_client_dir = "../../inference/ch_ppocr_mobile_v1.1_rec_infer/serving_client_dir"
|
||||||
|
|
||||||
|
cfg.rec_image_shape = "3, 32, 320"
|
||||||
|
cfg.rec_char_type = 'ch'
|
||||||
|
cfg.rec_batch_num = 30
|
||||||
|
cfg.max_text_length = 25
|
||||||
|
|
||||||
|
cfg.rec_char_dict_path = "../../ppocr/utils/ppocr_keys_v1.txt"
|
||||||
|
cfg.use_space_char = True
|
||||||
|
|
||||||
|
#params for text classifier
|
||||||
|
cfg.use_angle_cls = True
|
||||||
|
cfg.cls_server_dir = "../../inference/ch_ppocr_mobile_v1.1_cls_infer/serving_server_dir"
|
||||||
|
cfg.cls_client_dir = "../../inference/ch_ppocr_mobile_v1.1_cls_infer/serving_client_dir"
|
||||||
|
cfg.cls_image_shape = "3, 48, 192"
|
||||||
|
cfg.label_list = ['0', '180']
|
||||||
|
cfg.cls_batch_num = 30
|
||||||
|
cfg.cls_thresh = 0.9
|
||||||
|
|
||||||
|
return cfg
|
||||||
|
```
|
||||||
|
|
||||||
|
**step 3_1. 启动独立的检测服务或识别服务**
|
||||||
|
|
||||||
|
如果只需要搭建检测服务或识别服务,一行命令即可,检测服务的启动方式如下,识别同理。检测+识别的串联服务请直接跳至step 3_2。
|
||||||
|
|
||||||
|
```
|
||||||
|
# 启动文本检测服务,标准版/快速版 二选一
|
||||||
|
python det_rpc_server.py #标准版,Linux用户
|
||||||
|
python det_local_server.py #快速版,Windows/Linux用户
|
||||||
|
```
|
||||||
|
|
||||||
|
**step 3_2. 启动文本检测、识别串联的服务**
|
||||||
|
|
||||||
|
如果需要搭建检测+识别的串联服务,快速版与step 3_1中的独立服务启动方式相同,但标准版略有不同,具体步骤如下:
|
||||||
|
|
||||||
|
```
|
||||||
|
# 标准版,Linux用户
|
||||||
|
# GPU用户
|
||||||
|
# 启动检测服务
|
||||||
|
python -m paddle_serving_server_gpu.serve --model inference/ch_ppocr_mobile_v1.1_det_infer/serving_server_dir/ --port 9293 --gpu_id 0
|
||||||
|
# 启动方向分类器服务
|
||||||
|
python -m paddle_serving_server_gpu.serve --model inference/ch_ppocr_mobile_v1.1_cls_infer/serving_server_dir/ --port 9294 --gpu_id 0
|
||||||
|
# 启动串联服务
|
||||||
|
python ocr_rpc_server.py
|
||||||
|
|
||||||
|
# CPU用户
|
||||||
|
# 启动检测服务
|
||||||
|
python -m paddle_serving_server.serve --model inference/ch_ppocr_mobile_v1.1_det_infer/serving_server_dir/ --port 9293
|
||||||
|
# 启动方向分类器服务
|
||||||
|
python -m paddle_serving_server.serve --model ch_ppocr_mobile_v1.1_cls_infer/serving_server_dir/ --port 9294
|
||||||
|
# 启动串联服务
|
||||||
|
python ocr_rpc_server.py
|
||||||
|
|
||||||
|
# 快速版,Windows/Linux用户
|
||||||
|
python ocr_local_server.py
|
||||||
|
```
|
||||||
|
|
||||||
|
<a name="发送预测请求"></a>
|
||||||
|
## 发送预测请求
|
||||||
|
以上所有单独或串联的服务均可使用如下客户端进行访问:
|
||||||
|
```
|
||||||
|
python pdserving_client.py image_path
|
||||||
|
```
|
||||||
|
|
|
@ -153,16 +153,18 @@ class OCRService(WebService):
|
||||||
if ".lod" in x:
|
if ".lod" in x:
|
||||||
self.tmp_args[x] = fetch_map[x]
|
self.tmp_args[x] = fetch_map[x]
|
||||||
rec_res = self.text_recognizer.postprocess(outputs, self.tmp_args)
|
rec_res = self.text_recognizer.postprocess(outputs, self.tmp_args)
|
||||||
res = {
|
res = []
|
||||||
"pred_text": [x[0] for x in rec_res],
|
for i in range(len(rec_res)):
|
||||||
"score": [str(x[1]) for x in rec_res]
|
res.append({
|
||||||
}
|
"text": rec_res[i][0],
|
||||||
|
"confidence": float(rec_res[i][1])
|
||||||
|
})
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
ocr_service = OCRService(name="ocr")
|
ocr_service = OCRService(name="ocr")
|
||||||
ocr_service.load_model_config(global_args.rec_model_dir)
|
ocr_service.load_model_config(global_args.rec_server_dir)
|
||||||
ocr_service.init_rec()
|
ocr_service.init_rec()
|
||||||
if global_args.use_gpu:
|
if global_args.use_gpu:
|
||||||
ocr_service.prepare_server(
|
ocr_service.prepare_server(
|
||||||
|
|
|
@ -158,16 +158,18 @@ class OCRService(WebService):
|
||||||
if ".lod" in x:
|
if ".lod" in x:
|
||||||
self.tmp_args[x] = fetch_map[x]
|
self.tmp_args[x] = fetch_map[x]
|
||||||
rec_res = self.text_recognizer.postprocess(outputs, self.tmp_args)
|
rec_res = self.text_recognizer.postprocess(outputs, self.tmp_args)
|
||||||
res = {
|
res = []
|
||||||
"pred_text": [x[0] for x in rec_res],
|
for i in range(len(rec_res)):
|
||||||
"score": [str(x[1]) for x in rec_res]
|
res.append({
|
||||||
}
|
"text": rec_res[i][0],
|
||||||
|
"confidence": float(rec_res[i][1])
|
||||||
|
})
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
ocr_service = OCRService(name="ocr")
|
ocr_service = OCRService(name="ocr")
|
||||||
ocr_service.load_model_config(global_args.rec_model_dir)
|
ocr_service.load_model_config(global_args.rec_server_dir)
|
||||||
ocr_service.init_rec()
|
ocr_service.init_rec()
|
||||||
if global_args.use_gpu:
|
if global_args.use_gpu:
|
||||||
ocr_service.prepare_server(
|
ocr_service.prepare_server(
|
||||||
|
|
Loading…
Reference in New Issue