Using the Cloud Vision API on App Engine

Here’s a quick snippet of how to use the Cloud Vision API on App Engine. It’s pretty straight forward, the only tricky thing is making sure you set the Content-Type.

import json
from logging import info
from urllib import urlencode
from google.appengine.api import urlfetch

headers = {'Content-Type': 'application/json'}
url = 'https://vision.googleapis.com/v1/images:annotate?%s' % urlencode({
    'key': '<google_api_key>'
})
payload = json.dumps({
    'requests': [
        {
            'image': {
                'source': {
                    'gcsImageUri': 'gs://bucket/path/to/file.jpg'
                }
            },
            'features': [
                {'type': 'LABEL_DETECTION', 'maxResults': 10}
            ]
        }
    ]
})
response = urlfetch.fetch(url, deadline = 30, method = urlfetch.POST, headers = headers, payload = payload)
response_data = json.loads(response.content)
info(response_data)

The result should be something like this.

{
  "responses": [
    {
      "labelAnnotations": [
        {
          "mid": "/m/0215n",
          "description": "cartoon",
          "score": 0.83648151
        },
        {
          "mid": "/m/0919rx",
          "description": "line art",
          "score": 0.80135477
        },
        {
          "mid": "/m/0dgsmq8",
          "description": "artwork",
          "score": 0.71005219
        },
        {
          "mid": "/m/05h7rm",
          "description": "coloring book",
          "score": 0.62251049
        }
      ]
    }
  ]
}
 
20
Kudos
 
20
Kudos

Now read this

Setting up Auto Scaling on EC2

This post is specifically about Ubuntu, but the process is similar for other operating systems. You’ll need to have an S3 bucket, and know how to launch EC2 instances. Here’s a quick overview of the process: Create a bootstrap script... Continue →