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
        }
      ]
    }
  ]
}
 
16
Kudos
 
16
Kudos

Now read this

Load balancing WebSockets with ELB and nginx on EC2

Following this guide will allow you to create an Elastic Load Balancer for a WebSocket application with support for SSL and the ability to read client IP addresses. Setup an Elastic Load Balancer Enable TCP forwarding on port 80. If you... Continue →