We’re constantly being bombarded with images of celebrities and athletes on TV and in social media. If you’re anything like me, you probably have a hard time keeping all the names straight.

Never fear, machine learning is here to save the day once again! Clarifai provides image recognition APIs that allow you to categorize, moderate, and predict the subjects in photos and even videos with computer vision and artificial intelligence. This tutorial will walk you through using the Clarifai API to tag images of celebrities so you can keep up with all the latest office gossip — no memorizing names required.

Prerequisites

For this tutorial, we’re going to be using the Clarifai API. If you haven’t heard of Clarifai before, go over and check it out. They provide several machine learning models for you to make predictions against, including celebrities, demographics, and food. To use the API, you’ll need to sign up for a free account.

Once you’ve signed up, you’ll need to create an application and generate an API key. You can do so on the sidebar menu once you’ve logged in.

First, create an application:

create a new application

I selected ‘whosthatstar’ as my app name, but you can use whatever is easy to remember. I left the Base Workflow as ‘General’ so we’ll have access to a wide variety of machine learning models.

Select ‘Create App’ and it initializes the application and generates an API key for you.

Take note of your key in the ‘API Keys’ section, we’ll need it.

Install Clarifai

For the purposes of this tutorial, we’re going to use the Python package for the Clarifai API. They have bindings for JS, Python, Java, Objective-C, and cURL, so you should be able to adapt it to your preferred language.

Ensure you have Python 3.x installed, then proceed to install the package using virtualenv and pip:

These commands will give you a folder to store your work, an isolated environment to install packages, and install the Clarifai libraries.

Now we’re ready to get started coding.

Setup and Auth

Since we’re using Python for this tutorial, the first thing we need to do is create a Python file for our code. I created whosthatstar.py to go along with our application’s name.

We’ll need to import the Clarifai libraries and let it know about our API key:

Remember, you can access your API key from the developer dashboard on Clarifai’s website.

Apply the Model and Predictions

Next, we need to tell Clarifai what model to use. You can see a full list of their Models on their site, and you can even build and train your own. For the purposes of this tutorial, I am going to use the Celebrity model. Hopefully the variety of different models on Clarifai’s website gives you some ideas about models that you could build with your own data.

Now we need some images to test with. You can find your own images on social media, IMDB, or Google, just make sure they have some relatively well-known celebrities included. Clarifai’s Celebrity Model includes over 10,000 people, so you should be able to find matches for most stars you throw at it.

For my example, I’ll this picture of cast members from Game of Thrones, but you can use any photo you like. You can use video as well, but that is beyond the scope of this computer vision tutorial.

Make sure you point to a publicly accessible URL for your image. I uploaded the sample image to Imgur but you can host it or link it from wherever is convenient.

The model.predict line is what actually runs the code on the backend and returns results to us. We can manipulate these results for whatever the next step in the application is, but right here we’re just going to print out the full response so you can see what the API returns.

result is a JSON object and to make it easier to read at the command line, we import the pprint (pretty print) library and use that to print the result JSON blob.

You can read more about the full output of the API methods in the documentation, but here’s a snippet of what’s returned when we run it against that Game of Thrones image:

output from running the code

I’ve truncated a lot of the less-probability matches, but for each person in the photo you can see the top match (which turns out to be correct for all four actors) and the confidence the system has in their guess. As you can see, it’s pretty sure about these particular stars!

Also in the Clarifai Celebrity model are bounding boxes so you can mark off where certain faces occur. An interesting project would be to replace all detected faces in an image with a funny sticker or overlay. With the Clarifai API, this is definitely doable.

Here’s the reference page for the Celebrity model again if you want to learn more about what you can do with the API. Once you’re ready to level up your skills, you can use the Explorer on Clarifai’s site and interactively test and create new models.

Have fun, play around with the JSON responses, and let us know what you create!

The following two tabs change content below.
mm

Al Nelson

Al is a geek about all things tech. He's a professional technical writer and software developer who loves writing for tech businesses and cultivating happy users. You can find him on the web at http://www.alnelsonwrites.com or on Twitter as @musegarden.