1. 程式人生 > 其它 >【轉發】百度圖片翻譯API,對開發者真的很友好

【轉發】百度圖片翻譯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
  • 更多問題和建議,歡迎發郵件至[email protected],或加入百度翻譯開發者QQ群(1046556714)溝通交流。

————————————————
原文連結:https://blog.csdn.net/weixin_40289030/article/details/119995899