图片翻译
更新时间:2022-10-28
接口描述
基于业界领先的深度学习技术及翻译技术,提供多场景、多语种、高精度的整图识别+翻译服务。您只需传入图片,即可识别图片中的文字并进行翻译。
请求说明
请求示例
HTTP 方法:POST
请求URL:https://aip.baidubce.com/file/2.0/mt/pictrans/v1
URL参数:
接口类型 | 描述 |
---|---|
access_token | 通过API Key和Secret Key获取的access_token,参考“Access Token获取” |
Header如下:
参数 | 值 |
---|---|
Content-Type | multipart/form-data |
Body中放置请求参数,参数详情如下:
请求参数
字段名 | 类型 | 是否必填 | 描述 |
---|---|---|---|
image | FILES | Y | 请求翻译的图片数据 |
from | TEXT | Y | 源语种方向 |
to | TEXT | Y | 译文语种方向 |
v | INT | Y | 固定值:3 |
paste | INT | N | 图片贴合类型:0 - 关闭文字贴合 1 - 返回整图贴合 2 - 返回块区贴合 |
注:
1)图片类型使用image/jpeg、image/png,注意均为小写。图片大小不超过4M,最短边至少30px,最长边最大4096px,长宽比3:1以内。
2)image使用FILE协议图片上传Content-Type=mutipart/form-data。
返回参数
参数 | 描述 | 备注 | 类型 | |
---|---|---|---|---|
error_code | 返回错误码 | 0 成功 详情查看错误码列表 | int | |
error_msg | 错误信息 | success | ||
data | 返回数据 | 数据结构类似app拍照翻译,去掉一些返回参数 | object | |
from | 源语种方向 | 支持语种方向见下方 | string | |
to | 目标语种方向 | 支持语种方向见下方 | string | |
content | 分段翻译内容 | 包含每一段识别内容详情及定位信息 | array | |
sumSrc | 未分段翻译原文 | 多段之间,请用\n拼接 | string | |
sumDst | 未分段翻译译文 | 多段之间,请用\n拼接 | string | |
pasteImg | 图片贴合 (整屏贴合) | paste=1有效,base64格式 | string |
content数据结构
参数 | 描述 | 备注 |
---|---|---|
src | 识别原文 | |
dst | 翻译译文 | |
rect | 原文擦除矩形位置 | 格式:"rect":"0 0 321 199"矩形的位置信息,依次顺序left, top, wide, high (以图片左上角顶点为坐标原点) |
points | 译文贴合矩形位置 | (坐标0点为左上角),坐标顺序左上,右上,右下,左下 |
lineCount | 合并行数 | 表示该分段信息是原文的多少行合并在一起 |
pasteImg | 分段贴合图片 | paste=2有效,base64格式 |
支持语种方向
语言 | 标识 |
---|---|
自动检测 | auto(目标语言不可设置为auto) |
英语 | en |
中文 | zh |
日语 | jp |
韩语 | kor |
葡萄牙语 | pt |
法语 | fra |
德语 | de |
意大利语 | it |
西班牙 | spa |
俄语 | ru |
荷兰语 | nl |
马来语 | may |
丹麦语 | dan |
瑞典语 | swe |
印尼语 | id |
波兰语 | pl |
罗马尼亚语 | rom |
土耳其语 | tr |
希腊语 | el |
匈牙利语 | hu |
错误码列表
接口流控及鉴权错误码
错误码 | 错误信息 | 描述 | 解决方法 |
---|---|---|---|
1 | Unknown error | 未知错误 | 请重试 |
2 | Service temporarily unavailable | 服务处理超时 | 请重试 |
4 | Open api request limit reached | 集群超限额 | 请重试 |
6 | no permission to access data | 没有接口权限 | 请确认您调用的接口已经被赋权。企业认证生效时间为1小时左右,使用需要企业认证的服务,请等待生效后重试 |
18 | Open api qps request limit reached | QPS超限额 | 请降低您的调用频率 |
19 | Open api total request limit reached | 请求总量超限额 | 请检查当前可用字符/次数包额度 |
100 | Invalid parameter | 请求参数不合法 | 请检查请求参数是否正确,可能的原因是token拉取失败,无效的access token参数等” |
110 | Access token invalid or no longer valid | Access Token失效 | token有效期为30天,注意需要定期更换,也可以每次请求都拉取新token |
111 | Access token expired | Access token过期 | token有效期为30天,注意需要定期更换,也可以每次请求都拉取新token |
通用及业务错误码
错误码 | 错误信息 | 描述 | 解决方法 |
---|---|---|---|
0 | Success | 成功 | |
52001 | TIMEOUT | 请求超时 | 重试 |
52002 | SYSTEM ERROR | 系统错误 | 重试 |
54000 | PARAM_FROM_TO_OR_Q_EMPTY | 必填参数为空或固定参数有误 | 检查参数是否误传 |
54003 | Invalid Access Limit | 访问频率受限 | 请降低您的访问频率 |
54005 | Long query overflow, retry later | 长query请求频繁 | 请降低长query的发送频率,3s后再试 |
58001 | INVALID_TO_PARAM | 译文语言方向不支持,或译文语言字段有误 | 请检查译文语言是否在语言列表里 |
69001 | picture fail | 上传图片数据有误 | 检查图片是否有问题 |
69002 | picture recognition time out | 图片识别超时 | 重试 |
69003 | picture recognition fail | 内容识别失败 | 更换图片重试 |
69004 | picture recognition empty | 识别内容为空 | 更换图片重试 |
69005 | picture size limit 4M | 图片大小超限(超过4M) | 更换图片重试 |
69006 | picture px error | 图片尺寸不符合标准(最短边至少30px,最长边最大4096px) | 更换图片重试 |
69007 | picture format error | 图片格式不支持 | 更换图片重试 |
69012 | Invalid param paste type | 文字贴合参数异常 | 请检查参数 paste,枚举示例:0-关闭文字贴合 1-返回整图贴合 2-返回块区贴合 |
使用案例
如下是一张照片:
返回格式
Plain Text
1{
2 "error_code":"0",
3 "error_msg":"success",
4 "data":{
5 "from":"zh",
6 "to":"en",
7 "content":[
8 {
9 "src":"这是一个测试 ",
10 "dst":"This is a test.",
11 "rect":"79 23 246 43",
12 "lineCount":1,
13 "pasteImg":"xxx",
14 "points":[{"x":254,"y":280},{"x":506,"y":278},{"x":506,"y":303},{"x":254,"y":305}]
15 },
16 {
17 "src":"这是一个例子 ",
18 "dst":"This is an example.",
19 "rect":"79 122 201 37",
20 "lineCount":1,
21 "pasteImg":"xxx",
22 "points":[{"x":254,"y":280},{"x":506,"y":278},{"x":506,"y":303},{"x":254,"y":305}]
23 }
24 ],
25 "sumSrc":"这是一个测试 这是一个例子 ",
26 "sumDst":"This is a test. This is an example."
27 "pasteImg":"xxx"
28 }
29}
请求代码示例
提示一:使用示例代码前,请记得替换其中的示例Token、图片地址或Base64信息。
提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。
1<?php
2/**
3 * Send post request.
4 *
5 * @param string $url request url
6 * @param string $imgPath 本地图片地址
7 * @param string $from 翻译源语种
8 * @param string $to 翻译目标语种
9 * @param int $paste 贴合类型 0-不贴合 1-整图贴合 2-块区贴合
10 * @return mixed
11 */
12function sendPostRequest(string $url, string $imgPath, string $from, string $to, int $paste = 0)
13{
14 $header = ['Content-Type' => 'multipart/form-data'];
15 $formData = [
16 'from' => $from,
17 'to' => $to,
18 'v' => 3,
19 'paste' => $paste,
20 'image' => '@' . realpath($imgPath) . ';type=image/jpeg',
21 ];
22 if (class_exists('CURLFile')) {
23 $formData['image'] = new CURLFile(realpath($imgPath));
24 }
25
26 $ch = curl_init();
27 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
28 curl_setopt($ch, CURLOPT_POST, 1);
29 curl_setopt($ch, CURLOPT_POSTFIELDS, $formData);
30 curl_setopt($ch, CURLOPT_URL, $url);
31 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
32 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
33 $res = curl_exec($ch);
34 curl_close($ch);
35
36 return $res;
37}
38
39$token = '[调用鉴权接口获取的token]';// example: xxx
40$url = 'https://aip.baidubce.com/file/2.0/mt/pictrans/v1?access_token=' . $token;
41$imgPath = '本地文件绝对路径'; // example: /xx/xx.jpg
42$from = '源语种方向'; // example: zh
43$to = '目标语种方向'; // example: en
44$paste = 1; //图片贴合类型
45$res = sendPostRequest($url, $imgPath, $from, $to, $paste);
46var_dump($res);
1curl -i -k 'https://aip.baidubce.com/file/2.0/mt/pictrans/v1?access_token=[调用鉴权接口获取的token]'
1# python ocr_translate.py <image>
2#
1import com.mashape.unirest.http.HttpResponse;
2import com.mashape.unirest.http.Unirest;
3
4import java.io.File;
5
6
7class Demo {
8 /**
9 unirest下载地址https://mvnrepository.com/artifact/com.mashape.unirest/unirest-java
10 */
11 public static String pictrans() {
12 // 请求url
13 String url = "https://aip.baidubce.com/file/2.0/mt/pictrans/v1";
14 try {
15 String param = "{\"id\": \"kR7z8nOMLV7prmE194Po\"}";
16 // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
17 String accessToken = "";
18 Unirest.setTimeouts(0, 0);
19 HttpResponse<String> response = Unirest.post("https://aip.baidubce.com/file/2.0/mt/pictrans/v1?access_token=" + accessToken)
20 .field("image", new File("/Users/xxxx/Downloads/033102.jpeg"))
21 .field("from", "zh")
22 .field("to", "cht")
23 .field("v", "3")
24 .asString();
25 String result = response.getBody();
26 System.out.println(result);
27 return result;
28 } catch (Exception e) {
29 e.printStackTrace();
30 }
31 return null;
32 }
33 public static void main(String[] args) {
34 pictrans();
35 }
36}
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/file/2.0/mt/pictrans/v1";
7
8int pictrans(std::string &json_result, const std::string &access_token) {
9 std::string url = request_url + "?access_token=" + access_token;
10 CURL *curl = NULL;
11 CURLcode result_code;
12 int is_success;
13 curl = curl_easy_init();
14 if (curl) {
15 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
16 curl_easy_setopt(curl, CURLOPT_URL, url.data());
17 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
18 curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
19 struct curl_slist *headers = NULL;
20 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
21 curl_mime *mime;
22 curl_mimepart *part;
23 mime = curl_mime_init(curl);
24 part = curl_mime_addpart(mime);
25 curl_mime_name(part, "image");
26 curl_mime_filedata(part, "/Users/xxx/demo.jpeg");
27 part = curl_mime_addpart(mime);
28 curl_mime_name(part, "from");
29 curl_mime_data(part, "zh", CURL_ZERO_TERMINATED);
30 part = curl_mime_addpart(mime);
31 curl_mime_name(part, "to");
32 curl_mime_data(part, "en", CURL_ZERO_TERMINATED);
33 part = curl_mime_addpart(mime);
34 curl_mime_name(part, "v");
35 curl_mime_data(part, "3", CURL_ZERO_TERMINATED);
36 curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
37 json_result = curl_easy_perform(curl);
38 curl_mime_free(mime);
39 curl_easy_cleanup(curl);
40 is_success = 0;
41 } else {
42 fprintf(stderr, "curl_easy_init() failed.");
43 is_success = 1;
44 }
45 return is_success;
46}
1using System;
2using System.IO;
3using System.Net;
4using System.Text;
5using System.Web;
6using RestSharp;
7
8namespace com.baidu.ai
9{
10 public class Demo
11 {
12 public static string pictrans()
13 {
14 string token = "[调用鉴权接口获取的token]";
15 string host = "https://aip.baidubce.com/file/2.0/mt/pictrans/v1?access_token=" + token;
16 var client = new RestClient(host);
17 client.Timeout = -1;
18 var request = new RestRequest(Method.POST);
19 request.AddFile("image", "/Users/xxx/demo.jpeg");
20 request.AddParameter("from", "zh");
21 request.AddParameter("to", "en");
22 request.AddParameter("v", "3");
23 IRestResponse response = client.Execute(request);
24 string result = response.Content;
25 Console.WriteLine(result);
26 return result;
27 }
28 }
29}