栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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键值对的形式列出
*`当不指定Version时,默认选择最新版本`*| |--port/-p|服务端口,默认为8866| |--use_multiprocess|是否启用并发方式,默认为单进程方式,推荐多核CPU机器使用此方式
*`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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/963782.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号