DEV Community

Mitch
Mitch

Posted on

Python Celebrity Recognizer

Let's use the python requests library, and a free trial of Microsoft's computer vision API to identify celebrities in photos.

This short tutorial is aimed at showing developers how accessible and user friendly cognitive services can be. This tutorial is taken largely inpart from Microsoft's Cognitive Services documentation, it can be found here.

To start, your going to need a free subscription key from here.

First, we need to import the requests library

import requests
Enter fullscreen mode Exit fullscreen mode

Then we need the keys, and some basic URL's. We're going to use these values from the computer vision API documentation.

#subscription key
key = "your key here"

#Base endpoint and special celebrity endpoint
vision_base_url = "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/"
celebrity_analyze_url = vision_base_url + "models/celebrities/analyze"
Enter fullscreen mode Exit fullscreen mode

Next we need an image of a celebrity.I'll start with the king of pop.
Micheal Jackson

image_url = "http://images2.fanpop.com/image/photos/10700000/Close-Up-Large-Photo-michael-jackson-10731676-1267-1333.jpg"
Enter fullscreen mode Exit fullscreen mode

We're going to need some basic HTTP variables set up. These values are standard and have been taken from the documentation.

h = {'Ocp-Apim-Subscription-Key':key}
p = {'visualFeatures': 'Categories,Description,Color'}
d = {'url':image_url}
Enter fullscreen mode Exit fullscreen mode

We finally send the request to the service.

response = requests.post(celebrity_analyze_url,headers=h,params=p,json=d)
analysis = response.json()
Enter fullscreen mode Exit fullscreen mode

Let's look at our results:

result = analysis["result"]["celebrities"][0]["name"]
print(result)
Enter fullscreen mode Exit fullscreen mode

Micheal Jackson

Comment which celebrities the service can and can't recognize.

I've got the first one, this service can't recognize Prince...

Top comments (0)