拍照解题
更新时间:2026-06-25
接口描述
基于多模态大模型能力,实现K12阶段的全学科作业、试卷解题。支持用户拍照或上传图片,可输出对应每道题的答案、思路分析、过程解析等;支持结果原图渲染,图片清晰展示题目区域和对应答案解析,便于快速定位题目及其解答过程。
在线调试
您可以在 示例代码中心 中调试该接口,可进行签名验证、查看在线调用的请求内容和返回结果、示例代码的自动生成。
提交请求接口
请求说明
请求示例
HTTP 方法:POST
请求URL: https://aip.baidubce.com/stream/2.0/ocr/v1/solve
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,最长边最大8192px,支持jpg/jpeg/png/bmp格式。优先级:image > url > pdf_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字段失效 |
| pdf_file_num | 否 | string | - | 需要识别的PDF文件的对应页码,当 pdf_file 参数有效时,识别传入页码的对应页面内容,若不传入,则默认识别第 1 页 |
| disable_preprocess | 否 | bool | true/false | 是否开启图片矫正与增强,默认为false。可选值包括:true:开启false:不开启 |
| stream_switch | 否 | bool | true/false | 是否以流式接口的形式返回数据。可选值包括:true:是,按SSE协议逐块返回内容,以一条is_end:"true"消息结束;false(默认值):否,当所有内容生成完毕后一起返回 |
请求代码示例
1# 1. 获取access_token,有效期30天
2curl -X POST 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=你的API_Key&client_secret=你的Secret_Key' -H 'Content-Type: application/json'
3
4# 2. 使用access_token调用接口
5curl --location --request POST 'https://aip.baidubce.com/stream/2.0/ocr/v1/solve?access_token=第一步中返回的 access_token' \
6--header 'Content-Type: application/json' \
7--data-raw '{"url":"https://baidu-ai.bj.bcebos.com/ocr/doc_analysis.png","disable_preprocess":false,"stream_switch":false}'
1import requests
2import json
3
4API_KEY = "xxxxx"
5SECRET_KEY = "xxxxx"
6
7def main():
8
9 url = "https://aip.baidubce.com/stream/2.0/ocr/v1/solve?access_token=" + get_access_token()
10
11 payload = json.dumps({
12 "url": "https://baidu-ai.bj.bcebos.com/ocr/doc_analysis.png",
13 "disable_preprocess": False,
14 "stream_switch": False
15 }, ensure_ascii=False)
16 headers = {
17 'Content-Type': 'application/json'
18 }
19
20 response = requests.request("POST", url, headers=headers, data=payload.encode("utf-8"))
21
22 response.encoding = "utf-8"
23 print(response.text)
24
25
26def get_access_token():
27 """
28 使用 AK,SK 生成鉴权签名(Access Token)
29 :return: access_token,或是None(如果错误)
30 """
31 url = "https://aip.baidubce.com/oauth/2.0/token"
32 params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
33 return str(requests.post(url, params=params).json().get("access_token"))
34
35if __name__ == '__main__':
36 main()
1package baidu.com;
2
3import okhttp3.*;
4import org.json.JSONObject;
5import java.util.concurrent.TimeUnit;
6
7import java.io.*;
8
9
10/**
11 * 需要添加依赖
12 * <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
13 * <dependency>
14 * <groupId>com.squareup.okhttp3</groupId>
15 * <artifactId>okhttp</artifactId>
16 * <version>4.12.0</version>
17 * </dependency>
18 */
19
20class Sample {
21 public static final String API_KEY = "xxxxx";
22 public static final String SECRET_KEY = "xxxxx";
23
24 public static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().readTimeout(300, TimeUnit.SECONDS).build();
25
26 public static void main(String []args) throws IOException{
27 MediaType mediaType = MediaType.parse("application/json");
28 RequestBody body = RequestBody.create(mediaType, "{\"url\":\"https://baidu-ai.bj.bcebos.com/ocr/doc_analysis.png\",\"disable_preprocess\":false,\"stream_switch\":false}");
29 Request request = new Request.Builder()
30 .url("https://aip.baidubce.com/stream/2.0/ocr/v1/solve?access_token=" + getAccessToken())
31 .method("POST", body)
32 .addHeader("Content-Type", "application/json")
33 .build();
34 Response response = HTTP_CLIENT.newCall(request).execute();
35 System.out.println(response.body().string());
36
37 }
38
39
40 /**
41 * 从用户的AK,SK生成鉴权签名(Access Token)
42 *
43 * @return 鉴权签名(Access Token)
44 * @throws I
返回说明
返回参数
| 字段 | 类型 | 备注 | 说明 |
|---|---|---|---|
| log_id | uint64 | 唯一的log id,用于问题定位 | |
| error_code | int | 错误码 | |
| error_msg | string | 错误描述信息 | |
| result | dict | 返回的结果列表 | |
| +data_block | list | 整合result字符串 | |
| ++is_end | bool | true:尾包(结束信号) false:中间包 |
|
| ++index | int | 包序号:从0开始(0,1,2,3……) | |
| ++answer_content | string | 各流式包的单题的答案与解析 | |
| +content | string | 仅非流式场景存在 | 整合的完整单题的答案与解析 |
输出示例
1{
2 "log_id": 1234567890123456789,
3 "error_code": 0,
4 "error_msg": "",
5 "result": {
6 "data_block": [
7 {
8 "is_end": false,
9 "index": 0,
10 "answer_content": "【考点分析】:"
11 }
12 ]
13 }
14}
1{
2 "log_id": 1234567890123456789,
3 "error_code": 0,
4 "error_msg": "",
5 "result": {
6 "data_block": [
7 {
8 "is_end": false,
9 "index": 0,
10 "answer_content": "【考点分析】:本题考查乘法分配律的逆运用,"
11 },
12 {
13 "is_end": true,
14 "index": 1,
15 "answer_content": "属于整数四则混合运算中的简便计算考点。"
16 }
17 ],
18 "content": "【考点分析】:本题考查乘法分配律的逆运用,属于整数四则混合运算中的简便计算考点。"
19 }
20}
评价此篇文章
