Docs
GraphQL API
Search
Visual Search

Visual Search

In the world of images, it would be unfair if you could not search for them in your data. Unbody allows you to search images using Visual Search.

Visual Search operates by comparing a target image against a database to identify similar items. The search is conducted using the nearImage or nearMedia functions, depending on whether the search is specifically for images or for broader media content, respectively.

Understanding nearImage Search

When you perform a nearImage search, you provide a Base64-encoded string of your target image. I know you might be wondering what is Base64-encoding. Base64 is an encoding scheme that converts binary data into text format. The converted data is used to transfer media files over media that are designed to deal with textual data. This ensures the image can be included directly in the GraphQL query without needing to reference an external resource.

Performing the Search

The example given below demonstrates the nearImage search query.

{
  Get {
    Image(
      nearImage: {
        image: "base64_encoded_image_string"
        # Additional parameters...
      }
    ) {
      url
      description
      # Other image details...
    }
  }
}
  • Obtaining a Base64-encoded Image String There are many tools and libraries in various programming languages that help you encode your image to Base64-encoding. For example, you can use JavaScript to convert the image to Base64-encoding as shown in the examples below.

    function getBase64(file, callback) {
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = function () {
        const base64String = reader.result
          .replace("data:", "")
          .replace(/^.+,/, "");
        callback(base64String); // Pass the base64 string to the callback
      };
      reader.onerror = function (error) {
        console.log("Error: ", error);
      };
    }
     
    // Example usage:
    // Assuming you have a file input in your HTML: <input type="file" id="fileInput" />
    const fileInput = document.getElementById("fileInput");
     
    fileInput.addEventListener("change", (event) => {
      const file = event.target.files[0];
      getBase64(file, (base64) => {
        console.log(base64); // Here's your base64 string
      });
    });

    You can also use Python’s base64 library to perform the same task as shown in the example given below.

    import base64
     
    with open("path_to_your_image.jpg", "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read()).decode('utf-8')

    The encoded_string is the Base64-encoded string of the image. It can be used as the input to the image parameter in the nearImage search.

If you wish to learn more about Base64-encoding, follow the link Base64-encoding explained (opens in a new tab).

Parameters for nearImage Search

Let us have a look at the parameters you can use with the nearImage.

  • image (required): This is the Base64-encoded string of the image you're searching for.
  • certainty: It defined a threshold for the confidence of the model. It means the model has such confidence that the results are similar to the provided image (e.g. 0.8 for 80% certainty).
  • distance: It denotes how closely the results need to match the search image. The smaller number indicates a closer match.
  • limit: This parameter limits the maximum number of similar images that the search returns.

Example of a Full nearImage Query

{
  Get {
    Image(
      nearImage: {
        image: "base64_encoded_image_string",
        certainty: 0.8,
        distance: 0.5,
        limit: 10
      }
    ) {
      url
      description
    }
  }
}