Search code examples
node.jsgoogle-cloud-vision

Google cloud vision api 10 Limit results (nodejs)


I have read that the result limits are set to 10 by default, but Im not sure where to change this in the tutorial code provided by google.

vision.webDetection({ source: { filename: fileName } })
  .then((results) => {
    const webDetection = results[0].webDetection;
    if (webDetection.fullMatchingImages.length) {
      console.log(`Full matches found: ${webDetection.fullMatchingImages.length}`);
      webDetection.fullMatchingImages.forEach((image) => {
        console.log(`  URL: ${image.url}`);
        console.log(`  Score: ${image.score}`);
      });
    }
  })
  .catch((err) => {
    console.error('ERROR:', err);
  });

https://cloud.google.com/vision/docs/detecting-web


Solution

  • Try the following:

    const bucketName = 'Bucket where the file resides, e.g. my-bucket';
    const fileName = 'Path to file within bucket, e.g. path/to/image.png';
    
    const request = {
        source: {
            imageUri: `gs://${bucketName}/${fileName}`
        },
        features: [{
            maxResults: 10 // change this result
        }]
    };
    vision.webDetection(request)
        .then((results) => {
            const webDetection = results[0].webDetection;
            if (webDetection.fullMatchingImages.length) {
                console.log(`Full matches found: ${webDetection.fullMatchingImages.length}`);
                webDetection.fullMatchingImages.forEach((image) => {
                    console.log(`  URL: ${image.url}`);
                    console.log(`  Score: ${image.score}`);
                });
            }
        })
        .catch((err) => {
            console.error('ERROR:', err);
        });
    

    Ref: maxResults