###### tags: `阿瑜` # Day 12 ## Day 12: ML APIs | Google Cloud ### Practice #### Quest-`Integrate with Machine Learning APIs` ##### Lab-`Extract, Analyze, and Translate Text from Images with the Cloud ML APIs` - ID: **GSP075** - **Cloud Vision API**'s text detection method - 使用 OCR 從圖片提取文字 - **Translation API** - 翻譯文字 - **Natural Language API** - 分析文字 ###### Steps: ###### 1. Create an API Key - 在`Cloud Console`中,依序點選 **Navigation Menu > APIs & services > Credentials** ![](https://i.imgur.com/qkvKiTJ.png) - 按 **+ Create Credentials** > 下拉選單選 API key ![](https://i.imgur.com/cF1CfkP.png) - **Copy key** 後按Close ![](https://i.imgur.com/8AbMV0j.png) - 把Key加入**環境變數** ```shell= export API_KEY=<YOUR_API_KEY> ``` ![](https://i.imgur.com/nDU6ZZ8.png) ![](https://i.imgur.com/iF4X2t6.png) ###### 2. Upload an `image` to a cloud storage bucket image: sign.jpg 為偵測目標 - Search Storage ![](https://i.imgur.com/wD35WzR.png) - Create Bucket ![](https://i.imgur.com/okpDYyY.png) - Setting ![](https://i.imgur.com/3LSdfvY.png) - Create - 上傳檔案到Cloud Stotage ![](https://i.imgur.com/mweCWZc.png) ![](https://i.imgur.com/0uZNkGp.png) - 設定檔案權限為公開 ![](https://i.imgur.com/hawzPqK.png) ![](https://i.imgur.com/eRXjsDe.png) ![](https://i.imgur.com/Fmf6XUJ.png) ###### 3. Create Vision API request ![](https://i.imgur.com/yaq17qZ.png) ```json= { "requests": [ { "image": { "source": { "gcsImageUri": "gs://my-bucket-name/sign.jpeg" } }, "features": [ { "type": "TEXT_DETECTION", "maxResults": 10 } ] } ] } ``` ![](https://i.imgur.com/rPdubTR.png) ###### 4. Call the Vision API's text detection method 在Cloud shell中,使用**curl** call the Vision API ![](https://i.imgur.com/R66g9Bb.png) ```shell= curl -s -X POST -H "Content-Type: application/json" --data-binary @ocr-request.json https://vision.googleapis.com/v1/images:annotate?key=${API_KEY} ``` 可以看到: [更正-my bucket name為project id] ![](https://i.imgur.com/3J4sTAf.png) ![](https://i.imgur.com/3LFHM8N.png) 由於輸出太長,可以輸出到文字檔內 ![](https://i.imgur.com/p42ZqVR.png) ![](https://i.imgur.com/HhJrdN5.png) ###### 5. Sending text from the image to the Translation API - 將圖片擷取的文字交由**Translation API**進行翻譯 - 建立translation-request.json ![](https://i.imgur.com/TR8fd7c.png) - 使用上一步驟輸出的文字擋xxxresponse.json ![](https://i.imgur.com/IKEef0S.png) - Call the Translation API ![](https://i.imgur.com/GTFFW8a.png) ###### 6. Analyzing the image's text with the Natural Language API ![](https://i.imgur.com/gTgN6t7.png) ###### Summary: 上傳圖片到Cloud Storage, 使用`Vision API`擷取文字, 將擷取出來的文字存到文字檔(.json), 接著使用`Translation API`可以知道是哪國語言, 並翻譯成target(範例為en:english), 同樣地把翻譯好的結果存在文字檔, 建立`Natural Language API` 的request JSON檔, 將翻譯好的存檔輸入剛建的request, 再call api得每個word的分析(分類)。