Unlocking Image Similarity with Cognitive Actions from DreamSim

25 Apr 2025
Unlocking Image Similarity with Cognitive Actions from DreamSim

In the realm of artificial intelligence, the ability to evaluate and compare images is becoming increasingly vital. The DreamSim API provides developers with powerful Cognitive Actions that facilitate perceptual image similarity analysis. This blog post will guide you through the Calculate Perceptual Image Similarity action, detailing how you can integrate it to enhance your applications. By leveraging these pre-built actions, developers can save time and resources while achieving advanced image comparison capabilities.

Prerequisites

Before diving into the integration of Cognitive Actions, ensure you have the following setup:

  1. API Key: A valid API key from the DreamSim service is essential for authentication. You will need to pass this key as a Bearer token in the headers of your API requests.
  2. Internet Access: Since you'll be working with image URLs, a stable internet connection is necessary for fetching and processing images.

Cognitive Actions Overview

Calculate Perceptual Image Similarity

The Calculate Perceptual Image Similarity action enables you to determine how similar a reference image is to a set of other images. This action is particularly useful for tasks like image retrieval, duplicate detection, and similarity-based recommendations. It combines low-level image metrics with high-level human judgment to deliver accurate results.

Input

The input for this action requires a structured JSON object with the following fields:

  • imageUrls: A string containing a comma-separated list of image URLs. The first URL is treated as the reference image, and the subsequent URLs are the images to be compared against.
  • urlSeparator: An optional string to specify the character used to separate the URLs, with a default value of a comma.
Example Input
{
  "imageUrls": "https://bflapistorage.blob.core.windows.net/public/c8145aa6e5894cbd815d6ce708fea9f2/sample.jpg,https://replicate.delivery/czjl/dTjOfyxTTSQyc65ushgtv9O9z4oLoI4gVOW3Y7bHIgcFLhyJA/output.webp"
}

Output

The output is a JSON array where each entry contains:

  • distances: An object mapping each test image URL to its similarity score relative to the reference image (lower values indicate greater similarity).
  • reference: The URL of the reference image.
Example Output
[
  {
    "distances": {
      "https://replicate.delivery/czjl/dTjOfyxTTSQyc65ushgtv9O9z4oLoI4gVOW3Y7bHIgcFLhyJA/output.webp": 0.44552165269851685
    },
    "reference": "https://bflapistorage.blob.core.windows.net/public/c8145aa6e5894cbd815d6ce708fea9f2/sample.jpg"
  }
]

Conceptual Usage Example (Python)

Below is a conceptual Python code snippet demonstrating how to invoke the Calculate Perceptual Image Similarity action:

import requests
import json

# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute" # Hypothetical endpoint

action_id = "9f09857d-9919-479a-9161-fc7b1137fa87"  # Action ID for Calculate Perceptual Image Similarity

# Construct the input payload based on the action's requirements
payload = {
    "imageUrls": "https://bflapistorage.blob.core.windows.net/public/c8145aa6e5894cbd815d6ce708fea9f2/sample.jpg,https://replicate.delivery/czjl/dTjOfyxTTSQyc65ushgtv9O9z4oLoI4gVOW3Y7bHIgcFLhyJA/output.webp"
}

headers = {
    "Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
    "Content-Type": "application/json"
}

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json={"action_id": action_id, "inputs": payload}  # Hypothetical structure
    )
    response.raise_for_status()  # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully:")
    print(json.dumps(result, indent=2))

except requests.exceptions.RequestException as e:
    print(f"Error executing action {action_id}: {e}")
    if e.response is not None:
        print(f"Response status: {e.response.status_code}")
        try:
            print(f"Response body: {e.response.json()}")
        except json.JSONDecodeError:
            print(f"Response body: {e.response.text}")

In this code snippet:

  • Replace YOUR_COGNITIVE_ACTIONS_API_KEY with your actual API key.
  • The payload variable holds the necessary input as per the action's requirements.
  • The action ID is specified to identify which action to execute.

Conclusion

The Cognitive Actions provided by DreamSim offer an invaluable resource for developers looking to integrate advanced image similarity features into their applications. By utilizing the Calculate Perceptual Image Similarity action, you can enhance user experiences through effective image comparison. Consider exploring other potential use cases or actions that DreamSim offers to further enrich your applications. Happy coding!