百度圖片翻譯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
