手写作文识别(多模态)
接口描述
基于多模态大模型实现手写体作文内容的精准识别。支持单页、跨页、分栏作文等多种版式场景;能够有效过滤阴影、涂抹痕迹、额外批注等多种干扰信息;精准识别中文、英文手写作文笔迹,识别后输出易于处理的结构化文本,包含字层级、行层级、段落层级的坐标,无缝对接后续批改流程。
手写作文识别(多模态)API服务为异步接口,需要先调用提交请求接口获取 task_id,然后调用获取结果接口进行结果轮询,建议提交请求后 5~10 秒轮询。提交请求接口QPS为2,获取结果接口QPS为10。
在线调试
您可以在 示例代码中心 中调试该接口,可进行签名验证、查看在线调用的请求内容和返回结果、示例代码的自动生成。
申请试用
该接口正在邀测中,请您先提交 合作咨询 或 提交工单,提供公司名称、appid、应用场景等信息,工作人员协助开通权限后方可使用。
提交请求接口
请求说明
请求示例
HTTP 方法:POST
请求URL: https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting_composition/create_task
URL参数:
| 参数 | 值 |
|---|---|
| access_token | 通过API Key和Secret Key获取的access_token,参考“Access Token获取” |
Header如下:
| 参数 | 值 |
|---|---|
| Content-Type | application/json |
Body中放置请求参数,参数详情如下:
请求参数
| 参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
|---|---|---|---|---|
| image | 和 url/pdf_file 三选一 | string | - | 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过10M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式优先级: image > url > pdf_file,当image字段存在时,url字段失效 |
| url | 和 Image/pdf_file 三选一 | string | - | 图片完整url,url长度不超过1024字节,url对应的图片base64编码后大小不超过10M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式优先级: image > url > pdf_file,当image字段存在时,url字段失效请注意关闭URL防盗链 |
| pdf_file | 和 image/url 三选一 | string | - | PDF文件,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过10M,最短边至少15px,最长边最大4096px优先级: image > url > pdf_file,当image字段存在时,url字段失效 |
| recognize_granularity | 否 | string | line/word/none | 识别粒度,控制坐标返回,可选: • line: 行级坐标返回 • word: 行级坐标+字级别坐标返回 • none: 不返回坐标 |
| pdf_file_num | 否 | string | - | - |
请求代码示例
提示:使用示例代码前,请记得替换其中的示例Token、文档地址或Base64信息。
1curl -i -k 'https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting_composition/create_task?access_token=【调用鉴权接口获取的token】'
2 -H 'Content-Type: application/json'
3--data '{
4 "url": "https://ai.bdstatic.com/file/088749BAB26D4809B8A0B96FE100E7F0"
5}'
1# encoding:utf-8
2
3import requests
4import base64
5
6'''
7作文识别提交请求
8'''
9
10request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting_composition/create_task"
11# 二进制方式打开图片文件
12f = open('[本地文件]', 'rb')
13img = base64.b64encode(f.read())
14
15params = json.dumps({
16 "image": img
17})
18
19access_token = '[调用鉴权接口获取的token]'
20request_url = request_url + "?access_token=" + access_token
21headers = {'content-type': 'application/json'}
22response = requests.post(request_url, data=params, headers=headers)
23if response:
24 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;
6import com.google.gson.Gson;
7import java.util.HashMap;
8import java.util.Map;
9
10/**
11* 作文识别提交请求
12*/
13public class HandwritingCompositionCreateTask {
14
15 /**
16 * 重要提示代码中所需工具类
17 * FileUtil,Base64Util,HttpUtil,GsonUtils请从
18 * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
19 * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
20 * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
21 * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
22 * 下载
23 */
24 public static String handwritingCompositionCreateTask() {
25 // 请求url
26 String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting_composition/create_task";
27 try {
28 // 本地文件路径
29 String filePath = "[本地文件路径]";
30 byte[] imgData = FileUtil.readFileByBytes(filePath);
31 String imgStr = Base64Util.encode(imgData);
32
33 // 构造请求体
34 Map<String, Object> map = new HashMap<>();
35 map.put("image", imgStr); // 或者使用 url 参数
36 String param = new Gson().toJson(map);
37
38 // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
39 String accessToken = "[调用鉴权接口获取的token]";
40
41 String result = HttpUtil.post(url, accessToken, "application/json", param);
42 System.out.println(result);
43 return result;
44 } catch (Exception e) {
45 e.printStackTrace();
46 }
47 return null;
48 }
49
50 public static void main(String[] args) {
51 HandwritingCompositionCreateTask.handwritingCompositionCreateTask();
52 }
53}
返回说明
返回参数
| 字段 | 类型 | 说明 |
|---|---|---|
| log_id | uint64 | 唯一的log id,用于问题定位 |
| error_code | int | 错误码 |
| error_msg | string | 错误描述信息 |
| result | dict | 返回的结果列表 |
| + task_id | string | 该请求生成的task_id,后续使用该task_id获取识别结果 |
返回示例
成功返回示例:
1{
2 "error_code": 0,
3 "error_msg": "",
4 "log_id": "10138598131137362685273505665433",
5 "result": {
6 "task_id": "task-3zy9Bg8CHt1M4pPOcX2q5bg28j26801S"
7 }
8}
失败返回示例(详细的错误码说明见API文档-错误码):
1{
2 "log_id": 1965746008642488944,
3 "error_msg": "并发超限",
4 "error_code": 15
5}
获取结果接口
请求说明
请求示例
HTTP 方法:POST
请求URL: https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting_composition/get_result
URL参数:
| 参数 | 值 |
|---|---|
| access_token | 通过API Key和Secret Key获取的access_token,参考“Access Token获取” |
Header如下:
| 参数 | 值 |
|---|---|
| Content-Type | application/json |
Body中放置请求参数,参数详情如下:
请求参数
| 参数 | 是否必选 | 类型 | 说明 |
|---|---|---|---|
| task_id | 是 | string | 发送提交请求时返回的task_id |
请求代码示例
提示:使用示例代码前,请记得替换其中的示例Token、task_id。
1curl --location 'https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting_composition/get_result?access_token=【调用鉴权接口获取的token】' \
2--header 'Content-Type: application/json' \
3--data '{
4 "task_id": "1965376138007096888"
5}'
1# encoding:utf-8
2
3import requests
4import base64
5'''
6作文识别获取请求
7'''
8
9request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting_composition/get_result"
10
11
12params = json.dumps({
13 "task_id": "1965376138007096888"
14})
15access_token = '[调用鉴权接口获取的token]'
16request_url = request_url + "?access_token=" + access_token
17headers = {'content-type': 'application/json'}
18response = requests.post(request_url, data=params, headers=headers)
19if response:
20 print (response.json())
1package com.baidu.ai.aip;
2
3import com.baidu.ai.aip.utils.HttpUtil;
4import com.google.gson.Gson;
5
6import java.util.HashMap;
7import java.util.Map;
8
9/**
10* 作文识别获取请求
11*/
12public class HandwritingCompositionGetResult {
13
14 /**
15 * 重要提示代码中所需工具类
16 * HttpUtil,GsonUtils请从
17 * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
18 * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
19 * 下载
20 */
21 public static String handwritingCompositionGetResult() {
22 // 请求url
23 String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting_composition/get_result";
24 try {
25 // task_id 来自提交请求的返回结果
26 Map<String, Object> map = new HashMap<>();
27 map.put("task_id", "1965376138007096888");
28 String param = new Gson().toJson(map);
29
30 String accessToken = "[调用鉴权接口获取的token]";
31
32 String result = HttpUtil.post(url, accessToken, "application/json", param);
33 System.out.println(result);
34 return result;
35 } catch (Exception e) {
36 e.printStackTrace();
37 }
38 return null;
39 }
40
41 public static void main(String[] args) {
42 HandwritingCompositionGetResult.handwritingCompositionGetResult();
43 }
44}
45
46
47
48#include <iostream>
49#include <curl/curl.h>
50#include <string>
51
52const static std::string get_request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting_composition/get_result";
53static std::string get_result_str;
54
55/**
56 * curl发送http请求调用的回调函数
57 */
58static size_t get_callback(void *ptr, size_t size, size_t nmemb, void *stream) {
59 get_result_str = std::string((char *) ptr, size * nmemb);
60 return size * nmemb;
61}
62
63/**
64 * 作文识别 - 获取任务结果
65 * @return 调用成功返回0,发生错误返回其他错误码
66 */
67int handwriting_composition_get_result(std::string &json_result, const std::string &access_token, const std::string &task_id) {
68 std::string url = get_request_url + "?access_token=" + access_token;
69
70 CURL *curl = NULL;
71 CURLcode result_code;
72 int is_success;
73
74 // 构造JSON请求体
75 std::string json_body = "{\"task_id\":\"" + task_id + "\"}";
76
77 curl = curl_easy_init();
78 if (curl) {
79 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
80 curl_easy_setopt(curl, CURLOPT_POST, 1L);
81
82 // 设置请求头 Content-Type: application/json
83 struct curl_slist *headers = NULL;
84 headers = curl_slist_append(headers, "Content-Type: application/json");
85 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
86
87 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_body.c_str());
88
89 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, get_callback);
90 result_code = curl_easy_perform(curl);
91
92 if (result_code != CURLE_OK) {
93 fprintf(stderr, "curl_easy_perform() failed: %s\n",
94 curl_easy_strerror(result_code));
95 is_success = 1;
96 } else {
97 json_result = get_result_str;
98 is_success = 0;
99 }
100
101 curl_slist_free_all(headers);
102 curl_easy_cleanup(curl);
103 } else {
104 fprintf(stderr, "curl_easy_init() failed.\n");
105 is_success = 1;
106 }
107 return is_success;
108}
返回说明
返回参数
| 字段 | 类型 | 说明 |
|---|---|---|
| log_id | uint64 | 唯一的log id,用于问题定位 |
| error_code | int | 错误码 |
| error_msg | string | 错误描述信息 |
| result | dict | 返回的结果列表 |
| + task_id | string | 任务ID |
| + status | string | 任务状态,pending:排队中;processing:运行中;success:成功;failed:失败 |
| + created_time | string | 任务创建时间 |
| + started_time | string | 任务开始时间 |
| + finished_time | string | 任务结束时间 |
| + duration | string | 任务执行时长 |
| + result | dict | 作文识别结果 |
| ++ recognize_granularity | string | 识别粒度,控制坐标返回,可选: • line: 行级坐标返回 • word: 行级坐标+字级别坐标返回 • none: 不返回坐标 |
| ++ essayOverall | dict | 识别的文本总体内容 |
| +++ titleText | string | 作文题目文本 |
| +++ contentText | string | 作文正文文本 |
| ++ title | dict | 作文题目详细信息 |
| +++ bbox | list | 仅字级和行级粒度返回,标题外接矩形坐标 |
| +++ text | string | 标题文本内容 |
| +++ chars | list | 仅字级粒度返回,标题字级别详细列表 |
| ++++ isPunctuation | string | 该字符是否为标点符号 |
| ++++ bbox | dict | 字坐标 |
| ++++ char | string | 单个字符 |
| ++++ index | string | 字符索引 |
| ++ content | dict | 作文正文详细信息 |
| +++ lines | list | 仅字级和行级粒度返回,行级信息列表 |
| ++++ lineId | string | 行的唯一标识符 |
| ++++ text | string | 该行的文本内容 |
| ++++ bbox | string | 该行坐标 |
| ++++ paragraphId | string | 该行所属段落的ID ,关联 paragraphs |
| ++++ chars | list | 仅字级粒度返回,行内单字/字符详细列表 |
| +++++ char | string | 正文行的单字内容 |
| +++++ index | string | 正文行的单字编号 |
| +++++ bbox | string | 正文行的单字坐标 |
| +++++ isPunctuation | string | 正文行的单字是否为坐标 |
| +++ paragraphs | list | 段落级逻辑信息列表 |
| ++++ bbox | list | 仅字级和行级粒度返回,段落轮廓坐标列表,可能含多个框 |
| ++++ paragraphId | string | 段落唯一标识符 (如 "p1") |
| ++++ isColumn | string | 仅字级和行级粒度返回,是否分栏 (1:分栏,0: 不分栏) |
| ++++ text | string | 段落完整文本 |
| ++++ sentences | list | 段落内的句子列表 |
| +++++ bbox | list | 仅字级和行级粒度返回,句子轮廓坐标列表 |
| +++++ sentenceId | string | 句子唯一标识符 |
| +++++ text | string | 句子文本内容 |
| +++++ lineSegments | list | 仅字级和行级粒度返回,句行映射片段 ,描述该句子对应的行及起止位置 |
| ++++++ lineId | string | 对应 lines 里的 lineId |
| ++++++ startIndex | string | 该句在行中的起始位置标识 |
| ++++++ endIndex | string | 该句在行中的结束位置标识 |
返回示例
通用成功返回示例:
1{
2 "error_code": "0",
3 "error_msg": "",
4 "result": {
5 "task_id": "1965376138007096888",
6 "status": "Success",
7 "created_time": 1757417161000,
8 "started_time": 1757417162000,
9 "finished_time": 1757497412914,
10 "duration": 80250914,
11 "result": {
12 "recognize_granularity": "none",
13 "essayOverall": {
14 "titleText": "我的童年",
15 "contentText": "每个人都有难忘的童年。我的童年充满了欢声笑语,\n夏天会和小伙伴去河边捉鱼。冬天则围在火炉旁\n听奶奶讲故事。"
16 },
17 "title": { // 标题极简设计:仅文本+坐标(按需加单字)
18 "text": "我的童年",
19 "bbox": { "x": 100, "y": 50, "w": 95, "h": 20 } // 标题整体的xywh包围盒
20 // 字级粒度时新增chars数组,否则无此字段
21 },
22 "content": { // 正文保留完整层级:行→段落→句子
23 "paragraphs": [], // 逻辑段落(含坐标+句子关联)
24 "lines": [] // 物理行(含坐标+段落归属)
25 }
26 }
27}
行级别坐标
1"result": {
2 "recognize_granularity": "line",
3 "essayOverall": { /* 同上 */ },
4 "title": {
5 "text": "我的童年",
6 "bbox": [{ "x": 100, "y": 50, "w": 95, "h": 20 } ]// 标题整体坐标,无行/句子拆分
7 },
8 "content": {
9 "paragraphs": [
10 {
11 "paragraphId": "p1",
12 "text": "每个人都有难忘的童年。我的童年充满了欢声笑语,夏天会和小伙伴去河边捉鱼。",
13 "bbox": [{ "x": 80, "y": 100, "w": 350, "h": 40 }], // 段落最小包围盒
14 "isColumn": 0,
15 "sentences": [
16 {
17 "sentenceId": "s1-p1",
18 "text": "每个人都有难忘的童年。",
19 "bbox":[{ "x": 80, "y": 100, "w": 260, "h": 20 }], // 句子最小包围盒
20 "lineSegments": [ // 句子对应的行片段(解决跨行列)
21 { "lineId": "l1", "startIndex": 0, "endIndex": 10 }
22 ]
23 },
24 {
25 "sentenceId": "s2-p1",
26 "text": "我的童年充满了欢声笑语,夏天会和小伙伴去河边捉鱼。",
27 "bbox": [{ "x": 80, "y": 100, "w": 350, "h": 40 }],
28 "lineSegments": [
29 { "lineId": "l1", "startIndex": 11, "endIndex": 25 },
30 { "lineId": "l2", "startIndex": 0, "endIndex": 12 }
31 ]
32 }
33 ]
34 },
35 {
36 "paragraphId": "p2",
37 "text": "冬天则围在火炉旁听奶奶讲故事。",
38 "bbox": [{ "x": 80, "y": 130, "w": 350, "h": 40 }],
39 "sentences": [
40 {
41 "sentenceId": "s1-p2",
42 "text": "冬天则围在火炉旁听奶奶讲故事。",
43 "bbox":[{ "x": 80, "y": 130, "w": 350, "h": 40 }],
44 "lineSegments": [
45 { "lineId": "l2", "startIndex": 13, "endIndex": 20 },
46 { "lineId": "l3", "startIndex": 0, "endIndex": 8 }
47 ]
48 }
49 ]
50 }
51 ],
52 "lines": [
53 {
54 "lineId": "l1",
55 "text": "每个人都有难忘的童年。我的童年充满了欢声笑语,",
56 "bbox": { "x": 80, "y": 100, "w": 350, "h": 20 },
57 "paragraphId": "p1" // 仅关联一个段落,无数组
58 },
59 {
60 "lineId": "l2",
61 "text": "夏天会和小伙伴去河边捉鱼。冬天则围在火炉旁",
62 "bbox": { "x": 80, "y": 130, "w": 350, "h": 20 },
63 "paragraphId": "p1" // 仅关联一个段落,无数组
64 },
65 {
66 "lineId": "l3",
67 "text": "听奶奶讲故事。",
68 "bbox": { "x": 80, "y": 160, "w": 120, "h": 20 },
69 "paragraphId": "p2" // 仅关联一个段落,无数组
70 }
71 ]
72 }
73}
字级别坐标
1"result": {
2 "recognize_granularity": "word",
3 "essayOverall": { /* 同上 */ },
4 "title": {
5 "text": "我的童年",
6 "bbox": { "x": 100, "y": 50, "w": 95, "h": 20 }, // 标题整体坐标
7 "chars": [ // 标题单字坐标(仅字级粒度返回)
8 { "char": "我", "index": 0, "bbox": { "x": 100, "y": 50, "w": 20, "h": 20 }, "isPunctuation": false },
9 { "char": "的", "index": 1, "bbox": { "x": 125, "y": 50, "w": 20, "h": 20 }, "isPunctuation": false },
10 { "char": "童", "index": 2, "bbox": { "x": 150, "y": 50, "w": 20, "h": 20 }, "isPunctuation": false },
11 { "char": "年", "index": 3, "bbox": { "x": 175, "y": 50, "w": 20, "h": 20 }, "isPunctuation": false }
12 ]
13 },
14 "content": {
15 "paragraphs": [ /* 同line粒度的段落/句子结构 */ ],
16 "lines": [
17 {
18 "lineId": "l1",
19 "text": "每个人都有难忘的童年。我的童年充满了欢声笑语,",
20 "bbox": { "x": 80, "y": 100, "w": 350, "h": 20 },
21 "paragraphId": "p1",
22 "chars": [ // 正文行的单字坐标(仅字级粒度返回)
23 { "char": "每", "index": 0, "bbox": { "x": 80, "y": 100, "w": 20, "h": 20 }, "isPunctuation": false },
24 { "char": "个", "index": 1, "bbox": { "x": 105, "y": 100, "w": 20, "h": 20 }, "isPunctuation": false },
25 // 其余单字省略...
26 { "char": ",", "index": 24, "bbox": { "x": 425, "y": 100, "w": 10, "h": 20 }, "isPunctuation": true }
27 ]
28 },
29 // 其余行同此结构(含chars数组)
30 ]
31 }
32}
33
34 //无坐标
35"result": {
36 "recognizeGranularity": "none",
37 "essayOverall": { /* 同上 */ },
38 "title": {
39 "text": "我的童年" // 无bbox,无chars
40 },
41 "content": {
42 "paragraphs": [
43 {
44 "paragraphId": "p1",
45 "text": "每个人都有难忘的童年。我的童年充满了欢声笑语,夏天会和小伙伴去河边捉鱼。",
46 "sentences": [
47 {
48 "sentenceId": "s1-p1",
49 "text": "每个人都有难忘的童年。" },
50 {
51 "sentenceId": "s2-p1",
52 "text": "我的童年充满了欢声笑语,夏天会和小伙伴去河边捉鱼。" }
53 ]
54 },
55 {
56 "paragraphId": "p2",
57 "text": "冬天则围在火炉旁听奶奶讲故事。",
58 "sentences": [
59 {
60 "sentenceId": "s1-p2",
61 "text": "冬天则围在火炉旁听奶奶讲故事。" }
62 ]
63 }
64 ]
65 // 无lines字段
66 }
67}
68
失败返回示例(详细的错误码说明见API文档-错误码):
1{
2 "log_id": 1965712846932687146,
3 "error_msg": "the input image is not a composition",
4 "error_code": 216100
5}
