Skip to content

智能文本提取

注意:在学习使用不同函数之前,建议先阅读请求描述,了解基本的PDF处理流程。使用不同函数时,可以在上传文件时设置各自的特殊参数。其他基本步骤一致。

智能文本提取:

java
{    
  "lang": "auto",  
}

所需参数

lang:支持的类型和定义

  • auto - 自动分类语言
  • english - 英语
  • chinese - 简体中文
  • chinese_tra - 繁体中文
  • korean - 韩语
  • japanese - 日语
  • latin - 拉丁语
  • devanagari - 梵文字母

示例

  1. 授权

    您需要将认证响应中的 替换为从控制台获取的 publicKeysecretKey,并使用 accessToken

    curl
    curl --location --request POST 'https://api-server.compdf.com/server/v1/oauth/token' \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "publicKey": "publicKey",
        "secretKey": "secretKey"
    }'
    java
     import java.io.*;
     import okhttp3.*;
     public class main {
       public static void main(String []args) throws IOException{
         OkHttpClient client = new OkHttpClient().newBuilder()
           .build();
         MediaType mediaType = MediaType.parse("text/plain");
         RequestBody body = RequestBody.create(mediaType, "{\n    \"publicKey\": \"{{public_key}}\",\n    \"secretKey\": \"{{secret_key}}\"\n}");
         Request request = new Request.Builder()
           .url("https://api-server.compdf.com/server/v1/oauth/token")
           .method("POST", body)
           .build();
         Response response = client.newCall(request).execute();
       }
     }
  2. 创建任务

    您需要将 替换为上一步获取的 accessToken,将 ***替换为所需的界面和任务错误消息语言类型。请求成功后,您将从响应中收到 taskId

    curl
    curl --location --request GET 'https://api-server.compdf.com/server/v1/task/documentAI/ocr' \
    --header 'Authorization: Bearer accessToken'
    java
     import java.io.*;
     import okhttp3.*;
     public class main {
       public static void main(String []args) throws IOException{
         OkHttpClient client = new OkHttpClient().newBuilder()
           .build();
         MediaType mediaType = MediaType.parse("text/plain");
         RequestBody body = RequestBody.create(mediaType, "");
         Request request = new Request.Builder()
           .url("https://api-server.compdf.com/server/v1/task/documentAI/ocr?language={{language}}")
           .method("GET", body)
           .addHeader("Authorization", "Bearer {{accessToken}}")
           .build();
         Response response = client.newCall(request).execute();
       }
     }
  3. 上传文件

    替换为您要转换的文件,将 替换为您上一步获取到的 taskId,将 替换为您需要的界面错误信息语言类型,将 替换为您第一步获取到的 accessToken

    -支持图像格式: jpg,jpeg,png,bmp

    curl
    curl --location --request POST 'https://api-server.compdf.com/server/v1/file/upload' \
    --header 'Authorization: Bearer accessToken' \
    --form 'file=@"test.png"' \
    --form 'taskId="taskId"' \
    --form 'password=""' \
    --form 'parameter="{    \"lang\": \"auto\"    }"' \
    java
     import java.io.*;
     import okhttp3.*;
     public class main {
       public static void main(String []args) throws IOException{
         OkHttpClient client = new OkHttpClient().newBuilder()
           .build();
         MediaType mediaType = MediaType.parse("text/plain");
         RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
           .addFormDataPart("file","{{file}}",
                            RequestBody.create(MediaType.parse("application/octet-stream"),
                                               new File("<file>")))
           .addFormDataPart("taskId","{{taskId}}")
           .addFormDataPart("language","{{language}}")
           .addFormDataPart("parameter","{    \"lang\": \"auto\"    }")
           .build();
         Request request = new Request.Builder()
           .url("https://api-server.compdf.com/server/v1/file/upload")
           .method("POST", body)
           .addHeader("Authorization", "Bearer {{accessToken}}")
           .build();
         Response response = client.newCall(request).execute();
       }
     }
  4. 执行任务

    替换为 创建任务 步骤中获取的 taskId,将 替换为第一步获取的 access_token,将 替换为所需的界面错误信息语言类型。

    curl
    curl --location -g --request GET 'https://api-server.compdf.com/server/v1/execute/start?taskId=taskId' \
    --header 'Authorization: Bearer accessToken'
    java
     import java.io.*;
     import okhttp3.*;
     public class main {
      public static void main(String []args) throws IOException{
        OkHttpClient client = new OkHttpClient().newBuilder()
          .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = RequestBody.create(mediaType, "");
        Request request = new Request.Builder()
          .url("https://api-server.compdf.com/server/v1/execute/start?taskId={{taskId}}&language={{language}}")
          .method("GET", body)
          .addHeader("Authorization", "Bearer {{accessToken}}")
          .build();
        Response response = client.newCall(request).execute();
      }
     }
  5. 获取任务信息

    替换为 创建任务 步骤中获取的 taskId,将 替换为第一步中获取的 access_token

    curl
    curl --location -g --request GET 'https://api-server.compdf.com/server/v1/task/taskInfo?taskId=taskId' \
    --header 'Authorization: Bearer accessToken'
    java
     import java.io.*;
     import okhttp3.*;
     public class main {
       public static void main(String []args) throws IOException{
         OkHttpClient client = new OkHttpClient().newBuilder()
           .build();
         MediaType mediaType = MediaType.parse("text/plain");
         RequestBody body = RequestBody.create(mediaType, "");
         Request request = new Request.Builder()
           .url("https://api-server.compdf.com/server/v1/task/taskInfo?taskId={{taskId}}")
           .method("GET", body)
           .addHeader("Authorization", "Bearer {{accessToken}}")
           .build();
         Response response = client.newCall(request).execute();
       }
     }

结果

文件类型说明
.JSONOCR 识别结果

内容

参数说明
costOCR 识别时间
boxes输入图片所有检测到的物体框位置
textOCR 识别内容
rec_scoresOCR 文本识别分数,分数越高,结果越可信
java
{
        "cost": 149,
        "boxes": [
            [
                150,
                71,
                198,
                71,
                198,
                110,
                150,
                110
            ],
            [
                74,
                117,
                274,
                120,
                273,
                166,
                73,
                163
            ],
            [
                99,
                179,
                249,
                182,
                249,
                208,
                99,
                205
            ],
            [
                65,
                203,
                276,
                205,
                276,
                230,
                65,
                228
            ]
        ],
        "text": [
            "EPPING",
            "Twinned with",
            "Eppingen,Germany"
        ],
        "rec_scores": [
            0.46275457739830017,
            0.9971449971199036,
            0.9649983048439026,
            0.9587073922157288
        ]
}