DEV Community

Cover image for VIDEO | How to implement Image Similarity Search with Python
Eden AI
Eden AI

Posted on • Originally published at edenai.co

VIDEO | How to implement Image Similarity Search with Python

What is Image Similarity Search API?

Image Similarity Search API is a powerful tool that allows developers to compare images based on their visual content and retrieve similar images from a database or the web. This technology leverages advanced algorithms to analyze the visual features of images, such as colors, textures, and shapes, and identify similarities between them.

Image Similarity Search Eden AI

Image Similarity Search‍How Does it Work?

The Image Similarity Search API works by extracting key features from an input image and comparing them with features from other images in a dataset. It employs techniques like deep learning and computer vision to understand the content of images and measure their similarity.

When a query image is provided to the API, it processes the image and generates a feature vector representing its visual characteristics. Then, it searches through a collection of images to find those with similar feature vectors. The similarity between images is typically measured using distance metrics like Euclidean distance or cosine similarity.‍

For an in-depth comparison of the top APIs for enhancing visual content analysis, delve into our article "Best Image Similarity Search Solutions of 2024".‍

Diverse Applications of Image Similarity Search

- E-commerce Optimization: Online retailers utilize image similarity search to offer personalized product recommendations based on visual similarities, enhancing user experience and driving sales.
- Efficient Content Management: Media companies and digital asset platforms employ image similarity search to organize and retrieve images efficiently, streamlining workflow and content categorization processes.
- Creative Inspiration in Art and Design: Artists and designers leverage image similarity search to discover visually similar images, artwork, or designs for inspiration, facilitating creative ideation and exploration.
- Security and Surveillance: Security agencies utilize image similarity search for suspect identification, object tracking, and pattern analysis across surveillance footage, enhancing crime prevention and investigation capabilities.

How to use Image Similarity Search on Eden AI

Step 1: Create an Account on Eden AI

To get started with the Eden AI API, you need to sign up for an account on the Eden AI platform. Once registered, you will get an API key that grants you access to the diverse set of image Similarity providers available on the platform.‍

Eden AI App
Get your API Key for FREE

Step 2: Choose Your Image Source

Before diving into the code, decide where your query image is located:

- File URL: If your image is hosted online, you'll use its URL.
- Local File: If your image is stored locally on your machine, you'll provide its file path.

Choose your file type Eden AI

Step 3: Get the Python Code Snippet

Now, let's get to the code. Depending on your image source choice, you'll use different code snippets.‍
Using File URL
If you're using a file hosted online, here's the Python code snippet:

import json
import requests

headers = {"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiOWRmYTBmMDEtOTZlNS00ZWVjLTlhMTEtODM4M2Y2YjM0ZTY2IiwidHlwZSI6ImFwaV90b2tlbiJ9.vxdZl0DF2xO9xOnpBwNNXv8XA3D5fOxTX-JEBNlNkqk"}

url = "https://api.edenai.run/v2/image/search/launch_similarity"
json_payload = {
    "providers": "sentisight",
    "file_url": "🔗 URL of your image"
}

response = requests.post(url, json=json_payload, headers=headers)

result = json.loads(response.text)
print(result["sentisight"])
Enter fullscreen mode Exit fullscreen mode


Ensure to replace "🔗 URL of your image" with the actual URL of your image. The image you specify here will be used as the query for the similarity search.‍

Using Local File

If your image is stored locally, use the following code snippet:

import json
import requests

headers = {"Authorization": "Bearer your_bearer_token_here"}

url = "https://api.edenai.run/v2/image/search/launch_similarity"
data = {"providers": "sentisight"}
files = {'file': open("🖼️ path/to/your/image.png", 'rb')}

response = requests.post(url, data=data, files=files, headers=headers)
result = json.loads(response.text)
print(result['sentisight'])
Enter fullscreen mode Exit fullscreen mode

Replace "🖼️ path/to/your/image.png" with the actual path to your image file. This image will serve as the query for the similarity search.
Additionally, you can change the value of "providers" in both codes to any supported provider on Eden AI you want to use for the image similarity search.

By following these steps, you can harness the power of Eden AI's Image Similarity Search API to find visually similar images with ease. Whether you are working with images hosted online or stored locally, Eden AI provides a seamless and efficient way to integrate image similarity search into your projects. Experiment with different providers and customize the search to suit your specific needs, making the most out of this powerful tool.‍

Adding New Images to Your Dataset for Image Similarity Search

