You can’t store images directly in Algolia.
Instead, store the image on a content delivery network (CDN) or web server and add the image URL to a field in your .
When you retrieve a record from Algolia, use this URL to display the image in your app.
Why use image classification?
Retailers spend a lot of time building their catalogs. To offer relevant search and discovery, they often classify each item by hand, adding features like item type, material, or style. Removing some of this manual work lets you focus on what matters to your business, such as price, stock, and popularity. Visual recognition can pull this information automatically by analyzing each product image. It makes feature tagging more consistent. For example, you may have various names for the color “blue” within your product descriptions. Item descriptions could include “cerulean” or “sapphire,” but not “blue”. Without a consistent “blue” value, you might miss showing relevant products to users when they on or search for “blue” items. Image classification lets you add the “blue” tag consistently for all blue products. Image classification is especially valuable in consumer-to-consumer (C2C) marketplaces, where users may not describe products consistently or fully. Tags from image classification can increase the number of product attributes, making them more discoverable. Image classification helps beyond C2C marketplaces too. It’s useful anywhere your team tags features like “type,” “neckline,” and “sleeve length” by hand.What does image classification and tagging entail?
This guide shows how to use a third-party API or platform to classify images. Use these classifications to enrich your Algolia records. It provides examples for Google Cloud Vision API and ViSenze. The process is the same for other providers, like Amazon Rekognition. The goal is to enrich your records so that each one includes more descriptive text. This text comes from running the product image through an image classifier, which returns classifications. Another way of thinking of classifications is “tags” or “labels.” Adding these classifications to your Algolia records helps surface them in searches, whether users search with text or images. Enriching your records with classifications is a two-step process:- Image classification - sending image URLs to a third-party image recognition platform to retrieve classifications.
- Indexing - adding the relevant classification information to your Algolia records.
Platform considerations
Google Cloud Vision API is an all-purpose image recognition API. It draws from a large set of image data, so it can give a wide range of accurate classifications. The downside is that its classifications aren’t specialized or well structured. All-purpose image recognition platforms can introduce irrelevant classifications. An image of a model wearing a t-shirt could return relevant classifications, like “t-shirt,” along with the shirt’s color and style. But it could also return classifications like “neck” and “arm,” if these appear in the image. Google Cloud Vision API returns tags and confidence scores of all objects that it identifies in an image. If a platform exists for your use case, for example ViSenze for fashion retail, use the specialized platform instead of a general one. Platforms built for a specific case usually produce better classifications. These platforms tailor their classifications to industry terms and structure them consistently. For example, ViSenze takes an image of a model wearing a t-shirt. It identifies only fashion-related objects, excluding things like “neck” and “arm.” For each item it includes, such as “t-shirt,” it returns attributes like “neckline,” “fit,” and “sleeve length,” with values like “v-neck,” “trim,” and “short.” You can be sure that all shirt images retrieve these same attributes in the same structure.Before you begin
This tutorial requires a set of Algolia records, each with an image URL. You’ll also need access to an image recognition platform, such as Google Cloud Vision API. Algolia doesn’t search in your original data source, but in the data you index to Algolia. Algolia accepts and stores JSON data, meaning it doesn’t store image files. Instead, it’s common to index an image URL, so that you can display the image in your results.JSON
Image classification
Image classification takes an image and returns a set of classifications or labels for it. As AI advances, image classification keeps improving and becomes more accessible to non-experts. With platforms like Google Cloud Vision API or ViSenze, you often just send an image URL and get classifications back in the response.Using Google Vision API
If you haven’t already, create a Google Account and enable the Google Vision API for it. Set up authentication so that you can retrieve credentials and use the Vision API client library. The Google Vision API returns an array of classifications: JSON objects with different properties. Of these properties,description and score are the most useful.
The score shows how certain the API is about the description.
After initializing an instance of Google Cloud Vision’s Node.js client, you can write a function to retrieve labels from an image URL.
The example below creates a getImageLabels function that takes a public image URL, the Algolia record’s objectID, and a scoreLimit.
The scoreLimit is the threshold for how certain the platform must be about an object to include it in the classifications.
Since score is a number between 0 and 1, the scoreLimit should be between 0 and 1 too.
A higher scoreLimit means the API must be more certain about a label before it includes the label.
You can write a function to retrieve just these or any other attributes you find useful.
The getImageLabels example returns an object with a labels array.
The array contains only label descriptions and scores, where scores were higher than the scoreLimit.
The returned object also includes the original imageURL and objectID.
The objectID is important for sending this data to your Algolia index later.
JavaScript
JavaScript
When fetching images from HTTP URLs,
Google can’t guarantee that the request succeeds.
Your request might fail if the host denies it, for example due to request throttling or denial-of-service prevention.
It might also fail if Google throttles requests to the site to prevent abuse.
Google advises against depending on externally hosted images for production apps.
Using ViSenze
When using a case specific platform like ViSenze, the general idea is the same. Setup an account and credentials, and send public image URLs to their Recognition API to receive classifications. You need to tailor your function to the data structure the platform returns. For example, thegetImageLabels function below takes a public image URL, the Algolia record’s objectID, and a scoreLimit.
The scoreLimit is the threshold for how certain the platform must be about an object to include it in the classifications.
Since score is a number between 0 and 1,
the scoreLimit should be between 0 and 1 too.
A higher scoreLimit means the API must be more certain about a label before it includes the label.
The function returns an object with an objects array.
The objects array contains all identified objects, such as “t-shirt” or “belt,” along with their coordinates, labels, and scores.
The returned object also includes the original imageURL and objectID.
The objectID is important for sending this data to your Algolia index later.
JavaScript
JavaScript
Indexing image classifications
Once you’ve retrieved the classifications from your third-party image recognition platform, send them to Algolia. You can add classifications when you first index your data, or later with thebrowse method.
browse lets you retrieve and update your data as needed.
Using Google Vision API
This example uses thegetImageLabels function from the classification section to retrieve labels for each record with browse.
It then uses the partialUpdateObjects method to add the labels to the record.
labels attribute:
JSON
labels.description to your searchableAttributes.
To add search by image, or to filter on labels, include labels.description in attributesForFaceting.
Using ViSenze
This example uses thegetImageLabels function from the classification section to retrieve labels for each record with browse.
It then uses the partialUpdateObjects method to add the labels to the record.
It then updates the index settings to include each object’s labels in attributesForFaceting and searchableAttributes.
JSON