On this page we will use the Microsoft Cognitive API to famous landmarks. In this case we pass an image to the Azure Cloud and it returns a guess for a famous landmark
Image Recognition of landmarks using Microsoft Cognitive Services |
Source code
The following outlines the Python code used:
import httplib, urllib, base64 from pprint import pprint import json import sys text='https://upload.wikimedia.org/wikipedia/commons/thumb/9/93/Clock_Tower_-_Palace_of_Westminster%2C_London_-_May_2007.jpg/480px-Clock_Tower_-_Palace_of_Westminster%2C_London_-_May_2007.jpg' if (len(sys.argv)>1): text=str(sys.argv[1]) headers = { # Request headers. Replace the key below with your subscription key. 'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': 'YOUR KEY', } params = urllib.urlencode({ # Request parameters. All of them are optional. 'visualFeatures': 'Categories', 'details': 'Landmarks', 'language': 'en', }) body = "{'url':\'"+text+"\'}" try: conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com') conn.request("POST", "/vision/v1.0/analyze?%s" % params, body, headers) response = conn.getresponse() data = response.read() conn.close() d1=json.loads(data) print "My guess is: ",d1['categories'][0]['detail']['landmarks'][0]['name'] print print 'Score: %3s \"%s\" ' % (d1['categories'][0]['detail']['landmarks'][0]['confidence'],d1['categories'][0]['detail']['landmarks'][0]['name']) print print 'Data:' print(d1) print "Loading:" +body except KeyError: pass
A sample JSON return is:
My guess is: Big Ben Score: 0.997175 "Big Ben" Data: {u'requestId': u'87440fcc-8c4d-4a09-a62c-557211a2aef3', u'categories': [{u'score': 0.76171875, u'name': u'building_', u'detail': {u'landmarks': [{u'confidence': 0.997175, u'name': u'Big Ben'}]}}], u'metadata': {u'width': 480, u'format': u'Jpeg', u'height': 957}} Loading:{'url':'https://upload.wikimedia.org/wikipedia/commons/thumb/9/93/Clock_Tower_-_Palace_of_Westminster%2C_London_-_May_2007.jpg/480px-Clock_Tower_-_Palace_of_Westminster%2C_London_-_May_2007.jpg'}