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

How SWFWire works

This is an overview of a decompiler and a debugger I made in Flash a few years ago. This is mainly for my own reference. There are three parts of SWFWire: SWFWire Decompiler - A library for SWF and ABC decompilation SWFWire Inspector -... Continue →