三、OCR-docker部署最新版PaddleServing和PaddleOCR,服务端,客户端,dockerfile部署-hubserving部署,GPU运行

202 篇文章 13 订阅
93 篇文章 1 订阅

1、服务端部署

在这里插入图片描述

1.1、服务端启动参数理解

### 2.4 启动服务
**启动命令:**  
```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

1.2、dockerfile编写

FROM registry.baidubce.com/paddlepaddle/serving:0.9.0-cuda10.1-cudnn7-devel


COPY . /deploy
WORKDIR /deploy

# Install requirements
RUN pip config set global.index-url https://mirror.baidu.com/pypi/simple \
    && python3.7 -m pip install --upgrade setuptools \
    && python3.7 -m pip install --upgrade pip \
    && python3.7 -m pip install paddlepaddle-gpu==2.3.0.post101 \
    -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html \
    && pip3.7 install -r requirements.txt \
    && hub install chinese_ocr_db_crnn_mobile==1.1.2


ENTRYPOINT export CUDA_VISIBLE_DEVICES=0 && hub serving start -m chinese_ocr_db_crnn_mobile


1.3、制作镜像,启动容器指令

nvidia-docker build -t xx/ocr-hub-server-qa:1.0.0.0630 .

nvidia-docker run -p 8866:8866 --gpus all --name ocr-hub-server-qa -d xx/ocr-hub-server-qa:1.0.0.0630

nvidia-docker exec -it ocr-hub-server-qa /bin/bash

docker logs -f ocr-hub-server-qa

1.4、客户端访问案例

# -*- coding: utf-8 -*-
import requests
import json
import cv2
import base64
import time


def cv2_to_base64(image):
    data = cv2.imencode('.jpg', image)[1]
    return base64.b64encode(data.tostring()).decode('utf8')


# 发送HTTP请求
data = {'images': [cv2_to_base64(cv2.imread("./11.jpg"))]}
headers = {"Content-type": "application/json"}
url = "http://127.0.0.1:8866/predict/chinese_ocr_db_crnn_mobile"
time_now = time.time()
r = requests.post(url=url, headers=headers, data=json.dumps(data))
print(time.time() - time_now)
# 打印预测结果
print(r.json()["results"])

1.5、服务器制作镜像,启动容器指令

nvidia-docker build -t xx/ocr-hub-server-qa:1.0.0.0630 .

nvidia-docker run -p 8866:8866 --gpus all --name ocr-hub-server-qa -d xx/ocr-hub-server-qa:1.0.0.0630

nvidia-docker exec -it ocr-hub-server-qa /bin/bash

docker logs -f ocr-hub-server-qa

1.6、安装包

paddlehub==2.2.0

2、客户端部署

在这里插入图片描述

2.1、客户端ocr_client.py

# 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.
# pylint: disable=doc-string-missing

import requests
import json
import cv2
import base64
import re
from config_env.config_envi import common


class ImageOCR(object):
    def __init__(self):
        self.headers = {"Content-type": "application/json"}
        self.url = "http://%s/predict/chinese_ocr_db_crnn_mobile" % common['model_ip_port_server']
        self.index = ['姓名', '性别', '民族', '出生', '住址', '公民身份号码', '$']

    def cv2_to_base64(self, image):
        data = cv2.imencode('.jpg', image)[1]
        return base64.b64encode(data.tostring()).decode('utf8')

    def ocr_image(self, img_path):
        data = {'images': [self.cv2_to_base64(cv2.imread(img_path))]}
        r = requests.post(url=self.url, headers=self.headers, data=json.dumps(data))
        res = r.json()["results"]
        res_list = []
        for txt in res[0]['data']:
            res_list.append(txt['text'])
        res_str = ''.join(res_list)
        res_dict = {}
        for i in range(len(self.index) - 1):
            re_res = re.search(r'%s(.*?)%s' % (self.index[i], self.index[i + 1]), res_str)
            # print(re_res)
            res_dict[self.index[i]] = re.sub(r'[\s。]', '', re_res.group(1)) if re_res else ''
        return res_dict


if __name__ == '__main__':
    image_ocr = ImageOCR()
    test_img_path_ = "./11.jpg"
    print(image_ocr.ocr_image(test_img_path_))


2.2、测试案例request_post.py

# -*- coding: utf-8 -*-
import os
import requests
from config_env.config_envi import common


def ocr_Paddle(root, name):
    # root = 'E:\\png'
    # name = '2.png'
    file = os.path.join(root, name)
    files_t = {'file': (name, open(file, 'rb'))}
    headers = {'File-Name': name}

    r = requests.post("http://%s/nlp/v1/ocr/ocr_res" % common['model_ip_port_client'], files=files_t, headers=headers)

    print(eval(r.text))
    return eval(r.text)


if __name__ == '__main__':
    ocr_Paddle('./', '11.jpg')

2.3、服务器制作镜像,启动容器指令

nvidia-docker build -t xx/ocr-hub-client-qa:1.0.0.0630 .

nvidia-docker run -p 9539:9539 --name ocr-hub-client-qa -d xx/ocr-hub-client-qa:1.0.0.0630

nvidia-docker exec -it ocr-hub-client-qa /bin/bash

2.4、安装包

paddle-serving-client==0.9.0
paddle-serving-app==0.9.0
paddle-serving-server-gpu==0.9.0.post101
Flask-RESTful==0.3.9
Flask==1.1.4
flask_cors==3.0.10
prometheus-flask-exporter==0.18.7
gunicorn==20.1.0
gevent==21.12.0
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值