How to Compare Images for Similarity with Deep Learning AI using Python
Generally speaking, there are two common ways to detect if a pair of images are similar: 1) by comparing perceptual hashes or 2) by using deep learning AI to distinguish differences. The free-to-use API provided below allows you to compare two images through the latter method, requiring both image files in your request. You can easily take advantage of this API with the ready-to-run Python code examples provided below.
First things first, install the Python SDK with the below command:
pip install cloudmersive-image-api-client
With installation out of the way, copy in the remaining code below to import & call the function:
from __future__ import print_function
from cloudmersive_image_api_client.rest import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_image_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# create an instance of the API class
api_instance = cloudmersive_image_api_client.RecognizeApi(cloudmersive_image_api_client.ApiClient(configuration))
base_image = '/path/to/inputfile' # file | Image file to compare against. Common file formats such as PNG, JPEG are supported.
comparison_image = '/path/to/inputfile' # file | Image to compare to the base image.
recognition_mode = 'recognition_mode_example' # str | Optional, specify the recognition mode; possible values are Normal, Basic and Advanced. Default is Normal. (optional)
# Compare two images for similarity
api_response = api_instance.recognize_similarity_compare(base_image, comparison_image, recognition_mode=recognition_mode)
except ApiException as e:
print("Exception when calling RecognizeApi->recognize_similarity_compare: %s\n" % e)
Now simply include a free-tier Cloudmersive API key with your request, and you’re good to go. To get one, visit our website and register a free account (please note: this provides a limit of 800 API calls per month & no commitments; once you reach that limit, your total will reset the following month).