【转发】百度图片翻译API,对开发者真的很友好

百度图片翻译API结合OCR及机器翻译两项技术,支持对图片中包含的文本内容进行识别并翻译成指定的目标语言。您只需传入图片,即可轻松将图片中包含的文本内容翻译为指定语言,真正实现图片翻译一步到位!

 

四大功能亮点,全面满足您的需求

图片文字识别领先:多场景、多语种、高精度的整图文字检测和识别服务,多项ICDAR指标居世界第一,可识别中、英、法、日、韩等20种语言。

支持译文实景回填:原文识别后擦除图片原文区域,并对译文进行回填,实现输入原图,直接输出译文图片,体验一步到位的图片翻译服务。

机器翻译准确率高:采用百度领先的神经网络机器翻译模型,翻译准确率行业领先。

API调用方式灵活:接入方便灵活,适用移动端、Web端,以及多种操作系统集成,快速上手,简单易用。

 

只需三步,即可调用API接口

1. 使用您的百度账号登录百度AI开放平台(https://ai.baidu.com/),完成开发者认证;

2. 进入控制台后,左上角选择产品服务-人工智能-机器翻译,随后在页面选择开通图片翻译服务(还可以领取一万次免费测试资源哦!);

 

3. 创建应用,获得AppID及API Key,参考技术文档编写代码。

 

使用案例

如下是一张照片:

 

返回格式如下:

{
"error_code":"0",
"error_msg":"success",
"data":{
"from":"zh",
"to":"en",
"content":[
{
"src":"这是一个测试 ",
"dst":"This is a test.",
"rect":"79 23 246 43",
"lineCount":1,
"pasteImg":"xxx",
"points":[{"x":254,"y":280},{"x":506,"y":278},{"x":506,"y":303},{"x":254,"y":305}]
},
{
"src":"这是一个例子 ",
"dst":"This is an example.",
"rect":"79 122 201 37",
"lineCount":1,
"pasteImg":"xxx",
"points":[{"x":254,"y":280},{"x":506,"y":278},{"x":506,"y":303},{"x":254,"y":305}]
}
],
"sumSrc":"这是一个测试 这是一个例子 ",
"sumDst":"This is a test. This is an example."
"pasteImg":"xxx"
}
}


请求代码示例

提示一:使用示例代码前,请记得替换其中的示例Token、图片地址或Base64信息。

提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。

<?php
/**
* Send post request.
*
* @param string $url request url
* @param string $imgPath 本地图片地址
* @param string $from 翻译源语种
* @param string $to 翻译目标语种
* @param int $paste 贴合类型 0-不贴合 1-整图贴合 2-块区贴合
* @return mixed
*/
function sendPostRequest(string $url, string $imgPath, string $from, string $to, int $paste = 0)
{
$header = ['Content-Type' => 'multipart/form-data'];
$formData = [
'from' => $from,
'to' => $to,
'v' => 3,
'paste' => $paste,
'image' => '@' . realpath($imgPath) . ';type=image/jpeg',
];
if (class_exists('CURLFile')) {
$formData['image'] = new CURLFile(realpath($imgPath));
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $formData);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
curl_close($ch);

return $res;
}

$token = '[调用鉴权接口获取的token]';// example: xxx
$url = 'https://aip.baidubce.com/file/2.0/mt/pictrans/v1?access_token=' . $token;
$imgPath = '本地文件绝对路径'; // example: /xx/xx.jpg
$from = '源语种方向'; // example: zh
$to = '目标语种方向'; // example: en
$paste = 1; //图片贴合类型
$res = sendPostRequest($url, $imgPath, $from, $to, $paste);
var_dump($res);

 

了解更多

  • 更多产品信息,请点击:https://ai.baidu.com/tech/mt/pic_trans
  • 更多优惠活动,请点击:https://ai.baidu.com/support/news
  • 更多问题和建议,欢迎发邮件至translate_api@baidu.com,或加入百度翻译开发者QQ群(1046556714)沟通交流。

 

————————————————
原文链接:https://blog.csdn.net/weixin_40289030/article/details/119995899

 

posted @ 2021-08-30 17:31  老吴的技术知识园  阅读(580)  评论(0编辑  收藏  举报