Subscribed unsubscribe Subscribe Subscribe

Google Vision APIで画像解析してもらってみる

最近β版が公開された画像解析APIを使ってみます。

設定

コンソールプロジェクトの作成 or 選択
請求先の登録
Google Vision APIを有効にする
ブラウザーAPIキーの生成

https://cloud.google.com/vision/docs/getting-started?hl=ja

キーをenvに設定

echo export GOOGLE_VISION_API_KEY=XXXXXXXXXXXXXXXXXX >> ~/.bash_profile

画像をダウンロード

https://cloud.google.com/vision/docs/images/faulkner.jpg?hl=ja

base64エンコードしてみる

base64 faulkner.jpg > faulknerBase64Encoded.jpg

request.jsonの作成

{
  "requests":[
    {
      "image":{
        "content":"/9j/4AAQSkZJRgABAQEASABIAAD/4QCYRXhpZg..."
      },
      "features":[
        {
          "type":"LABEL_DETECTION",
          "maxResults":1
        }
      ]
    }
  ]
}

Vision APIを使ってみる

curl -v -k -s -H "Content-Type: application/json" https://vision.googleapis.com/v1/images:annotate?key=$GOOGLE_VISION_API_KEY --data-binary @request.json

* Adding handle: conn: 0xXXXXXXXXXXXXXXX
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0xXXXXXXXXXXXXXX) send_pipe: 1, recv_pipe: 0
* About to connect() to vision.googleapis.com port 443 (#0)
*   Trying 64.XXXXXXXXX...
* Connected to vision.googleapis.com (64.XXXXXXXXXXXX) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
* Server certificate: *.googleapis.com
* Server certificate: Google Internet Authority G2
* Server certificate: GeoTrust Global CA
> POST /v1/images:annotate?key=XXXXXXXXXXXXXXXXXXXXX HTTP/1.1
> User-Agent: curl/7.30.0
> Host: vision.googleapis.com
> Accept: */*
> Content-Type: application/json
> Content-Length: 222909
> Expect: 100-continue
> 
* Done waiting for 100-continue
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=UTF-8
< Vary: X-Origin
< Vary: Referer
< Date: Wed, 23 Mar 2016 13:06:34 GMT
* Server ESF is not blacklisted
< Server: ESF
< Cache-Control: private
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< X-Content-Type-Options: nosniff
< Alternate-Protocol: 443:quic,p=1
< Alt-Svc: quic=":443"; ma=2592000; v="31,30,29,28,27,26,25"
< Accept-Ranges: none
< Vary: Origin,Accept-Encoding
< Transfer-Encoding: chunked
< 
{
  "responses": [
    {
      "labelAnnotations": [
        {
          "mid": "/m/068hy",
          "description": "pet",
          "score": 0.98414
        }
      ]
    }
  ]
}
* Connection #0 to host vision.googleapis.com left intact

今回は単純な内容でAPIを投げていますが、APIの使い方さえ、理解すれば、解析の知識がなくても、画像を解析できそうです。
人間がしていたことをGoogle Vision API+プログラムですることが可能なようです。

https://cloud.google.com/vision/docs/requests-and-responses?hl=ja