2016年11月21日 星期一

用 Google Cloud Vision API 辨認文字


今日繼續嘗試 Google Cloud Vision API v1 我在 Apple 網頁抓了上面的圖片,把它上傳到 Google Cloud Storage。然後用 APIs Explorer 進行文字辨認。
Request
POST https://vision.googleapis.com/v1/images:annotate?key={YOUR_API_KEY}
{
   "requests": [{
      "features": [{
         "type": "TEXT_DETECTION"
      }],
      "image": {
         "source": {
            "gcsImageUri": "gs://dummy/macbook_pro_en.png"
         }
      }
   }]
}

ResponseResponse
{
   "responses": [{
      "textAnnotations": [{
         "locale": "la",
         "description": "MacBook Pro\nA touch of genius.\n",
         "boundingPoly": {
            "vertices": [{
               "x": 126,
               "y": 47
            }, {
               "x": 410,
               "y": 47
            }, {
               "x": 410,
               "y": 122
            }, {
               "x": 126,
               "y": 122
            }]
         }
      }, {
         "description": "MacBook",
         "boundingPoly": {
            "vertices": [{
               "x": 175,
               "y": 47
            }, {
               "x": 306,
               "y": 47
            }, {
               "x": 306,
               "y": 73
            }, {
               "x": 175,
               "y": 73
            }]
         }
      }, {
         "description": "Pro",
         "boundingPoly": {
            "vertices": [{
               "x": 320,
               "y": 47
            }, {
               "x": 362,
               "y": 47
            }, {
               "x": 362,
               "y": 73
            }, {
               "x": 320,
               "y": 73
            }]
         }
      }, {
         "description": "A",
         "boundingPoly": {
            "vertices": [{
               "x": 126,
               "y": 79
            }, {
               "x": 141,
               "y": 79
            }, {
               "x": 141,
               "y": 122
            }, {
               "x": 126,
               "y": 122
            }]
         }
      }, {
         "description": "touch",
         "boundingPoly": {
            "vertices": [{
               "x": 157,
               "y": 79
            }, {
               "x": 243,
               "y": 79
            }, {
               "x": 243,
               "y": 122
            }, {
               "x": 157,
               "y": 122
            }]
         }
      }, {
         "description": "of",
         "boundingPoly": {
            "vertices": [{
               "x": 258,
               "y": 79
            }, {
               "x": 286,
               "y": 79
            }, {
               "x": 286,
               "y": 122
            }, {
               "x": 258,
               "y": 122
            }]
         }
      }, {
         "description": "genius.",
         "boundingPoly": {
            "vertices": [{
               "x": 296,
               "y": 79
            }, {
               "x": 410,
               "y": 79
            }, {
               "x": 410,
               "y": 122
            }, {
               "x": 296,
               "y": 122
            }]
         }
      }]
   }]
}

英文文字認得不錯,那中文又如何?我也試了一次。效果很好:
Request
POST https://vision.googleapis.com/v1/images:annotate?key={YOUR_API_KEY}
{
   "requests": [{
      "features": [{
         "type": "TEXT_DETECTION"
      }],
      "image": {
         "source": {
            "gcsImageUri": "gs://dummy/macbook_pro_tc.png"
         }
      }
   }]
}

Response
{
   "responses": [{
      "textAnnotations": [{
         "locale": "zh-Hant",
         "description": "MacBook Pro\n天才橫溢,一觸而發\n亢。\no\n",
         "boundingPoly": {
            "vertices": [{
               "x": 110,
               "y": 52
            }, {
               "x": 426,
               "y": 52
            }, {
               "x": 426,
               "y": 115
            }, {
               "x": 110,
               "y": 115
            }]
         }
      }, {
         "description": "MacBook",
         "boundingPoly": {
            "vertices": [{
               "x": 171,
               "y": 52
            }, {
               "x": 300,
               "y": 52
            }, {
               "x": 300,
               "y": 75
            }, {
               "x": 171,
               "y": 75
            }]
         }
      }, {
         "description": "Pro",
         "boundingPoly": {
            "vertices": [{
               "x": 311,
               "y": 52
            }, {
               "x": 355,
               "y": 52
            }, {
               "x": 355,
               "y": 75
            }, {
               "x": 311,
               "y": 75
            }]
         }
      }, {
         "description": "天才",
         "boundingPoly": {
            "vertices": [{
               "x": 110,
               "y": 82
            }, {
               "x": 176,
               "y": 82
            }, {
               "x": 176,
               "y": 115
            }, {
               "x": 110,
               "y": 115
            }]
         }
      }, {
         "description": "橫溢",
         "boundingPoly": {
            "vertices": [{
               "x": 178,
               "y": 82
            }, {
               "x": 244,
               "y": 82
            }, {
               "x": 244,
               "y": 114
            }, {
               "x": 178,
               "y": 114
            }]
         }
      }, {
         "description": ",",
         "boundingPoly": {
            "vertices": [{
               "x": 254,
               "y": 96
            }, {
               "x": 258,
               "y": 96
            }, {
               "x": 258,
               "y": 104
            }, {
               "x": 254,
               "y": 104
            }]
         }
      }, {
         "description": "一",
         "boundingPoly": {
            "vertices": [{
               "x": 277,
               "y": 96
            }, {
               "x": 307,
               "y": 96
            }, {
               "x": 307,
               "y": 99
            }, {
               "x": 277,
               "y": 99
            }]
         }
      }, {
         "description": "觸",
         "boundingPoly": {
            "vertices": [{
               "x": 311,
               "y": 83
            }, {
               "x": 342,
               "y": 83
            }, {
               "x": 342,
               "y": 114
            }, {
               "x": 311,
               "y": 114
            }]
         }
      }, {
         "description": "而",
         "boundingPoly": {
            "vertices": [{
               "x": 345,
               "y": 84
            }, {
               "x": 376,
               "y": 84
            }, {
               "x": 376,
               "y": 114
            }, {
               "x": 345,
               "y": 114
            }]
         }
      }, {
         "description": "發",
         "boundingPoly": {
            "vertices": [{
               "x": 380,
               "y": 83
            }, {
               "x": 410,
               "y": 83
            }, {
               "x": 410,
               "y": 114
            }, {
               "x": 380,
               "y": 114
            }]
         }
      }, {
         "description": "亢",
         "boundingPoly": {
            "vertices": [{
               "x": 395,
               "y": 91
            }, {
               "x": 409,
               "y": 92
            }, {
               "x": 409,
               "y": 102
            }, {
               "x": 395,
               "y": 101
            }]
         }
      }, {
         "description": "。",
         "boundingPoly": {
            "vertices": [{
               "x": 419,
               "y": 94
            }, {
               "x": 426,
               "y": 94
            }, {
               "x": 426,
               "y": 102
            }, {
               "x": 419,
               "y": 102
            }]
         }
      }, {
         "description": "o",
         "boundingPoly": {
            "vertices": [{
               "x": 419,
               "y": 95
            }, {
               "x": 425,
               "y": 95
            }, {
               "x": 425,
               "y": 103
            }, {
               "x": 419,
               "y": 103
            }]
         }
      }]
   }]
}

沒有留言: