File path specification for images problem

I need to learn how to specify the file path for files at internal storage folders. I found at Github.com the next example to compare two images:

ImagingApi imagingApi = new ImagingApi(“clientSecret”, “clientId”);

// create search context or use existing search context ID if search context was created earlier
SearchContextStatus status = imagingApi.createImageSearch(
new CreateImageSearchRequest(null, null, null, null));
String searchContextId = status.getId();

// specify images for comparing (image ID is a path to image in storage)
String imageInStorage1 = “WorkFolder/Image1.jpg”;
String imageInStorage2 = “WorkFolder/Image2.jpg”;

// compare images
SearchResultsSet result = imagingApi.compareImages(
new CompareImagesRequest(
searchContextId, imageInStorage1, null, imageInStorage2, null, null));
Double similarity = result.getResults().get(0).getSimilarity();

When I created my account I also created my App and my Storage (as internal storage) where I added the folder Test0 to upload my files and I replaced in the code of the example this line:

String imageInStorage1 = “WorkFolder/Image1.jpg”; with
String imageInStorage1 = “Test0\Foto1.png”;

But when I run my class I receive the error:
javax.imageio.IIOException: Can’t read input file!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1310)
at com.sipsa.tast.demos.AsposeCompare0.main(AsposeCompare0.java:30)
com.aspose.imaging.cloud.sdk.invoker.ApiException: {“requestId”:null,“error”:{“code”:“internalError”,“message”:“Dynabic.Storage.Exceptions.HttpWebException: AmazonS3 Storage exception: The specified key does not exist. Bucket ‘aspose.cloud-filestorage-prod’, FilePath ‘950619/3f16b8c6-71b6-4613-a888-0c4ac8b1238a/Test0/Foto1.png’\r\n at Dynabic.Storage.Managers.AmazonS3Manager.RunAmazonCode[T](Func1 action) in C:\\build\\src\\submodules\\Dynabic.Storage\\src\\Dynabic.Storage\\Managers\\AmazonS3Manager.cs:line 856\r\n at Aspose.Imaging.Cloud.Core.Business.ResilientStorageManager.InvokeWithRetry[T](Func1 func, Func2 postCheck, Boolean retryFalseResult, Int32 retryCount) in C:\\build\\src\\Aspose.Imaging.Cloud.Core\\Business\\ResilientStorageManager.cs:line 434\r\n at Aspose.Imaging.Cloud.Core.Business.ResilientStorageManager.InvokeWithRetry[T](Func1 func, Func2 postCheck, Boolean retryFalseResult, Int32 retryCount) in C:\\build\\src\\Aspose.Imaging.Cloud.Core\\Business\\ResilientStorageManager.cs:line 455\r\n at Aspose.Imaging.Cloud.Core.Business.ResilientStorageManager.InvokeWithRetry[T](Func1 func, Func2 postCheck, Boolean retryFalseResult, Int32 retryCount) in C:\\build\\src\\Aspose.Imaging.Cloud.Core\\Business\\ResilientStorageManager.cs:line 455\r\n at Aspose.Imaging.Cloud.Core.Business.ResilientStorageManager.InvokeWithRetry[T](Func1 func, Func2 postCheck, Boolean retryFalseResult, Int32 retryCount) in C:\\build\\src\\Aspose.Imaging.Cloud.Core\\Business\\ResilientStorageManager.cs:line 455\r\n at Aspose.Imaging.Cloud.Core.Business.ResilientStorageManager.InvokeWithRetry[T](Func1 func, Func2 postCheck, Boolean retryFalseResult, Int32 retryCount) in C:\\build\\src\\Aspose.Imaging.Cloud.Core\\Business\\ResilientStorageManager.cs:line 455\r\n at Aspose.Imaging.Cloud.Core.Business.ResilientStorageManager.Download(String path, String versionId) in C:\\build\\src\\Aspose.Imaging.Cloud.Core\\Business\\ResilientStorageManager.cs:line 127\r\n at Aspose.Imaging.Cloud.AI.Business.SearchContextForCloud.GetImage(String id, IStorageManager fileManager) in C:\\build\\src\\Aspose.Imaging.Cloud.AI\\Business\\SearchContextForCloud.cs:line 134\r\n at Aspose.Imaging.Cloud.AI.Controllers.SearchContextController.<>c__DisplayClass5_0.<<CompareImages>b__0>d.MoveNext() in C:\\build\\src\\Aspose.Imaging.Cloud.AI\\Controllers\\SearchContextController.cs:line 230\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at Aspose.Imaging.Cloud.AI.Controllers.BaseSearchContextController.InSearchContextAsync[T](String methodName, String searchContextId, String folder, Func2 searchContextFunc, String storage, Boolean isContextChanged)”,“description”:“Operation Failed. Internal error.”,“dateTime”:“2023-04-13T10:46:13.9774562Z”,“innerError”:null}}
at com.aspose.imaging.cloud.sdk.invoker.internal.requesthandlers.ApiExceptionRequestHandler.throwApiException(ApiExceptionRequestHandler.java:98)
at com.aspose.imaging.cloud.sdk.invoker.internal.requesthandlers.ApiExceptionRequestHandler.processResponse(ApiExceptionRequestHandler.java:71)
at com.aspose.imaging.cloud.sdk.invoker.internal.ApiInvoker.readResponse(ApiInvoker.java:448)
at com.aspose.imaging.cloud.sdk.invoker.internal.ApiInvoker.invokeInternal(ApiInvoker.java:300)
at com.aspose.imaging.cloud.sdk.invoker.internal.ApiInvoker.invokeApi(ApiInvoker.java:106)
at com.aspose.imaging.cloud.sdk.api.ImagingApi.compareImages(ImagingApi.java:279)
at com.sipsa.tast.demos.AsposeCompare0.main(AsposeCompare0.java:70)
Exception in thread “main” java.lang.NullPointerException: Cannot invoke “com.aspose.imaging.cloud.sdk.model.SearchResultsSet.getResults()” because “result” is null
at com.sipsa.tast.demos.AsposeCompare0.main(AsposeCompare0.java:77)