In the previous tutorial, we learned how to use the Eden AI Image Similarity Search API to find similar images using a URL or local file. Now, by learning how to add new images to your dataset, you can continually update and refine your image library, making your similarity searches even more effective. Whether you are adding images from an online source or uploading them directly from your device, these steps will help you manage your dataset with ease.‍

Step-by-Step Guide

Original Code from Eden AI Documentation
Before we dive into the specific cases, here is the original code from Eden AI documentation:

import requests

url = "https://api.edenai.run/v2/image/search/upload_image"

payload = {
    "response_as_dict": True,
    "attributes_as_list": False,
    "show_original_response": False
}
headers = {
    "accept": "application/json",
    "content-type": "application/json",
    "authorization": "Bearer your_bearer_token_here"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)‍
Enter fullscreen mode Exit fullscreen mode

Adding Images via URL

When adding images via URL, you send the image URL to the API endpoint, which then processes and adds the image to your dataset.

import requests

url = "https://api.edenai.run/v2/image/search/upload_image"

payload = {
    "response_as_dict": True,
    "attributes_as_list": False,
    "show_original_response": False,
    "providers": "sentisight,nyckel",
    "image_name": "test.jpg",
    "file_url": "http://edenai-resource-example.jpg"
}
headers = {
    "accept": "application/json",
    "content-type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)
Enter fullscreen mode Exit fullscreen mode

Modified Code Example
Here is how you can modify the code to add an image via URL:

1. Payload Modifications: Add "providers", "image_name", and "file_url" to the payload.

  • Specify the providers you want to use.
  • Provide the name of the image (optional).
  • Specify the URL of the image you want to add to your dataset.

2. Header Modifications: Remove the "authorization" header since it's not required for URL uploads.
3. Request Modifications: Use the "requests.post" method with payload and headers to send the request to the API endpoint.

‍‍

Adding Images via Local File

When adding images from a local file, you need to send the file data directly to the API.

import requests

url = "https://api.edenai.run/v2/image/search/upload_image"

payload = {
    "response_as_dict": True,
    "attributes_as_list": False,
    "show_original_response": False,
    "providers": "sentisight",
    "image_name": "car5.jpeg"
}
headers = {
   "authorization": "Bearer dummy_token_for_demo_purposes"
}
files = {'file': open("./Assets/car3.jpeg", "rb")}

response = requests.post(url, data=payload, files=files, headers=headers)

print(response.text)
Enter fullscreen mode Exit fullscreen mode

Modified Code Example
Here is how you can modify the code to add an image via local file:‍
1. Payload Modifications: Add "providers" and "image_name" to the payload.

  • Specify the providers you want to use.
  • Provide the name of the image.
  1. Header Modifications:
  2. Ensure that the "authorization" header remains unchanged as it's still required for file uploads.
  3. Remove "accept" and "content-type" since it is not required for local file uploads.

  4. Specify the path to the local file you want to upload.
    4. Request Modifications: Use the "requests.post" method with both payload, files, and headers to send the request to the API endpoint.

    By following these steps, you can easily add new images to your dataset for image similarity search with Eden AI. Keeping your image library updated will enhance the accuracy and relevance of your searches, providing better results over time. Whether you're adding images via URL or local file, Eden AI's API simplifies the process, allowing you to focus on building and refining your application.

Video Tutorial

To help you visualize these steps, we have prepared a video tutorial demonstrating both how to run an image similarity search and how to add images to your dataset. Watch the video below to follow along and see the process in action:
Watch the video HERE

Benefits of using Eden AI's unique API

Using Eden AI API is quick and easy.‍

Multiple AI Engines in one API key

‍Save time and cost

We offer a unified API for all providers: simple and standard to use, with a quick switch that allows you to have access to all the specific features very easily (diarization, timestamps, noise filter, etc.).‍

Easy to integrate

The JSON output format is the same for all suppliers thanks to Eden AI's standardization work. The response elements are also standardized thanks to Eden AI's powerful matching algorithms.‍

Customization

With Eden AI you can integrate a third-party platform: we can quickly develop connectors. To go further and customize your API request with specific parameters, check out our documentation.‍

You can see Eden AI documentation here.

Next step in your project

The Eden AI team can help you with your Image Similarity Search integration project. This can be done by :

  • Organizing a product demo and a discussion to understand your needs better. You can book a time slot on this link: Contact
  • By testing the public version of Eden AI for free: however, not all providers are available on this version. Some are only available on the Enterprise version.
  • By benefiting from the support and advice of a team of experts to find the optimal combination of providers according to the specifics of your needs
  • Having the possibility to integrate on a third-party platform: we can quickly develop connectors.

C‍reate your Account on Eden AI

Top comments (0)