文章

Paddle服务端Ocr部署,客户端选择复制

服务端Docker安装Paddle Ocr部署

第一步安装paddlex

详细链接:https://paddlepaddle.github.io/PaddleX/latest/installation/installation.html#21-dockerpaddlex

命令如下:

本命令在原命令进行优化,暴露端口8080,禁止与主机共享,后台运行。

dockerrun -d --namepaddlex-server  -v$PWD:/paddle--shm-size=8g-p 8076:8080 -itccr-2vdh3abv-pub.cnc.bj.baidubce.com/paddlex/paddlex:paddlex3.0.0b2-paddlepaddle3.0.0b2-cpu/bin/bash
docker exec -it paddlex-server /bin/bash

第二步安装通用OCR

paddlex --get_pipeline_config OCR

第三步服务化部署

详细链接:https://paddlepaddle.github.io/PaddleX/latest/pipeline_deploy/service_deploy.html

执行命令:

paddlex --serve --pipeline OCR.yaml

第四步Java访问

test图片示例:


import okhttp3.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;

public class Main {
    public static void main(String[] args) throws IOException {
        
        String API_URL = "http://localhost:8080/ocr"; // 服务URL
        String imagePath = "./test.jpg"; // 本地图像
        String outputImagePath = "./out.jpg"; // 输出图像

        // 对本地图像进行Base64编码
        File file = new File(imagePath);
        byte[] fileContent = java.nio.file.Files.readAllBytes(file.toPath());
        String imageData = Base64.getEncoder().encodeToString(fileContent);

        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode params = objectMapper.createObjectNode();
        params.put("image", imageData); // Base64编码的文件内容或者图像URL

        // 创建 OkHttpClient 实例
        OkHttpClient client = new OkHttpClient();
        MediaType JSON = MediaType.Companion.get("application/json; charset=utf-8");
        RequestBody body = RequestBody.Companion.create(params.toString(), JSON);
        Request request = new Request.Builder()
                .url(API_URL)
                .post(body)
                .build();

        // 调用API并处理接口返回数据
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                String responseBody = response.body().string();
                JsonNode resultNode = objectMapper.readTree(responseBody);
                JsonNode result = resultNode.get("result");
                String base64Image = result.get("image").asText();
                JsonNode texts = result.get("texts");

                byte[] imageBytes = Base64.getDecoder().decode(base64Image);
                try (FileOutputStream fos = new FileOutputStream(outputImagePath)) {
                    fos.write(imageBytes);
                }
                System.out.println("Output image saved at " + outputImagePath);
                System.out.println("\nDetected texts: " + texts.toString());
            } else {
                System.err.println("Request failed with code: " + response.code());
            }
        }
    }
}

前端copy搭建

项目地址:https://github.com/boommanpro/paddle-copy

示例地址:https://boommanpro.github.io/paddle-copy/