Search code examples
node.jsgoogle-cloud-functionsgoogle-cloud-vision

Can't change MaxResults on Cloud Vision Labeling


I'm using Google Vision to detect photos on an image and the results are limited to the top 10. I want to extend this number to 50. How do I do that?

Here is my code:

const client = new vision.ImageAnnotatorClient();

  for (const file of allFiles) {
    if (file.metadata.contentType.startsWith("image/")) {
      const [resultImageLabel] = await client.labelDetection(
        `gs://${bucketName}/${file.name}`
      );

      const labels = resultImageLabel.labelAnnotations;
      const faceDetection = resultFaceDetection.faceAnnotations;

      const labelArray = [];
      const labelScore = {};

      // image Labelling
      for (const label of labels) {
        labelArray.push(label.description);
        labelScore[label.description] = label.score;
      }

      console.log(labelArray);
      console.log(labelScore);
    }
  }

The answers that i've seen are in JSON format so i added at the beginning of the for loop:

const request = {
      image: {
        source: { imageUri: `gs://${bucketName}/${file.name}` },
      },
      features: [
        {
          maxResults: 50,
        },
      ],
    };

// Changed from image path to request
const [resultImageLabel] = await client.labelDetection(request);

Then I get the following error:

functions: Error: Setting explicit features is not supported on this method. Use the #annotateImage method instead.

Looked through the "#annotateImage" documentation and I don't see what I'm missing.

When I comment out the "features" section of the code, everything works as before.

This is running in an https Firebase Cloud Function.


Solution

  • For your requirements,you can consider the following code below as an example:

    const client = new vision.ImageAnnotatorClient();
    async function testing(){
    
    const allFiles = [
    { name: 'gs://bucket_name/file1.jpeg', metadata: { contentType: 'image/jpeg' } },
    ];
    
    for (const file of allFiles) {
    if (file.metadata.contentType.startsWith("image/")) {
    const request = {
    image: {
    source: { imageUri: file.name },
    },
    features: [
    {
    type: 'LABEL_DETECTION',
    maxResults: 50,
    },
    ],
    };
    
    const [resultImageLabel] = await client.annotateImage(request);
    const labels = resultImageLabel.labelAnnotations;
    const labelArray = [];
    const labelScore = {};
    
    for (const label of labels) {
    labelArray.push(label.description);
    labelScore[label.description] = label.score;
    }
    console.log(labelArray);
    console.log(labelScore);
    }
    } 
    } 
    
    testing();
    

    Sample image used :https://unsplash.com/photos/wfwUpfVqrKU

    From the error message it is clear that `features’ is not supported on the labelDetection method so instead that method we have to use the ‘annotateImage’ method.You can find more information from these link1 and link2.