Find Similar Images Introduction
This article explains how to find similar images. First, you will upload a group of images to Cloud Storage then use the API to find similar images.
The API URL is: GET /imaging/ai/imageSearch/{searchContextId}/findSimilar
Resource URI
Swagger UI lets you call Aspose.Imaging REST APIs directly from the browser. The description of the APIs and their parameters are also given there.
cURL Example
SDKs
Using an SDK (API client) is the quickest way for a developer to speed up the development. An SDK takes care of a lot of low-level details of making requests and handling responses and lets you focus on writing code specific to your particular project. Check out our GitHub repository for a complete list of Aspose.Imaging SDKs along with working examples, to get you started in no time.
SDK Examples
.NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-imaging-cloud/aspose-imaging-cloud-dotnet
public void FindSimilarImages()
{
// Create new search context
string searchContextId = this.CreateImageSearch();
string ImageToFind = "4.jpg";
string ImagesPath = @"FindSimilar\";
string findImageId = ImagesPath + ImageToFind;
double? similarityThreshold = 3; // The similarity threshold
int? maxCount = 3; // The maximum count
string folder = null; // Path to input files
string storage = null; // We are using default Cloud Storage
// Upload images to Cloud Storage
string[] images = { "1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg",
"9.jpg", "10.jpg"};
foreach (string imageName in images)
{
// Upload local image to Cloud Storage
using (FileStream localInputImage = File.OpenRead(ImagingBase.PathToDataFiles + ImagesPath + imageName))
{
var uploadFileRequest = new UploadFileRequest(ImagesPath + imageName, localInputImage);
FilesUploadResult result = this.ImagingApi.UploadFile(uploadFileRequest);
}
}
this.CreateImageFeatures(ImagesPath.TrimEnd('\\'), true, searchContextId);
SearchResultsSet searchResultsSet = this.ImagingApi.FindSimilarImages(
new FindSimilarImagesRequest(searchContextId, similarityThreshold, maxCount, imageId: findImageId, folder: folder, storage: storage));
Console.WriteLine("Results Count: " + searchResultsSet.Results.Count);
// Delete the search context
this.DeleteImageSearch(searchContextId);
}
Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String APP_KEY = ""; // Get AppKey and AppSID from https:dashboard.aspose.cloud/
String APP_SID = ""; // Get AppKey and AppSID from https:dashboard.aspose.cloud/
String DATA_PATH = "src/main/resources/";
ImagingApi imagingApi = new ImagingApi(APP_KEY, APP_SID);
String fileName = "aspose-logo.jpg";
try {
// Upload image to Cloud Storage
File inputFile = new File(DATA_PATH + fileName);
FileInputStream localInputImageStream = new FileInputStream(inputFile);
byte[] localInputImage = IOUtils.toByteArray(localInputImageStream);
UploadFileRequest uploadFileRequest = new UploadFileRequest(fileName, localInputImage, null);
FilesUploadResult result = imagingApi.uploadFile(uploadFileRequest);
// Find similar images
String searchContextId = "5728a4ed-12bb-4b4a-8dce-38295735549f";
Double similarityThreshold = 90.0;
Integer maxCount = 5;
String folder = null; // Folder with image to process
String storage = null; // We are using default Cloud Storage
SearchResultsSet results = imagingApi.getSearchContextFindSimilar(
new GetSearchContextFindSimilarRequest(
searchContextId, similarityThreshold, maxCount, null, fileName, folder, storage));
// process search results
for (SearchResult searchResult : results.getResults())
{
System.out.println("ImageName: " + searchResult.getImageId() +
", Similarity: " + searchResult.getSimilarity());
}
} catch (Exception e) {
System.out.println(e.getMessage());
}