docker部署PaddleOCR_paddleocr 镜像(1)

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 "";
}

public static String fileToBase64(String path) {
    // 读取图片字节数组
    try (InputStream in = Files.newInputStream(Paths.get(path))){
        byte[] data = new byte[in.available()];
        in.read(data);
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);// 返回Base64编码过的字节数组字符串
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

}


## 5. 遇到的问题


1. 报 protobuf 包依赖冲突,后在Dockerfile 文件里后面加了这行,安装个低版本的 protobuf 解决



RUN pip install protobuf==3.20.0 -i https://mirror.baidu.com/pypi/simple


2. PaddlePaddle出现“非法指令”或“illegal instruction”



> 
> 原因:PaddlePaddle使用avx SIMD指令提高cpu执行效率,因此错误的使用二进制发行版可能会导致这种错误,请先判断你的电脑是否支持AVX指令集,再选择性的安装支持AVX指令集的PaddlePaddle还是不支持AVX指令集的PaddlePaddle,或者使用Docker镜像来安装最新版本的PaddlePaddle,Docker镜像中的PaddlePaddle默认支持是支持AVX指令集的,可以提高cpu的执行效率。在启动的时候会检查系统是否支持PaddlePaddle初始化时所需要的资源,从AVX指令集开始检查。  
>  解决:遇到此问题可以先用 lscpu 命令查看docker宿主机是否支持AVX指令集,我是换台服务器解决。
> 
> 
> 


3. 容器运行后,调用的时候报了 module ‘numpy’ has no attribute ‘int’. 异常



AttributeError: module ‘numpy’ has no attribute ‘int’.
np.int was a deprecated alias for the builtin int. To avoid this error in existing code, use int by itself. Doing this will not modify any behavior and is safe. When replacing np.int, you may wish to use e.g. np.int64 or np.int32 to specify the precision. If you wish to review your current use, check the release note link for additional information.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:



> 
> 原因:np.int 在 NumPy 1.20 中已弃用,在 NumPy 1.24 中已删除。  
>  解决:将容器中的 /PaddleOCR/deploy/hubserving/ocr\_system/module.py  
 **自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

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

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

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

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

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

![img](https://img-blog.csdnimg.cn/img_convert/939c28ba6e5e67fa9c8732de4bc43432.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/9deb531c3a70b8cff434122883784672.jpeg)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值