未验证 提交 a987ec70 编写于 作者: D dyning 提交者: GitHub

Merge pull request #308 from MissPenguin/develop

modify hubserving
...@@ -3,12 +3,14 @@ ...@@ -3,12 +3,14 @@
"ocr_det": { "ocr_det": {
"init_args": { "init_args": {
"version": "1.0.0", "version": "1.0.0",
"det_model_dir": "./inference/ch_det_mv3_db/",
"use_gpu": true "use_gpu": true
}, },
"predict_args": { "predict_args": {
"visualization": false "visualization": false
} }
} }
} },
"port": 8866,
"use_multiprocess": false,
"workers": 2
} }
...@@ -22,8 +22,6 @@ import paddlehub as hub ...@@ -22,8 +22,6 @@ import paddlehub as hub
from tools.infer.utility import draw_boxes, base64_to_cv2 from tools.infer.utility import draw_boxes, base64_to_cv2
from tools.infer.predict_det import TextDetector from tools.infer.predict_det import TextDetector
class Config(object):
pass
@moduleinfo( @moduleinfo(
name="ocr_det", name="ocr_det",
...@@ -33,43 +31,28 @@ class Config(object): ...@@ -33,43 +31,28 @@ class Config(object):
author_email="paddle-dev@baidu.com", author_email="paddle-dev@baidu.com",
type="cv/text_recognition") type="cv/text_recognition")
class OCRDet(hub.Module): class OCRDet(hub.Module):
def _initialize(self, def _initialize(self, use_gpu=False):
det_model_dir="",
det_algorithm="DB",
use_gpu=False
):
""" """
initialize with the necessary elements initialize with the necessary elements
""" """
self.config = Config() from ocr_det.params import read_params
self.config.use_gpu = use_gpu cfg = read_params()
cfg.use_gpu = use_gpu
if use_gpu: if use_gpu:
try: try:
_places = os.environ["CUDA_VISIBLE_DEVICES"] _places = os.environ["CUDA_VISIBLE_DEVICES"]
int(_places[0]) int(_places[0])
print("use gpu: ", use_gpu) print("use gpu: ", use_gpu)
print("CUDA_VISIBLE_DEVICES: ", _places) print("CUDA_VISIBLE_DEVICES: ", _places)
cfg.gpu_mem = 8000
except: except:
raise RuntimeError( raise RuntimeError(
"Environment Variable CUDA_VISIBLE_DEVICES is not set correctly. If you wanna use gpu, please set CUDA_VISIBLE_DEVICES via export CUDA_VISIBLE_DEVICES=cuda_device_id." "Environment Variable CUDA_VISIBLE_DEVICES is not set correctly. If you wanna use gpu, please set CUDA_VISIBLE_DEVICES via export CUDA_VISIBLE_DEVICES=cuda_device_id."
) )
self.config.ir_optim = True cfg.ir_optim = True
self.config.gpu_mem = 8000
#params for text detector
self.config.det_algorithm = det_algorithm
self.config.det_model_dir = det_model_dir
# self.config.det_model_dir = "./inference/det/"
#DB parmas
self.config.det_db_thresh =0.3
self.config.det_db_box_thresh =0.5
self.config.det_db_unclip_ratio =2.0
#EAST parmas self.text_detector = TextDetector(cfg)
self.config.det_east_score_thresh = 0.8
self.config.det_east_cover_thresh = 0.1
self.config.det_east_nms_thresh = 0.2
def read_images(self, paths=[]): def read_images(self, paths=[]):
images = [] images = []
...@@ -83,10 +66,9 @@ class OCRDet(hub.Module): ...@@ -83,10 +66,9 @@ class OCRDet(hub.Module):
images.append(img) images.append(img)
return images return images
def det_text(self, def predict(self,
images=[], images=[],
paths=[], paths=[],
det_max_side_len=960,
draw_img_save='ocr_det_result', draw_img_save='ocr_det_result',
visualization=False): visualization=False):
""" """
...@@ -94,10 +76,8 @@ class OCRDet(hub.Module): ...@@ -94,10 +76,8 @@ class OCRDet(hub.Module):
Args: Args:
images (list(numpy.ndarray)): images data, shape of each is [H, W, C]. If images not paths images (list(numpy.ndarray)): images data, shape of each is [H, W, C]. If images not paths
paths (list[str]): The paths of images. If paths not images paths (list[str]): The paths of images. If paths not images
use_gpu (bool): Whether to use gpu. Default false. draw_img_save (str): The directory to store output images.
output_dir (str): The directory to store output images.
visualization (bool): Whether to save image or not. visualization (bool): Whether to save image or not.
box_thresh(float): the threshold of the detected text box's confidence
Returns: Returns:
res (list): The result of text detection box and save path of images. res (list): The result of text detection box and save path of images.
""" """
...@@ -111,8 +91,6 @@ class OCRDet(hub.Module): ...@@ -111,8 +91,6 @@ class OCRDet(hub.Module):
assert predicted_data != [], "There is not any image to be predicted. Please check the input data." assert predicted_data != [], "There is not any image to be predicted. Please check the input data."
self.config.det_max_side_len = det_max_side_len
text_detector = TextDetector(self.config)
all_results = [] all_results = []
for img in predicted_data: for img in predicted_data:
result = {'save_path': ''} result = {'save_path': ''}
...@@ -121,7 +99,7 @@ class OCRDet(hub.Module): ...@@ -121,7 +99,7 @@ class OCRDet(hub.Module):
result['data'] = [] result['data'] = []
all_results.append(result) all_results.append(result)
continue continue
dt_boxes, elapse = text_detector(img) dt_boxes, elapse = self.text_detector(img)
print("Predict time : ", elapse) print("Predict time : ", elapse)
result['data'] = dt_boxes.astype(np.int).tolist() result['data'] = dt_boxes.astype(np.int).tolist()
...@@ -146,7 +124,7 @@ class OCRDet(hub.Module): ...@@ -146,7 +124,7 @@ class OCRDet(hub.Module):
Run as a service. Run as a service.
""" """
images_decode = [base64_to_cv2(image) for image in images] images_decode = [base64_to_cv2(image) for image in images]
results = self.det_text(images_decode, **kwargs) results = self.predict(images_decode, **kwargs)
return results return results
...@@ -156,5 +134,5 @@ if __name__ == '__main__': ...@@ -156,5 +134,5 @@ if __name__ == '__main__':
'./doc/imgs/11.jpg', './doc/imgs/11.jpg',
'./doc/imgs/12.jpg', './doc/imgs/12.jpg',
] ]
res = ocr.det_text(paths=image_path, visualization=True) res = ocr.predict(paths=image_path, visualization=True)
print(res) print(res)
\ No newline at end of file
# -*- coding:utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
class Config(object):
pass
def read_params():
cfg = Config()
#params for text detector
cfg.det_algorithm = "DB"
cfg.det_model_dir = "./inference/ch_det_mv3_db/"
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_model_dir = "./inference/ch_det_mv3_crnn/"
# cfg.rec_image_shape = "3, 32, 320"
# cfg.rec_char_type = 'ch'
# cfg.rec_batch_num = 30
# cfg.rec_char_dict_path = "./ppocr/utils/ppocr_keys_v1.txt"
# cfg.use_space_char = True
return cfg
\ No newline at end of file
...@@ -3,11 +3,13 @@ ...@@ -3,11 +3,13 @@
"ocr_rec": { "ocr_rec": {
"init_args": { "init_args": {
"version": "1.0.0", "version": "1.0.0",
"det_model_dir": "./inference/ch_rec_mv3_crnn/",
"use_gpu": true "use_gpu": true
}, },
"predict_args": { "predict_args": {
} }
} }
} },
"port": 8867,
"use_multiprocess": false,
"workers": 2
} }
...@@ -22,8 +22,6 @@ import paddlehub as hub ...@@ -22,8 +22,6 @@ import paddlehub as hub
from tools.infer.utility import base64_to_cv2 from tools.infer.utility import base64_to_cv2
from tools.infer.predict_rec import TextRecognizer from tools.infer.predict_rec import TextRecognizer
class Config(object):
pass
@moduleinfo( @moduleinfo(
name="ocr_rec", name="ocr_rec",
...@@ -33,41 +31,28 @@ class Config(object): ...@@ -33,41 +31,28 @@ class Config(object):
author_email="paddle-dev@baidu.com", author_email="paddle-dev@baidu.com",
type="cv/text_recognition") type="cv/text_recognition")
class OCRRec(hub.Module): class OCRRec(hub.Module):
def _initialize(self, def _initialize(self, use_gpu=False):
rec_model_dir="",
rec_algorithm="CRNN",
rec_char_dict_path="./ppocr/utils/ppocr_keys_v1.txt",
rec_batch_num=30,
use_gpu=False
):
""" """
initialize with the necessary elements initialize with the necessary elements
""" """
self.config = Config() from ocr_rec.params import read_params
self.config.use_gpu = use_gpu cfg = read_params()
cfg.use_gpu = use_gpu
if use_gpu: if use_gpu:
try: try:
_places = os.environ["CUDA_VISIBLE_DEVICES"] _places = os.environ["CUDA_VISIBLE_DEVICES"]
int(_places[0]) int(_places[0])
print("use gpu: ", use_gpu) print("use gpu: ", use_gpu)
print("CUDA_VISIBLE_DEVICES: ", _places) print("CUDA_VISIBLE_DEVICES: ", _places)
cfg.gpu_mem = 8000
except: except:
raise RuntimeError( raise RuntimeError(
"Environment Variable CUDA_VISIBLE_DEVICES is not set correctly. If you wanna use gpu, please set CUDA_VISIBLE_DEVICES via export CUDA_VISIBLE_DEVICES=cuda_device_id." "Environment Variable CUDA_VISIBLE_DEVICES is not set correctly. If you wanna use gpu, please set CUDA_VISIBLE_DEVICES via export CUDA_VISIBLE_DEVICES=cuda_device_id."
) )
self.config.ir_optim = True cfg.ir_optim = True
self.config.gpu_mem = 8000
#params for text recognizer self.text_recognizer = TextRecognizer(cfg)
self.config.rec_algorithm = rec_algorithm
self.config.rec_model_dir = rec_model_dir
# self.config.rec_model_dir = "./inference/rec/"
self.config.rec_image_shape = "3, 32, 320"
self.config.rec_char_type = 'ch'
self.config.rec_batch_num = rec_batch_num
self.config.rec_char_dict_path = rec_char_dict_path
self.config.use_space_char = True
def read_images(self, paths=[]): def read_images(self, paths=[]):
images = [] images = []
...@@ -81,7 +66,7 @@ class OCRRec(hub.Module): ...@@ -81,7 +66,7 @@ class OCRRec(hub.Module):
images.append(img) images.append(img)
return images return images
def rec_text(self, def predict(self,
images=[], images=[],
paths=[]): paths=[]):
""" """
...@@ -102,14 +87,13 @@ class OCRRec(hub.Module): ...@@ -102,14 +87,13 @@ class OCRRec(hub.Module):
assert predicted_data != [], "There is not any image to be predicted. Please check the input data." assert predicted_data != [], "There is not any image to be predicted. Please check the input data."
text_recognizer = TextRecognizer(self.config)
img_list = [] img_list = []
for img in predicted_data: for img in predicted_data:
if img is None: if img is None:
continue continue
img_list.append(img) img_list.append(img)
try: try:
rec_res, predict_time = text_recognizer(img_list) rec_res, predict_time = self.text_recognizer(img_list)
except Exception as e: except Exception as e:
print(e) print(e)
return [] return []
...@@ -121,7 +105,7 @@ class OCRRec(hub.Module): ...@@ -121,7 +105,7 @@ class OCRRec(hub.Module):
Run as a service. Run as a service.
""" """
images_decode = [base64_to_cv2(image) for image in images] images_decode = [base64_to_cv2(image) for image in images]
results = self.det_text(images_decode, **kwargs) results = self.predict(images_decode, **kwargs)
return results return results
...@@ -132,5 +116,5 @@ if __name__ == '__main__': ...@@ -132,5 +116,5 @@ if __name__ == '__main__':
'./doc/imgs_words/ch/word_2.jpg', './doc/imgs_words/ch/word_2.jpg',
'./doc/imgs_words/ch/word_3.jpg', './doc/imgs_words/ch/word_3.jpg',
] ]
res = ocr.rec_text(paths=image_path) res = ocr.predict(paths=image_path)
print(res) print(res)
\ No newline at end of file
# -*- coding:utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
class Config(object):
pass
def read_params():
cfg = Config()
# #params for text detector
# cfg.det_algorithm = "DB"
# cfg.det_model_dir = "./inference/ch_det_mv3_db/"
# 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_model_dir = "./inference/ch_rec_mv3_crnn/"
cfg.rec_image_shape = "3, 32, 320"
cfg.rec_char_type = 'ch'
cfg.rec_batch_num = 30
cfg.rec_char_dict_path = "./ppocr/utils/ppocr_keys_v1.txt"
cfg.use_space_char = True
return cfg
\ No newline at end of file
...@@ -3,14 +3,15 @@ ...@@ -3,14 +3,15 @@
"ocr_system": { "ocr_system": {
"init_args": { "init_args": {
"version": "1.0.0", "version": "1.0.0",
"det_model_dir": "./inference/ch_det_mv3_db/",
"rec_model_dir": "./inference/ch_rec_mv3_crnn/",
"use_gpu": true "use_gpu": true
}, },
"predict_args": { "predict_args": {
"visualization": false "visualization": false
} }
} }
} },
"port": 8868,
"use_multiprocess": false,
"workers": 2
} }
...@@ -23,9 +23,6 @@ from tools.infer.utility import draw_ocr, base64_to_cv2 ...@@ -23,9 +23,6 @@ from tools.infer.utility import draw_ocr, base64_to_cv2
from tools.infer.predict_system import TextSystem from tools.infer.predict_system import TextSystem
class Config(object):
pass
@moduleinfo( @moduleinfo(
name="ocr_system", name="ocr_system",
version="1.0.0", version="1.0.0",
...@@ -34,58 +31,28 @@ class Config(object): ...@@ -34,58 +31,28 @@ class Config(object):
author_email="paddle-dev@baidu.com", author_email="paddle-dev@baidu.com",
type="cv/text_recognition") type="cv/text_recognition")
class OCRSystem(hub.Module): class OCRSystem(hub.Module):
def _initialize(self, def _initialize(self, use_gpu=False):
det_model_dir="",
det_algorithm="DB",
rec_model_dir="",
rec_algorithm="CRNN",
rec_char_dict_path="./ppocr/utils/ppocr_keys_v1.txt",
rec_batch_num=30,
use_gpu=False
):
""" """
initialize with the necessary elements initialize with the necessary elements
""" """
self.config = Config() from ocr_system.params import read_params
self.config.use_gpu = use_gpu cfg = read_params()
cfg.use_gpu = use_gpu
if use_gpu: if use_gpu:
try: try:
_places = os.environ["CUDA_VISIBLE_DEVICES"] _places = os.environ["CUDA_VISIBLE_DEVICES"]
int(_places[0]) int(_places[0])
print("use gpu: ", use_gpu) print("use gpu: ", use_gpu)
print("CUDA_VISIBLE_DEVICES: ", _places) print("CUDA_VISIBLE_DEVICES: ", _places)
cfg.gpu_mem = 8000
except: except:
raise RuntimeError( raise RuntimeError(
"Environment Variable CUDA_VISIBLE_DEVICES is not set correctly. If you wanna use gpu, please set CUDA_VISIBLE_DEVICES via export CUDA_VISIBLE_DEVICES=cuda_device_id." "Environment Variable CUDA_VISIBLE_DEVICES is not set correctly. If you wanna use gpu, please set CUDA_VISIBLE_DEVICES via export CUDA_VISIBLE_DEVICES=cuda_device_id."
) )
self.config.ir_optim = True cfg.ir_optim = True
self.config.gpu_mem = 8000
self.text_sys = TextSystem(cfg)
#params for text detector
self.config.det_algorithm = det_algorithm
self.config.det_model_dir = det_model_dir
# self.config.det_model_dir = "./inference/det/"
#DB parmas
self.config.det_db_thresh =0.3
self.config.det_db_box_thresh =0.5
self.config.det_db_unclip_ratio =2.0
#EAST parmas
self.config.det_east_score_thresh = 0.8
self.config.det_east_cover_thresh = 0.1
self.config.det_east_nms_thresh = 0.2
#params for text recognizer
self.config.rec_algorithm = rec_algorithm
self.config.rec_model_dir = rec_model_dir
# self.config.rec_model_dir = "./inference/rec/"
self.config.rec_image_shape = "3, 32, 320"
self.config.rec_char_type = 'ch'
self.config.rec_batch_num = rec_batch_num
self.config.rec_char_dict_path = rec_char_dict_path
self.config.use_space_char = True
def read_images(self, paths=[]): def read_images(self, paths=[]):
images = [] images = []
...@@ -99,10 +66,9 @@ class OCRSystem(hub.Module): ...@@ -99,10 +66,9 @@ class OCRSystem(hub.Module):
images.append(img) images.append(img)
return images return images
def recognize_text(self, def predict(self,
images=[], images=[],
paths=[], paths=[],
det_max_side_len=960,
draw_img_save='ocr_result', draw_img_save='ocr_result',
visualization=False, visualization=False,
text_thresh=0.5): text_thresh=0.5):
...@@ -111,11 +77,8 @@ class OCRSystem(hub.Module): ...@@ -111,11 +77,8 @@ class OCRSystem(hub.Module):
Args: Args:
images (list(numpy.ndarray)): images data, shape of each is [H, W, C]. If images not paths images (list(numpy.ndarray)): images data, shape of each is [H, W, C]. If images not paths
paths (list[str]): The paths of images. If paths not images paths (list[str]): The paths of images. If paths not images
use_gpu (bool): Whether to use gpu. draw_img_save (str): The directory to store output images.
batch_size(int): the program deals once with one
output_dir (str): The directory to store output images.
visualization (bool): Whether to save image or not. visualization (bool): Whether to save image or not.
box_thresh(float): the threshold of the detected text box's confidence
text_thresh(float): the threshold of the recognize chinese texts' confidence text_thresh(float): the threshold of the recognize chinese texts' confidence
Returns: Returns:
res (list): The result of chinese texts and save path of images. res (list): The result of chinese texts and save path of images.
...@@ -130,8 +93,6 @@ class OCRSystem(hub.Module): ...@@ -130,8 +93,6 @@ class OCRSystem(hub.Module):
assert predicted_data != [], "There is not any image to be predicted. Please check the input data." assert predicted_data != [], "There is not any image to be predicted. Please check the input data."
self.config.det_max_side_len = det_max_side_len
text_sys = TextSystem(self.config)
cnt = 0 cnt = 0
all_results = [] all_results = []
for img in predicted_data: for img in predicted_data:
...@@ -142,7 +103,7 @@ class OCRSystem(hub.Module): ...@@ -142,7 +103,7 @@ class OCRSystem(hub.Module):
all_results.append(result) all_results.append(result)
continue continue
starttime = time.time() starttime = time.time()
dt_boxes, rec_res = text_sys(img) dt_boxes, rec_res = self.text_sys(img)
elapse = time.time() - starttime elapse = time.time() - starttime
cnt += 1 cnt += 1
print("Predict time of image %d: %.3fs" % (cnt, elapse)) print("Predict time of image %d: %.3fs" % (cnt, elapse))
...@@ -187,7 +148,7 @@ class OCRSystem(hub.Module): ...@@ -187,7 +148,7 @@ class OCRSystem(hub.Module):
Run as a service. Run as a service.
""" """
images_decode = [base64_to_cv2(image) for image in images] images_decode = [base64_to_cv2(image) for image in images]
results = self.recognize_text(images_decode, **kwargs) results = self.predict(images_decode, **kwargs)
return results return results
...@@ -197,5 +158,5 @@ if __name__ == '__main__': ...@@ -197,5 +158,5 @@ if __name__ == '__main__':
'./doc/imgs/11.jpg', './doc/imgs/11.jpg',
'./doc/imgs/12.jpg', './doc/imgs/12.jpg',
] ]
res = ocr.recognize_text(paths=image_path, visualization=True) res = ocr.predict(paths=image_path, visualization=False)
print(res) print(res)
\ No newline at end of file
# -*- coding:utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
class Config(object):
pass
def read_params():
cfg = Config()
#params for text detector
cfg.det_algorithm = "DB"
cfg.det_model_dir = "./inference/ch_det_mv3_db/"
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_model_dir = "./inference/ch_rec_mv3_crnn/"
cfg.rec_image_shape = "3, 32, 320"
cfg.rec_char_type = 'ch'
cfg.rec_batch_num = 30
cfg.rec_char_dict_path = "./ppocr/utils/ppocr_keys_v1.txt"
cfg.use_space_char = True
return cfg
\ No newline at end of file
# 服务部署 # 服务部署
PaddleOCR提供2种服务部署方式: PaddleOCR提供2种服务部署方式:
- 基于HubServing的部署:已集成到PaddleOCR中([code](https://github.com/PaddlePaddle/PaddleOCR/tree/develop/deploy/ocr_hubserving)),按照本教程使用; - 基于HubServing的部署:已集成到PaddleOCR中([code](https://github.com/PaddlePaddle/PaddleOCR/tree/develop/deploy/hubserving)),按照本教程使用;
- 基于PaddleServing的部署:详见PaddleServing官网[demo](https://github.com/PaddlePaddle/Serving/tree/develop/python/examples/ocr),后续也将集成到PaddleOCR。 - 基于PaddleServing的部署:详见PaddleServing官网[demo](https://github.com/PaddlePaddle/Serving/tree/develop/python/examples/ocr),后续也将集成到PaddleOCR。
服务部署目录下包括检测、识别、2阶段串联三种服务包,根据需求选择相应的服务包进行安装和启动。目录如下: 服务部署目录下包括检测、识别、2阶段串联三种服务包,根据需求选择相应的服务包进行安装和启动。目录如下:
...@@ -15,12 +15,13 @@ deploy/hubserving/ ...@@ -15,12 +15,13 @@ deploy/hubserving/
每个服务包下包含3个文件。以2阶段串联服务包为例,目录如下: 每个服务包下包含3个文件。以2阶段串联服务包为例,目录如下:
``` ```
deploy/hubserving/ocr_system/ deploy/hubserving/ocr_system/
└─ __init__.py 空文件 └─ __init__.py 空文件,必选
└─ config.json 配置文件,启动服务时作为参数传入 └─ config.json 配置文件,可选,使用配置启动服务时作为参数传入
└─ module.py 主模块,包含服务的完整逻辑 └─ module.py 主模块,必选,包含服务的完整逻辑
└─ params.py 参数文件,必选,包含模型路径、前后处理参数等参数
``` ```
## 启动服务 ## 快速启动服务
以下步骤以检测+识别2阶段串联服务为例,如果只需要检测服务或识别服务,替换相应文件路径即可。 以下步骤以检测+识别2阶段串联服务为例,如果只需要检测服务或识别服务,替换相应文件路径即可。
### 1. 安装paddlehub ### 1. 安装paddlehub
```pip3 install paddlehub --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple``` ```pip3 install paddlehub --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple```
...@@ -31,39 +32,71 @@ PaddleOCR提供3种服务模块,根据需要安装所需模块。如: ...@@ -31,39 +32,71 @@ PaddleOCR提供3种服务模块,根据需要安装所需模块。如:
安装检测服务模块: 安装检测服务模块:
```hub install deploy/hubserving/ocr_det/``` ```hub install deploy/hubserving/ocr_det/```
或,安装识别服务模块: 或,安装识别服务模块:
```hub install deploy/hubserving/ocr_rec/``` ```hub install deploy/hubserving/ocr_rec/```
或,安装检测+识别串联服务模块: 或,安装检测+识别串联服务模块:
```hub install deploy/hubserving/ocr_system/``` ```hub install deploy/hubserving/ocr_system/```
### 3. 修改配置文件 ### 3. 启动服务
在config.json中指定模型路径、是否使用GPU、是否对结果做可视化等参数,如,串联服务ocr_system的配置: #### 方式1. 命令行命令启动(仅支持CPU)
**启动命令:**
```shell
$ hub serving start --modules [Module1==Version1, Module2==Version2, ...] \
--port XXXX \
--use_multiprocess \
--workers \
```
**参数:**
|参数|用途|
|-|-|
|--modules/-m|PaddleHub Serving预安装模型,以多个Module==Version键值对的形式列出<br>*`当不指定Version时,默认选择最新版本`*|
|--port/-p|服务端口,默认为8866|
|--use_multiprocess|是否启用并发方式,默认为单进程方式,推荐多核CPU机器使用此方式<br>*`Windows操作系统只支持单进程方式`*|
|--workers|在并发方式下指定的并发任务数,默认为`2*cpu_count-1`,其中`cpu_count`为CPU核数|
如启动串联服务: ```hub serving start -m ocr_system```
这样就完成了一个服务化API的部署,使用默认端口号8866。
#### 方式2. 配置文件启动(支持CPU、GPU)
**启动命令:**
```hub serving start --config/-c config.json```
其中,`config.json`格式如下:
```python ```python
{ {
"modules_info": { "modules_info": {
"ocr_system": { "ocr_system": {
"init_args": { "init_args": {
"version": "1.0.0", "version": "1.0.0",
"det_model_dir": "./inference/det/",
"rec_model_dir": "./inference/rec/",
"use_gpu": true "use_gpu": true
}, },
"predict_args": { "predict_args": {
"visualization": false "visualization": false
} }
} }
} },
"port": 8868,
"use_multiprocess": false,
"workers": 2
} }
``` ```
其中,模型路径对应的模型为```inference模型```。
### 4. 运行启动命令 - `init_args`中的可配参数与`module.py`中的`_initialize`函数接口一致。其中,**当`use_gpu`为`true`时,表示使用GPU启动服务**。
```hub serving start -m ocr_system --config hubserving/ocr_det/config.json``` - `predict_args`中的可配参数与`module.py`中的`predict`函数接口一致。
这样就完成了一个服务化API的部署,默认端口号为8866。 **注意:**
- 使用配置文件启动服务时,其他参数会被忽略。
- 如果使用GPU预测(即,`use_gpu`置为`true`),则需要在启动服务之前,设置CUDA_VISIBLE_DEVICES环境变量,如:```export CUDA_VISIBLE_DEVICES=0```,否则不用设置。
**NOTE:** 如使用GPU预测(即,config中use_gpu置为true),则需要在启动服务之前,设置CUDA_VISIBLE_DEVICES环境变量,如:```export CUDA_VISIBLE_DEVICES=0```,否则不用设置。 如,使用GPU 3号卡启动串联服务:
```shell
export CUDA_VISIBLE_DEVICES=3
hub serving start -c deploy/hubserving/ocr_system/config.json
```
## 发送预测请求 ## 发送预测请求
配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果: 配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果:
...@@ -89,21 +122,25 @@ r = requests.post(url=url, headers=headers, data=json.dumps(data)) ...@@ -89,21 +122,25 @@ r = requests.post(url=url, headers=headers, data=json.dumps(data))
print(r.json()["results"]) print(r.json()["results"])
``` ```
你可能需要根据实际情况修改```url```字符串中的端口号和服务模块名称。 你可能需要根据实际情况修改`url`字符串中的端口号和服务模块名称。
上面所示代码都已写入测试脚本,可直接运行命令:```python tools/test_hubserving.py``` 上面所示代码都已写入测试脚本,可直接运行命令:```python tools/test_hubserving.py```
## 自定义修改服务模块 ## 自定义修改服务模块
如果需要修改服务逻辑,你一般需要操作以下步骤: 如果需要修改服务逻辑,你一般需要操作以下步骤(以修改`ocr_system`为例)
1、 停止服务 - 1、 停止服务
```hub serving stop -m ocr_system``` ```hub serving stop --port/-p XXXX```
2、 到相应的module.py文件中根据实际需求修改代码 - 2、 到相应的`module.py`和`params.py`等文件中根据实际需求修改代码。
例如,如果需要替换部署服务所用模型,则需要到`params.py`中修改模型路径参数`det_model_dir`和`rec_model_dir`,当然,同时可能还需要修改其他相关参数,请根据实际情况修改调试。 建议修改后先直接运行`module.py`调试,能正确运行预测后再启动服务测试。
3、 卸载旧服务包 - 3、 卸载旧服务包
```hub uninstall ocr_system``` ```hub uninstall ocr_system```
4、 安装修改后的新服务包 - 4、 安装修改后的新服务包
```hub install deploy/hubserving/ocr_system/``` ```hub install deploy/hubserving/ocr_system/```
- 5、重新启动服务
```hub serving start -m ocr_system```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册