@juanvi

If you are not passing the storage name in the API call explicitly, then please ensure you have set the default storage of your application in the aspose.cloud dashboard. Please also check the documentation for more details to compare two images.

Tilal Ahmad:

I think it will be good to notice you about the version I’m using in my tests (maybe it is the reason for some of the problems I have): as it is recommended on page GitHub - aspose-imaging-cloud/aspose-imaging-cloud-java: Java library for communicating with the Aspose.Imaging Cloud API I updated my pom with aspose-imaging-cloud v22.4. I have been testing the example for Java in your documentation (Compare Two Images|Documentation) and as first issue: the class PostSearchContextCompareImagesRequest used in the example is not recognized.

Second issue: Once I changed this class with CompareImagesRequest class and changed also the method to perform the comparision from postSearchContextCompareImages to compareImages, it worked without errors and uploaded the files to the root directory in the default Cloud Storage, but compared the files only if they are under that root directory, it looks like if it wasn’t taking into account the parameter folder when I populated it with the only folder name in that storage and moved there the files to compare.

This is my code:

		String DATA_PATH = "C:\\DATOS\\Fotos\\";

	String imageInStorage1 = "Foto1.png";
	String imageInStorage2 = "Foto2.png";

	try {
	  // Upload both images to Cloud Storage
	  String[] images = {
	    imageInStorage1,
	    imageInStorage2
	  };
	  for (String imageName: images) {

	    File inputFile = new File(DATA_PATH + imageName);
	    FileInputStream localInputImageStream = new FileInputStream(inputFile);

	    byte[] localInputImage = IOUtils.toByteArray(localInputImageStream);

	    UploadFileRequest uploadFileRequest = new UploadFileRequest(imageName, localInputImage, null);
	    FilesUploadResult result = imagingApi.uploadFile(uploadFileRequest);
	  }

	  // Compare two images
	  // create search context or use existing search context ID if search context was created earlier
	  SearchContextStatus status = null;
	  try {
	    status = imagingApi.createImageSearch(
	      new CreateImageSearchRequest(null, null, null, null));
	  } catch (Exception e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	  }
	  String searchContextId = status.getId();
	  //String searchContextId = "5728a4ed-12bb-4b4a-8dce-38295735549f";
	  String folder = "Test0"; // Folder with image to process
	  String storage = null; // We are using default Cloud Storage
	  imageInStorage1 = "Foto1.png";
	  imageInStorage2 = "Foto3.png";

	  //		    SearchResultsSet result = imagingApi.postSearchContextCompareImages(
	  SearchResultsSet result = imagingApi.compareImages(
	    //		            new PostSearchContextCompareImagesRequest(
	    new CompareImagesRequest(
	      searchContextId, imageInStorage1, null, imageInStorage2, folder, storage));
	  Double similarity = result.getResults().get(0).getSimilarity();
	  System.out.println("Images Similarity: " + similarity.toString());
	} catch (Exception e) {
	  System.out.println(e.getMessage());
	}

@juanvi

Thanks for your feedback. We are looking into the issue and will update you shortly.

@juanvi

I have noticed that images inside a folder do not work as expected. So I logged ticket IMAGINGCLOUD-815 for further investigation. We will keep you updated about the issue resolution progress within this forum thread.

Secondly, we will review and update the code examples in the documentation.