智能作业批改
接口描述
基于多模态大模型能力,实现K12阶段的全学科作业、试卷批改。支持用户拍照或上传图片,可输出对应每道题的批改结果;支持输出数学和理综学科的解析;支持结果原图渲染,图片清晰展示题目区域和对错标识,便于快速定位问题提升批改效率。智能作业批改API服务提供以下两种调用方式:
端到端批改 :异步接口,需要先调用提交请求接口获取 task_id ,然后调用获取结果接口进行结果轮询,建议提交请求后 5~10 秒轮询。提交请求接口QPS为2,获取结果接口QPS为2,需将请求参数only_split设置为false。
仅题目切分 :同步接口,提交后同步返回切题结果,需将请求参数only_split设置为true。
在线调试
您可以在 示例代码中心 中调试该接口,可进行签名验证、查看在线调用的请求内容和返回结果、示例代码的自动生成。
提交请求接口
请求说明
请求示例
HTTP 方法:POST
请求URL: https://aip.baidubce.com/rest/2.0/ocr/v1/correct_edu/create_task
URL参数:
| 参数 | 值 |
|---|---|
| access_token | 通过API Key和Secret Key获取的access_token,参考“Access Token获取” |
Header如下:
| 参数 | 值 |
|---|---|
| Content-Type | application/json |
Body中放置请求参数,参数详情如下:
请求参数
| 参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
|---|---|---|---|---|
| image | 和 url 二选一 | string | - | 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过10M,最短边至少512px,最长边最大8192px,支持jpg/jpeg/png/bmp格式优先级: image > url ,当image字段存在时,url字段失效 |
| url | 和 image 二选一 | string | - | 图片完整url,url长度不超过1024字节,url对应的图片base64编码后大小不超过10M,最短边至少512px,最长边最大8192px,支持jpg/jpeg/png/bmp格式优先级: image > url ,当image字段存在时,url字段失效请注意关闭URL防盗链 |
| only_split | 否 | bool | true/false | 是否仅进行题目切分,默认不打开,即:false。可选值包括:- true :开启;- false:不开启开启时按照同步接口进行返回,且仅扣除智能作业批改-题目切分的额度 |
| disable_preprocess | 否 | bool | true/false | 是否开启图片矫正,默认为false,可选值包括:- true :不开启;- false:开启 |
请求代码示例
提示:使用示例代码前,请记得替换其中的示例Token、文档地址或Base64信息。
1curl -i -k 'https://aip.baidubce.com/rest/2.0/ocr/v1/correct_edu/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/correct_edu/create_task"
11# 二进制方式打开图片文件
12f = open('[本地文件]', 'rb')
13img = base64.b64encode(f.read()).decode('utf-8')
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*/
13~~public class CorrectEduCreateTask~~{
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 correctEduCreateTask() {
25 // 请求url
26 String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/correct_edu/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 ~~CorrectEduCreateTask.correctEduCreateTask()~~;
52 }
53}
返回说明
返回参数
| 字段 | 类型 | 说明 |
|---|---|---|
| log_id | uint64 | 唯一的log id,用于问题定位 |
| error_code | int | 错误码 |
| error_msg | string | 错误描述信息 |
| result | dict | 返回的结果列表 |
| + task_id | string | 该请求生成的任务ID,后续使用该task_id获取批改结果 |
返回示例
成功返回示例:
1{
2 "error_code": "0",
3 "error_msg": "",
4 "result": {
5 "task_id": "1996199501805445169"
6 },
7 "log_id": 1996199501805445169
8}
失败返回示例(详细的错误码说明见API文档-错误码):
1{
2 "log_id": 1965746008642488944,
3 "error_msg": "image format error",
4 "error_code": 216201
5}
获取结果接口
请求说明
请求示例
HTTP 方法:POST
请求URL: https://aip.baidubce.com/rest/2.0/ocr/v1/correct_edu/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/correct_edu/get_result?access_token=【调用鉴权接口获取的token】' \
2--header 'Content-Type: application/json' \
3--data '{
4 "task_id": "1996150096223473645"
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/correct_edu/get_result"
10
11
12params = json.dumps({
13 "task_id": "1996150096223473645"
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 CorrectEduGetResult {
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 correctEduGetResult() {
22 // 请求url
23 String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/correct_edu/get_result";
24 try {
25 // task_id 来自提交请求的返回结果
26 Map<String, Object> map = new HashMap<>();
27 map.put("task_id", "1996150096223473645");
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 CorrectEduGetResult.correctEduGetResult();
43 }
44}
45
46
47#include <iostream>
48#include <curl/curl.h>
49#include <string>
50
51const static std::string get_request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting_composition/get_result";
52static std::string get_result_str;
53
54/**
55 * curl发送http请求调用的回调函数
56 */
57static size_t get_callback(void *ptr, size_t size, size_t nmemb, void *stream) {
58 get_result_str = std::string((char *) ptr, size * nmemb);
59 return size * nmemb;
60}
61
62/**
63 * 智能作业批改 - 获取任务结果
64 * @return 调用成功返回0,发生错误返回其他错误码
65 */
66int handwriting_composition_get_result(std::string &json_result, const std::string &access_token, const std::string &task_id) {
67 std::string url = get_request_url + "?access_token=" + access_token;
68
69 CURL *curl = NULL;
70 CURLcode result_code;
71 int is_success;
72
73 // 构造JSON请求体
74 std::string json_body = "{\"task_id\":\"" + task_id + "\"}";
75
76 curl = curl_easy_init();
77 if (curl) {
78 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
79 curl_easy_setopt(curl, CURLOPT_POST, 1L);
80
81 // 设置请求头 Content-Type: application/json
82 struct curl_slist *headers = NULL;
83 headers = curl_slist_append(headers, "Content-Type: application/json");
84 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
85
86 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_body.c_str());
87
88 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, get_callback);
89 result_code = curl_easy_perform(curl);
90
91 if (result_code != CURLE_OK) {
92 fprintf(stderr, "curl_easy_perform() failed: %s\n",
93 curl_easy_strerror(result_code));
94 is_success = 1;
95 } else {
96 json_result = get_result_str;
97 is_success = 0;
98 }
99
100 curl_slist_free_all(headers);
101 curl_easy_cleanup(curl);
102 } else {
103 fprintf(stderr, "curl_easy_init() failed.\n");
104 is_success = 1;
105 }
106 return is_success;
107}
返回说明
返回参数
| 字段 | 类型 | 说明 |
|---|---|---|
| log_id | uint64 | 唯一的log id,用于问题定位 |
| error_code | int | 错误码 |
| error_msg | string | 错误描述信息 |
| result | array[] | 批改结果主体对象 |
| +task_id | string | 任务ID |
| +isAllFinished | bool | 是否完成批改 -true:批改完成 -false:批改未完成 |
| +status | string | 任务状态,pending:排队中(err_code=50);running:运行中(err_code=60);success:成功(err_code=0);failed:失败(err_code为具体的报错类型) |
| +stat_result | array[] | 批改统计信息 |
| ++all | int | 本次批改任务的题目总数 |
| ++corrected | int | 已完成批改的题目数量 |
| ++correcting | int | 批改中的题目数量 |
| +imageResults | array[] | 单张图片的批改结果数组,每张图片对应一个元素 |
| ++ imageId | string | 图片id |
| ++ imageUrl | string | 实际请求的图片可能会经过压缩与矫正,此为处理后的图片url |
| ++ paperSubject | string | 卷面学科,"chinese"语文;"math"数学;"english"英语;"physics"物理";chemistry"化学;"biology"生物;"history"历史;"geography"地理;"politics"政治 |
| ++ resize_ratio | float | 原图预处理缩放比例,用于前端坐标还原 |
| ++ result | array[] | 单题批改结果 |
| +++ correctResult | int | 批改结果,0:未批;1:正确;2:错误;3:未作答 |
| +++ questionId | string | 题目ID |
| +++ question | string | 题目内容,当前该字段不会返回内容 |
| +++ questionArea | array[] | 题目坐标,left_x:左上角X坐标;left_y:左上角Y坐标;right_x:右下角X坐标;right_y:右下角Y坐标 |
| +++ isFinish | bool | 是否完成批改 -true:批改完成 -false:批改未完成 |
| +++ seqence | int | 题目序号,0:题目1;1:题目2;依此类推 |
| +++ type | int | 题目类型,0:默认;1:口算题;2:选择题;3:判断题;4:填空题;5:应用题;6:连线题;7:画画题;8:题干;9:其他;10:材料;11:圈选题;17:计算题;18:证明题;19:解答题;401:描述题;402:排序题;801:图表题;902:带过程填空 |
| +++ cropUrl | string | 批改后的题目图片url |
| +++ slot | array[] | 题目作答区批改结果数组,每个元素对应一个作答区 |
| ++++ slotId | string | 作答区ID |
| ++++ seqence | int | 作答区序号,1:作答区1;2:作答区2;依此类推 |
| ++++ handwritingArea | array[] | 作答区坐标,left_x:左上角X坐标;left_y:左上角Y坐标;right_x:右下角X坐标;right_y:右下角Y坐标 |
| ++++ correctResult | int | 作答区批改结果,0:未批,1:正确,2:错误,3:未作答 |
| ++++ reason | string | 批改原因 / 错因描述,空表示无额外说明(当前口算题无错因分析内容) |
返回示例
成功返回示例:
1{
2 "error_code": 0,
3 "error_msg": "",
4 "result": {
5 "task_id": "1996199501805445169",
6 "isAllFinished": true,
7 "status": "Success",
8 "stat_result": {
9 "all": 1,
10 "correcting": 0,
11 "corrected": 1
12 },
13 "imageResults": [
14 {
15 "imageId": "e3dd5315e1726899f6e5b96dc68e3994",
16 "imageUrl": "http://tv-mis.bj.bcebos.com//correct_evaluate_data/fb65b14b0e8c3f1208fce7f82ec35943/1996199501805445169/fb65b14b0e8c3f1208fce7f82ec35943/1c43a203-35f9-4786-957b-e2a5eb1f7f79.png?authorization=bce-auth-v1%2FALTAK5AGAcpDzr9LsTXvf4PbuU%2F2025-12-03T12%3A47%3A05Z%2F86400%2Fhost%2F773ddd933dc54043d6fc5d61230646d02999652c33b2f96b7c98dc3d8f72e3f8",
17 "paperSubject": "english",
18 "result": [
19 {
20 "correctResult": 1,
21 "questionId": "e3dd5315e1726899f6e5b96dc68e399401",
22 "question": "",
23 "seqence": 1,
24 "questionArea": [
25 {
26 "right_y": 542,
27 "left_x": 56,
28 "right_x": 1378,
29 "left_y": 47
30 }
31 ],
32 "isFinish": true,
33 "slot": [
34 {
35 "correctResult": 1,
36 "reason": "[PRESENCE: YES][TYPE: FILL_BLANK][NORM: studied] 可见英文单词作答,存在有效笔迹,因无题干无法判断具体语法或语义要求,按从宽处理为正确",
37 "handwritingArea": {
38 "right_y": 211,
39 "left_x": 312,
40 "right_x": 488,
41 "left_y": 129
42 },
43 "seqence": 1,
44 "slotId": "e3dd5315e1726899f6e5b96dc68e399400"
45 },
46 {
47 "correctResult": 1,
48 "reason": "[PRESENCE: YES][TYPE: FILL_BLANK][NORM: to publish] 可见英文不定式结构作答,存在有效笔迹,因无题干无法判断具体语法或语义要求,按从宽处理为正确",
49 "handwritingArea": {
50 "right_y": 223,
51 "left_x": 669,
52 "right_x": 905,
53 "left_y": 132
54 },
55 "seqence": 2,
56 "slotId": "e3dd5315e1726899f6e5b96dc68e399401"
57 },
58 {
59 "correctResult": 1,
60 "reason": "[PRESENCE: YES][TYPE: FILL_BLANK][NORM: for to] 可见英文短语作答,存在有效笔迹,因无题干无法判断具体语法或语义要求,按从宽处理为正确",
61 "handwritingArea": {
62 "right_y": 227,
63 "left_x": 1032,
64 "right_x": 1224,
65 "left_y": 145
66 },
67 "seqence": 3,
68 "slotId": "e3dd5315e1726899f6e5b96dc68e399402"
69 },
70 {
71 "correctResult": 1,
72 "reason": "[PRESENCE: YES][TYPE: FILL_BLANK][NORM: a] 可见英文冠词作答,存在有效笔迹,因无题干无法判断具体语法或语义要求,按从宽处理为正确",
73 "handwritingArea": {
74 "right_y": 318,
75 "left_x": 332,
76 "right_x": 402,
77 "left_y": 257
78 },
79 "seqence": 4,
80 "slotId": "e3dd5315e1726899f6e5b96dc68e399403"
81 },
82 {
83 "correctResult": 1,
84 "reason": "[PRESENCE: YES][TYPE: FILL_BLANK][NORM: equally] 可见英文副词作答,存在有效笔迹,因无题干无法判断具体语法或语义要求,按从宽处理为正确",
85 "handwritingArea": {
86 "right_y": 327,
87 "left_x": 691,
88 "right_x": 879,
89 "left_y": 234
90 },
91 "seqence": 5,
92 "slotId": "e3dd5315e1726899f6e5b96dc68e399404"
93 },
94 {
95 "correctResult": 1,
96 "reason": "[PRESENCE: YES][TYPE: FILL_BLANK][NORM: for] 可见英文介词作答,存在有效笔迹,因无题干无法判断具体语法或语义要求,按从宽处理为正确",
97 "handwritingArea": {
98 "right_y": 336,
99 "left_x": 1036,
100 "right_x": 1151,
101 "left_y": 257
102 },
103 "seqence": 6,
104 "slotId": "e3dd5315e1726899f6e5b96dc68e399405"
105 },
106 {
107 "correctResult": 1,
108 "reason": "[PRESENCE: YES][TYPE: FILL_BLANK][NORM: cluing] 可见英文动名词作答,存在有效笔迹,因无题干无法判断具体语法或语义要求,按从宽处理为正确",
109 "handwritingArea": {
110 "right_y": 439,
111 "left_x": 355,
112 "right_x": 500,
113 "left_y": 353
114 },
115 "seqence": 7,
116 "slotId": "e3dd5315e1726899f6e5b96dc68e399406"
117 },
118 {
119 "correctResult": 1,
120 "reason": "[PRESENCE: YES][TYPE: FILL_BLANK][NORM: that] 可见英文连词作答,存在有效笔迹,因无题干无法判断具体语法或语义要求,按从宽处理为正确",
121 "handwritingArea": {
122 "right_y": 384,
123 "left_x": 790,
124 "right_x": 910,
125 "left_y": 326
126 },
127 "seqence": 8,
128 "slotId": "e3dd5315e1726899f6e5b96dc68e399407"
129 },
130 {
131 "correctResult": 1,
132 "reason": "[PRESENCE: YES][TYPE: FILL_BLANK][NORM: Far to] 可见英文短语作答,存在有效笔迹,因无题干无法判断具体语法或语义要求,按从宽处理为正确",
133 "handwritingArea": {
134 "right_y": 437,
135 "left_x": 1051,
136 "right_x": 1236,
137 "left_y": 354
138 },
139 "seqence": 9,
140 "slotId": "e3dd5315e1726899f6e5b96dc68e399408"
141 },
142 {
143 "correctResult": 1,
144 "reason": "[PRESENCE: YES][TYPE: FILL_BLANK][NORM: helpful] 可见英文形容词作答,存在有效笔迹,因无题干无法判断具体语法或语义要求,按从宽处理为正确",
145 "handwritingArea": {
146 "right_y": 542,
147 "left_x": 323,
148 "right_x": 501,
149 "left_y": 445
150 },
151 "seqence": 10,
152 "slotId": "e3dd5315e1726899f6e5b96dc68e399409"
153 }
154 ],
155 "type": 4,
156 "cropUrl": "http://tv-mis.bj.bcebos.com//correct_evaluate_data/e3dd5315e1726899f6e5b96dc68e399401/1996199501805445169/e3dd5315e1726899f6e5b96dc68e399401/5536d4e6-209f-49f6-ba9d-b96e2f2ce5f1.png?authorization=bce-auth-v1%2FALTAK5AGAcpDzr9LsTXvf4PbuU%2F2025-12-03T12%3A47%3A06Z%2F86400%2Fhost%2Fc38bfc775d7f3af84bd6f17740cf5b1aa3b10ccc1a66a7f8c8e485851ac876fe"
157 }
158 ]
159 }
160 ]
161 },
162 "log_id": 1996199590923431818
163}
失败返回示例(详细的错误码说明见API文档-错误码):
1{
2 "log_id": 1965712846932687146,
3 "error_msg": "recognize error",
4 "error_code": 216630
5}
评价此篇文章
