docker部署PaddleOCR_paddleocr 镜像

用 docker 部署 PaddleOCR 是因为 PaddleOCR 以源码安装的方式比较繁杂,要注意比较多的细节,而且很多环境往往是没有外网的,因此Docker就是一个很好的解决方案,它将开放所需要的环境都封装在镜像中了,方便部署使用。

1. PaddleOCR 镜像制作

Dockerfile文件

# Version: 2.5.1
# FROM paddlepaddle/paddle:2.5.1
FROM registry.baidubce.com/paddlepaddle/paddle:2.5.1
# PaddleOCR base on Python3.7
RUN pip3.7 install --no-cache-dir --upgrade pip -i https://mirror.baidu.com/pypi/simple
RUN pip3.7 install --no-cache-dir paddlehub --upgrade -i https://mirror.baidu.com/pypi/simple
RUN git clone https://gitee.com/PaddlePaddle/PaddleOCR.git /PaddleOCR
WORKDIR /PaddleOCR
RUN pip3.7 install --no-cache-dir -r requirements.txt -i https://mirror.baidu.com/pypi/simple
RUN mkdir -p /PaddleOCR/inference/
# 下载并解压 OCR 文本检测模型
ADD https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar /PaddleOCR/inference/
# 下载并解压 OCR 文本方向分类模型
ADD https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar /PaddleOCR/inference/
# 下载并解压 OCR 文本识别模型
ADD https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar /PaddleOCR/inference/
RUN tar xf /PaddleOCR/inference/ch_PP-OCRv3_det_infer.tar -C /PaddleOCR/inference/
RUN tar xf /PaddleOCR/inference/ch_ppocr_mobile_v2.0_cls_infer.tar -C /PaddleOCR/inference/
RUN tar xf /PaddleOCR/inference/ch_PP-OCRv3_rec_infer.tar -C /PaddleOCR/inference/
RUN pip3.7 uninstall -y astroid
RUN pip3.7 install astroid==2.12.2 --upgrade pip -i https://mirror.baidu.com/pypi/simple
RUN pip3.7 uninstall protobuf
RUN pip3.7 install protobuf==3.20.0 --upgrade pip -i https://mirror.baidu.com/pypi/simple
# 安装ocr\_system服务
RUN hub install deploy/hubserving/ocr_system/

# 安装表格识别,下载基于SLANet的中文表格识别模型,如果只需要文本提取,则可以不安装该服务,删掉这一块即可
ADD https://paddleocr.bj.bcebos.com/ppstructure/models/slanet/ch_ppstructure_mobile_v2.0_SLANet_infer.tar /PaddleOCR/inference/
ADD https://paddleocr.bj.bcebos.com/dygraph_v2.0/table/en_ppocr_mobile_v2.0_table_structure_infer.tar /PaddleOCR/inference/
RUN tar xf /PaddleOCR/inference/ch_ppstructure_mobile_v2.0_SLANet_infer.tar -C /PaddleOCR/inference/
RUN tar xf /PaddleOCR/inference/en_ppocr_mobile_v2.0_table_structure_infer.tar -C /PaddleOCR/inference/
#安装structure\_table服务
RUN hub install deploy/hubserving/structure_table/

EXPOSE 8866
CMD ["/bin/bash","-c","hub serving start -m ocr\_system structure\_table"]

创建好Dockerfile文件后,执行如下命令即可自动构建镜像,要预留足够的存储空间,构建完成后大概6G多,整个构建过程根据网速定,我花了差不多1.5小时才构建完。

docker build -t paddle-ocr:cpu .

2. 运行

docker run -dp 8866:8866 --name ocr paddle-ocr:cpu

当然也可以用 docker-compose 管理

version: '3'
services:
  ocr:
    image: paddle-ocr:cpu
    restart: always
    container_name: ocr
    ports:
      - 8866:8866

3. 服务配置

镜像运行后,使用 docker exec -it ocr bash 进入容器内进行修改,修改后重新容器即可。

  1. 文本检测+文本方向分类+文本识别3阶段串联服务(ocr_system)配置文件是deploy/hubserving/ocr_system/params.py,包含模型路径和相关参数,这里使用默认配置即可,如果更换模型需要对应修改配置文件。
  2. 表格识别服务(structure_table)配置.
#打开配置文件
vim deploy/hubserving/structure_table/params.py

#调整模型文件路径为./inference/ch\_ppstructure\_mobile\_v2.0\_SLANet\_infer/
#调整字典文件路径为./ppocr/utils/dict/table\_structure\_dict\_ch.txt

4. 测试调用

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.14</version>
        </dependency>

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.ParseException;
public class HttpUtils {
    //接口地址
    //private static final String apiURL = "http://192.168.0.101:8866/predict/structure\_table";
    private static final String apiURL = "http://192.168.0.101:8866/predict/ocr\_system";

    public static void main(String[] args) throws ParseException {
        JSONArray arry = new JSONArray();
        JSONObject param = new JSONObject();
        arry.add(fileToBase64("C:\\Users\\Aaron\\Pictures\\2.PNG"));
        param.put("images",arry);
        String result = HttpUtils.post(apiURL, param.toJSONString());
        System.out.println(result);
    }

    /**
     * 调用 API
     */
    public static String post(String url, String parameters) {
        HttpPost post = new HttpPost(url);
        // 建立一个NameValuePair数组,用于存储欲传送的参数
        post.addHeader("Content-type","application/json");
        post.setHeader("Accept", "application/json");
        post.setEntity(new StringEntity(parameters, StandardCharsets.UTF_8));
        long startTime = System.currentTimeMillis();
        try (CloseableHttpClient httpClient = HttpClients.createDefault()){
            HttpResponse response = httpClient.execute(post);
            long endTime = System.currentTimeMillis();
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("statusCode:" + statusCode);
            System.out.println("调用API 花费时间(单位:秒):" + (endTime - startTime)/1000);
            if (statusCode != HttpStatus.SC_OK) {
                System.out.println("Method failed:" + response.getStatusLine());
            }
            String body = EntityUtils.toString(response.getEntity(),"utf-8");
            return body;
        } catch (IOException e) {
            // 网络错误
            e.printStackTrace();
        }
        return "";
    }
 **自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**

![img](https://img-blog.csdnimg.cn/img_convert/8660eec8fd63106a6d08b7deb334404d.png)

![img](https://img-blog.csdnimg.cn/img_convert/9a3655f8c75cefb340d5b45a62b43b5b.png)

![img](https://img-blog.csdnimg.cn/img_convert/5942d6db8b84a68965979fb6ebdf17ae.png)

![img](https://img-blog.csdnimg.cn/img_convert/6f02990d33892498e71a959f27d788bc.png)

![img](https://img-blog.csdnimg.cn/img_convert/6c361282296f86381401c05e862fe4e9.png)

![img](https://img-blog.csdnimg.cn/img_convert/9f49b566129f47b8a67243c1008edf79.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)**

f47b8a67243c1008edf79.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)**

![](https://img-blog.csdnimg.cn/img_convert/c37450ef386b1e1110fc63059b89e9c8.jpeg)
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值