1

I am using Spring org.springframework.cloud.gcp.vision.CloudVisionTemplate for detecing labels in an image using Google Vision API like below:

AnnotateImageResponse annotateImageResponse = cloudVisionTemplate.analyzeImage(imageResource,
                Feature.Type.LABEL_DETECTION);
        annotationList = annotateImageResponse.getLabelAnnotationsList();

By default it seems like, it only fetches 10 results, but we need all the labels detected. I went through the Google API doc and it mentions about a parameter maxResults.

But this is described using REST API. I am looking how to set this maxResults using spring CloudVisionTemplate in Java or Google Vision Java SDK.

Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37
pankaj
  • 1,643
  • 4
  • 22
  • 35

1 Answers1

0

I haven't found this limit of 10 result in any documentation, however I tested it on Try this API in the documentation. When I removed "maxResults": 5, from the request, it indeed returned 10 result. I tired with "maxResults": 100 and returned more than 30 results.

In Java API it's seems to be possible, but not using CloudVisionTemplate. It's implementation is available on github and it is not extremely sophisticated... We can see there that Feature is build only with type from list provided as parameter (in line 128):

Feature.newBuilder().setType(featureType).build()

Now in Java API reference we may find that this Feature.builder contains method setMaxResults. So you can customize cloudVisionTemplate or create your own code with something like:

Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).setMaxResults(100).build()

Unfortunately I do not have environment to test this. If you will try please share the result. I don't think it will be possible to set unlimited number as max, but probably you are able to choose number large enough.

vitooh
  • 4,132
  • 1
  • 5
  • 16
  • @vitooh - Yes, this way we solved it, but it is surprising that Spring does not have support for setting max_results parameter. – pankaj Jul 26 '21 at 16:28