PP-OCRv5
更新时间:2026-04-07
接口描述
PP-OCRv5是PP-OCR系列最新一代的文字识别解决方案,专为多场景、多文字类型的识别任务设计。相比前代版本,PP-OCRv5在文字类型支持和应用场景适应性方面实现了全面升级。该方案不仅能够返回文本行的坐标信息,还可输出对应文本内容及其置信度,有效提升了文字检测与识别的准确性和实用性。
本接口提供PP-OCRv5标准化API服务,支持开箱即用、免部署的快捷接入方式,助力实现多场景文字识别。
在线调试
您可以在 示例代码中心 中调试该接口,可进行签名验证、查看在线调用的请求内容和返回结果、示例代码的自动生成。
请求说明
请求示例
HTTP 方法:POST
请求URL: https://aip.baidubce.com/rest/2.0/ocr/v1/pp_ocrv5
URL参数:
| 参数 | 值 |
|---|---|
| access_token | 通过API Key和Secret Key获取的access_token,参考“Access Token获取” |
Header如下:
| 参数 | 值 |
|---|---|
| Content-Type | application/x-www-form-urlencoded |
Body中放置请求参数,参数详情如下:
请求参数
| 参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
|---|---|---|---|---|
| image | 和 url/pdf_file 三选一 | string | - | 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过10M,最短边至少15px,最长边最大8192px,支持jpg/jpeg/png/bmp格式 优先级:image > url > pdf_file > ofd_file ,当image字段存在时,url、pdf_file字段失效 |
| url | 和 image/pdf_file 三选一 | string | - | 图片完整url,url长度不超过1024字节,url对应的图片base64编码后大小不超过10M,最短边至少15px,最长边最大8192px,支持jpg/jpeg/png/bmp格式优先级:image > url > pdf_file ,当image字段存在时,url字段失效请注意关闭URL防盗链 |
| pdf_file | 和 image/url 三选一 | string | - | PDF文件,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过10M,最短边至少15px,最长边最大8192px优先级:image > url > pdf_file,当image、url字段存在时,pdf_file字段失效 |
| useDocOrientationClassify | 否 | boolean | - | 是否在推理时使用文档方向分类模块,开启后,可以自动识别并矫正 0°、90°、180°、270°的图片,默认初始化为 False |
| useDocUnwarping | 否 | boolean | - | 是否在推理时使用文本图像矫正模块,开启后,可以自动矫正扭曲图片,例如褶皱、倾斜等情况,默认初始化为 False |
| useTextlineOrientation | 否 | boolean | - | 是否在推理时使用文本行方向分类模块,开启后,可以自动识别和矫正 0° 和 180° 的文本行,默认初始化为 False |
请求代码示例
提示一:使用示例代码前,请记得替换其中的示例Token、图片地址或Base64信息。
提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。
1curl -i -k 'https://aip.baidubce.com/rest/2.0/ocr/v1/pp_ocrv5?access_token=【调用鉴权接口获取的token】'
2 -H 'Content-Type: application/x-www-form-urlencoded'
3--data-urlencode 'useDocOrientationClassify=true' \
4--data-urlencode 'useDocUnwarping=true' \
5--data-urlencode 'useTextlineOrientation=true' \
6--data-urlencode 'image=【图片Base64编码,需UrlEncode】'
1# encoding:utf-8
2
3import requests
4import base64
5
6'''
7PPOCR-v5
8'''
9
10request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/pp_ocrv5"
11# 二进制方式打开图片文件
12f = open('[本地文件]', 'rb')
13img = base64.b64encode(f.read())
14
15params = {"image":img}
16access_token = '[调用鉴权接口获取的token]'
17request_url = request_url + "?access_token=" + access_token
18headers = {'content-type': 'application/x-www-form-urlencoded'}
19response = requests.post(request_url, data=params, headers=headers)
20if response:
21 print (response.json())
1package com.baidu.ai.aip;
2
3import com.baidu.ai.aip.utils.Base64Util;
4import com.baidu.ai.aip.utils.FileUtil;
5import com.baidu.ai.aip.utils.HttpUtil;
6
7import java.net.URLEncoder;
8
9/**
10* PPOCR-v5
11*/
12public class Ppocrv5 {
13
14 /**
15 * 重要提示代码中所需工具类
16 * FileUtil,Base64Util,HttpUtil,GsonUtils请从
17 * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
18 * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
19 * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
20 * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
21 * 下载
22 */
23 public static String ppocrv5() {
24 // 请求url
25 String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/pp_ocrv5";
26 try {
27 // 本地文件路径
28 String filePath = "[本地文件路径]";
29 byte[] imgData = FileUtil.readFileByBytes(filePath);
30 String imgStr = Base64Util.encode(imgData);
31 String imgParam = URLEncoder.encode(imgStr, "UTF-8");
32
33 String param = "image=" + imgParam;
34
35 // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
36 String accessToken = "[调用鉴权接口获取的token]";
37
38 String result = HttpUtil.post(url, accessToken, param);
39 System.out.println(result);
40 return result;
41 } catch (Exception e) {
42 e.printStackTrace();
43 }
44 return null;
45 }
46
47 public static void main(String[] args) {
48 Ppocrv5.ppocrv5();
49 }
50}
1#include <iostream>
2#include <curl/curl.h>
3
4// libcurl库下载链接:https://curl.haxx.se/download.html
5// jsoncpp库下载链接:https://github.com/open-source-parsers/jsoncpp/
6const static std::string request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/pp_ocrv5";
7static std::string ppocrv5_result;
8/**
9* curl发送http请求调用的回调函数,回调函数中对返回的json格式的body进行了解析,解析结果储存在全局的静态变量当中
10* @param 参数定义见libcurl文档
11* @return 返回值定义见libcurl文档
12*/
13static size_t callback(void *ptr, size_t size, size_t nmemb, void *stream) {
14 // 获取到的body存放在ptr中,先将其转换为string格式
15 ppocrv5_result = std::string((char *) ptr, size * nmemb);
16 return size * nmemb;
17}
18/**
19* PPOCR-v5
20* @return 调用成功返回0,发生错误返回其他错误码
21*/
22int ppocrv5(std::string &json_result, const std::string &access_token) {
23 std::string url = request_url + "?access_token=" + access_token;
24 CURL *curl = NULL;
25 CURLcode result_code;
26 int is_success;
27 curl = curl_easy_init();
28 if (curl) {
29 curl_easy_setopt(curl, CURLOPT_URL, url.data());
30 curl_easy_setopt(curl, CURLOPT_POST, 1);
31 curl_httppost *post = NULL;
32 curl_httppost *last = NULL;
33 curl_formadd(&post, &last, CURLFORM_COPYNAME, "image", CURLFORM_COPYCONTENTS, "【base64_img】", CURLFORM_END);
34
35 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
36 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
37 result_code = curl_easy_perform(curl);
38 if (result_code != CURLE_OK) {
39 fprintf(stderr, "curl_easy_perform() failed: %s\n",
40 curl_easy_strerror(result_code));
41 is_success = 1;
42 return is_success;
43 }
44 json_result = ppocrv5_result;
45 curl_easy_cleanup(curl);
46 is_success = 0;
47 } else {
48 fprintf(stderr, "curl_easy_init() failed.");
49 is_success = 1;
50 }
51 return is_success;
52}
返回说明
返回参数
| 字段 | 是否必选 | 类型 | 说明 |
|---|---|---|---|
| log_id | 是 | uint64 | 唯一的log id,用于问题定位 |
| page_result | 否 | array [object] | 识别结果数组,包含多页识别结果 |
| +lines_result_num | 是 | uint32 | 识别单页行结果个数 |
| + lines | 是 | string | 单页的识别结果字符串 |
| + words | 否 | string | 识别结果字符串 |
| + probability | 是 | string | 识别结果中每一行的置信度值。 · average:行置信度平均值 · variance:行置信度方差 · min:行置信度最小值 当请求参数 probability=true 时返回该字段。 |
| +rec_polys | 是 | Array | 文字所在的多边形坐标点(用于画不规则四边形框),每个检测框由4个顶点坐标构成的numpy数组表示,数组shape为(4, 2),数据类型为int16 |
| +rec_boxes | 是 | Array | 文字所在的矩形包围盒坐标(用于画规则矩形框),shape为(n, 4),dtype为int16。示例值为[x_min, y_min, x_max, y_max]坐标,其中(x_min, y_min)为左上角坐标,(x_max, y_max)为右下角坐标 |
返回示例
JSON
1{
2 "page_result": [
3 {
4 "lines": [
5 "无言独上西楼",
6 "月如钩",
7 "寂寞梧桐深院锁清秋",
8 "剪不断理还乱是离愁",
9 "别是一般滋味在心头"
10 ],
11 "probability": [
12 0.986164391040802,
13 0.9752357602119446,
14 0.9896835684776306,
15 0.9941657185554504,
16 0.9918990731239319
17 ],
18 "rec_polys": [
19 [
20 [
21 405,
22 104
23 ],
24 [
25 915,
26 104
27 ],
28 [
29 915,
30 242
31 ],
32 [
33 405,
34 242
35 ]
36 ],
37 [
38 [
39 408,
40 267
41 ],
42 [
43 684,
44 267
45 ],
46 [
47 684,
48 393
49 ],
50 [
51 408,
52 393
53 ]
54 ],
55 [
56 [
57 403,
58 425
59 ],
60 [
61 1346,
62 391
63 ],
64 [
65 1352,
66 542
67 ],
68 [
69 409,
70 575
71 ]
72 ],
73 [
74 [
75 409,
76 600
77 ],
78 [
79 1361,
80 566
81 ],
82 [
83 1367,
84 716
85 ],
86 [
87 415,
88 750
89 ]
90 ],
91 [
92 [
93 412,
94 788
95 ],
96 [
97 1291,
98 743
99 ],
100 [
101 1298,
102 891
103 ],
104 [
105 419,
106 935
107 ]
108 ]
109 ],
110 "rec_boxes": [
111 [
112 405,
113 104,
114 915,
115 242
116 ],
117 [
118 408,
119 267,
120 684,
121 393
122 ],
123 [
124 403,
125 391,
126 1352,
127 575
128 ],
129 [
130 409,
131 566,
132 1367,
133 750
134 ],
135 [
136 412,
137 743,
138 1298,
139 935
140 ]
141 ],
142 "lines_result_num": 5
143 }
144 ],
145 "log_id": 2041410581916955057
146}
评价此篇文章
