How to Compare Images with Deep Learning AI or Perceptual Hashes in Nodejs

Cloudmersive
3 min readMay 24, 2024

--

If we’re tracking down image duplicates for copyright reasons — or if we’re simply interested in determining the degree of difference between two similar looking photos — we can easily handle our image comparison action by calling a few free APIs in our Nodejs application.

We can use a Deep Learning AI comparison method to analyze two images and generate a “similarity score” (between 0.0–1.0, with higher values indicating greater similarity), or we can use the tried-and-true perceptual hash method (used commonly in image copyright disputes) which involves comparing a baseline hash value for one image with the hash value of another image.

Baseline perceptual hash values remain the same through each iteration of a photo (i.e., when edits are made to an original photo), and comparing hashes shows how much another version of that photo has been edited.

Using the below code, we can easily take advantage of free APIs that perform each of the comparison methods described above, respectively. We can authorize our API calls with a single free API key (this allows a limit of 800 API calls per month with no additional commitments), and we can install the client SDK with a single NPM command.

Let’s start with client SDK installation. We can run the following NPM command:

npm install cloudmersive-image-api-client --save

Alternatively, we can add the Node client directly to our package.json:

  "dependencies": {
"cloudmersive-image-api-client": "^1.3.4"
}

Next, we can copy code examples to structure our API calls. We can replace the ‘YOUR API KEY’ placeholder string with our own API key string to take care of authorization.

Using the below code, we can call an API that compares images based on Deep Learning AI analysis. If we want, we can elect to customize the ‘recognitionMode’ parameter with a Basic, Normal or Advanced string value. Advanced will improve the quality of our image recognition operation (default is set to normal):

var CloudmersiveImageApiClient = require('cloudmersive-image-api-client');
var defaultClient = CloudmersiveImageApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveImageApiClient.RecognizeApi();

var baseImage = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Image file to compare against. Common file formats such as PNG, JPEG are supported.

var comparisonImage = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Image to compare to the base image.

var opts = {
'recognitionMode': "recognitionMode_example" // String | Optional, specify the recognition mode; possible values are Normal, Basic and Advanced. Default is Normal.
};

var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.recognizeSimilarityCompare(baseImage, comparisonImage, opts, callback);

Using the following code, we can compare the similarity between two perceptual image hashes. This assumes we already have hashes present:

var CloudmersiveImageApiClient = require('cloudmersive-image-api-client');
var defaultClient = CloudmersiveImageApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveImageApiClient.RecognizeApi();

var request = new CloudmersiveImageApiClient.ImageSimilarityHashDistanceRequest(); // ImageSimilarityHashDistanceRequest |


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.recognizeSimilarityHashDistance(request, callback);

We can use the following JSON model to structure our hash comparison request:

{
"ImageHash1": "string",
"ImageHash2": "string"
}

If we don’t have image hashes for the images we want to compare, we can use the following code to generate a hash for our images. We can once again set the ‘recognitionMode’ parameter here for more fault tolerance in our image analysis:

var CloudmersiveImageApiClient = require('cloudmersive-image-api-client');
var defaultClient = CloudmersiveImageApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveImageApiClient.RecognizeApi();

var imageFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.

var opts = {
'recognitionMode': "recognitionMode_example" // String | Optional, specify the recognition mode; possible values are Normal, Basic and Advanced. Default is Normal.
};

var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.recognizeSimilarityHash(imageFile, opts, callback);

And that’s all the code we’ll need! Now we can easily compare images in two distinct ways using just a few lines of code.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet