iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 11
0
Everything on Azure

Azure Service 實作 ( Blockchain、AI、 Serverless Architecture)系列 第 11

11. 使用 Azure OCR 辨識圖片文字

OCR,全名為 Optical Character Recognition,用來辨識圖片中的字體。

目前 Azure OCR API 可偵測的語言如下:

unk (AutoDetect)
zh-Hans (ChineseSimplified)
zh-Hant (ChineseTraditional)
cs (Czech)
da (Danish)
nl (Dutch)
en (English)
fi (Finnish)
fr (French)
de (German)
el (Greek)
hu (Hungarian)
it (Italian)
ja (Japanese)
ko (Korean)
nb (Norwegian)
pl (Polish)
pt (Portuguese,
ru (Russian)
es (Spanish)
sv (Swedish)
tr (Turkish)
ar (Arabic)
ro (Romanian)
sr-Cyrl (SerbianCyrillic)
sr-Latn (SerbianLatin)
sk (Slovak)

可以到此網站進行 API 測試:https://eastasia.dev.cognitive.microsoft.com/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fc/console

填上相關參數:
https://ithelp.ithome.com.tw/upload/images/20181022/20112426e6clDSGSaz.png

回傳資料如下:
https://ithelp.ithome.com.tw/upload/images/20181022/20112426W3NfcVjMrB.png

程式範例:

const https = require("https");

const param =
  "?language=zh-Hant&detectOrientation=true";

const options = {
  host: "eastasia.api.cognitive.microsoft.com",
  port: 443,
  path: `/vision/v1.0/ocr${param}`,
  method: "POST",
  headers: {
    "Ocp-Apim-Subscription-Key": "填上金鑰"
  }
};

const req = https.request(options, res => {
  let chunk = '';
  let text = '';
  res.on("data", function(data) {
    chunk += data;
  });
  res.on("end", function(data) {
    let { lines } = JSON.parse(chunk).regions[0];
    lines.forEach(line => {
      let { words } = line;
      words.forEach(word => {
        text += word.text;
      })
    })
    console.log(text);
  });
});

req.on("error", e => {
  console.error(e);
});

req.write(
  JSON.stringify({
    url: "http://album.udn.com/community/img/PSN_PHOTO/sunnyvicky/f_5032764_1.jpg"})
);

req.end();

使用以下圖片測試:
http://album.udn.com/community/img/PSN_PHOTO/sunnyvicky/f_5032764_1.jpg

看看效果:
https://ithelp.ithome.com.tw/upload/images/20181022/20112426dPA7GiaCCl.png

如此我們就完成了識別圖片中的文字功能。


上一篇
10. 使用 Azure 影像辨識出圖片中的地標或明星
下一篇
12. 使用 Azure Custom Vision 來識別圖片內物體
系列文
Azure Service 實作 ( Blockchain、AI、 Serverless Architecture)30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言