Establish an AI Operating Model and get out of prototype and into production
October 12, 2017

Search Images By Visual Similarity with the Clarifai API

Table of Contents:

When you’re searching for images, words are often not enough to find exactly what you need. Wouldn’t it be amazing if you could just show your computer a picture and say, “Find me images that look like this?” With the Clarifai API, you can search for any image by visual similarity – here’s how!

The Clarifai Search API has a variety of different ways for you to query your inputs. In one of our previous posts, we talked about searching your content by geo location. In this post, we will have an image do all of the talking. You won’t need to do any training on your dataset; simply upload images and then you can search over those images seamlessly.

Getting Started

For the first step, you need to have Python (version 3.6.2) installed for your appropriate operating system. You can head over to a terminal and write in python --version. If you happen to have a version of Python 2 installed you may need to try python3 --version. You should see a prompt come up reading: Python 3.6.2. If neither of these work, you may need to check if Python is included in your PATH environment.

Many people who develop in Python suggest to have a “virtual environment”. These help to manage any application-specific dependencies. Having one won’t be necessary for this tutorial but if you are curious you can read more about them in the Python documentation.

Now, install the official Python Clarifai client using pip install clarifai. The last thing you’ll need to do is make sure to sign up for a free Clarifai account and

create an application.

Adding your inputs

You will need to have a dataset of inputs to search against. These inputs get indexed on your application and Clarifai will be able to “see” the image. To a machine, an image is nothing but vectors that describe what each pixel looks like. When you perform a search using an image, Clarifai will look to see how close these vectors are to one another to determine if the images are visually similar. The visual similarity can be more effective if your dataset includes objects that are relevant to what you’d like to search against. If you were to add a dataset full of only food and try to search using an image of a dog your search results wouldn’t be as strong.

Here’s a dataset of images from ImageNet on food. You should see a file containing nothing but lines of URLs of images of food. Save this file as food-data.txt in the same directory as your code then we will take this file and upload the images in batches.

import os
from import ClarifaiApp
from import Image as ClImage

app = ClarifaiApp(api_key='YOUR_API_KEY')

FILE_NAME = 'food-data.txt'
FILE_PATH = os.path.join(os.path.curdir, FILE_NAME)

# Counter variables
current_batch = 0
counter = 0
batch_size = 32

with open(FILE_PATH) as data_file:
    images = [url.strip() for url in data_file]
    row_count = len(images)
    print("Total number of images:", row_count)

while(counter < row_count):
    print("Processing batch: #", (current_batch+1))
    imageList = []

    for current_index in range(counter, counter+batch_size - 1):
        except IndexError:


    counter = counter + batch_size
    current_batch = current_batch + 1

Wowza! Over one thousand images being uploaded effortlessly.

Jim Carey Drinking Coffee

Searching using an Image

Now the amazing part you’ve been waiting for is being able to search by visual similarity using an image. Let’s say you wanted to find out if there is anything similar to this picture of cookies. All you would need from here is:

from import ClarifaiApp

app = ClarifaiApp(api_key='YOUR_API_KEY')

# Search using a URL
search = app.inputs.search_by_image(url='')

for search_result in search:
    print("Score:", search_result.score, "| URL:", search_result.url)

The search_by_image() function returns a list of Image objects that wrap the response but we will have it print out the score and the url associated with it. You can also use image bytes or a filename to query against.

# Response
Score: 0.8486366 | URL:
Score: 0.79205513 | URL:
Score: 0.7901007 | URL:
Score: 0.741455 | URL:
Score: 0.7173992 | URL:
Score: 0.68365324 | URL:
Score: 0.6734046 | URL:
Score: 0.6723133 | URL:
Score: 0.66024935 | URL:
Score: 0.6529919 | URL:
Score: 0.6473093 | URL:
Score: 0.63564813 | URL:
Score: 0.6230167 | URL:
Score: 0.61543244 | URL:
Score: 0.61172754 | URL:
Score: 0.6057524 | URL:
Score: 0.60092676 | URL:
Score: 0.60023034 | URL:
Score: 0.5992471 | URL:
Score: 0.5968622 | URL:

The search response will show a value for score from 0 to 1. A score closer to 1 means the image is more visually similar; a score closer to 0 means the image is less visually similar. The response from Clarifai also defaults from the top 20 results. If you want to change, that you can add a different value for  the per_page parameter in the search_by_image() function.


You’re probably thinking to yourself, “That’s all it took?” and the answer is yes. The part that took the longest was getting the dataset. Otherwise, performing our search was only five lines of code. The Search Clarifai API doesn’t stop here:

If you have any questions, concerns, or even friendly notes feel free to reach out to us over at or comment below!