Search code examples
javascriptnode.jsgoogle-cloud-vision

How to run Google Vision API label detection and safe search detection in a single request in Node.js?


The Google Vision API pricing page states that Safe Search Detection is free with Label Detection for up to 5,000,000 requests per month. To me, this implies that there should be a way to run both label and safe search detection in a single request, but I cannot find how to do it.

I am calling Google Vision API from a Node.js program, by using the Node.js client provided by Google.

When calling safeSearchDetection, passing only an image path as a parameter, I get the safe search results back, but an empty array for labels (labelAnnotations), as exemplified below.

Code snippet:

const visionClient = new vision.ImageAnnotatorClient();
const [ result ] = await client.safeSearchDetection('./resources/wakeupcat.jpg');
console.log ({ result });

Output:

{
  result: {
    faceAnnotations: [],
    landmarkAnnotations: [],
    logoAnnotations: [],
    labelAnnotations: [],
    textAnnotations: [],
    localizedObjectAnnotations: [],
    safeSearchAnnotation: {
      adult: "VERY_UNLIKELY",
      spoof: "VERY_UNLIKELY",
      medical: "VERY_UNLIKELY",
      violence: "VERY_UNLIKELY",
      racy: "VERY_UNLIKELY"
    }
  }
}

The other way around happens when I call the labelDetection function. We get valid labelAnnotations, but we get null as safeSearchAnnotation.

How can I make a call to run a safe search detection, using the Node.js client, in such a manner that the labelAnnotations are also returned?

I am afraid that if I make two calls (one for label detection and another for search safe detection), I will be billed twice.


Solution

  • As it turns out, it is indeed possible to make a single request to get both labels and safe search results (having safe search for free in this case, up to 5M monthly requests).

    We just need to use the annotateImage function, passing an AnnotateImageRequest parameter, specifying label detection and safe search detection as features:

    const visionClient = new vision.ImageAnnotatorClient();
    const visionRequest = {
      features: [
        { type: 'LABEL_DETECTION' },
        { type: 'SAFE_SEARCH_DETECTION' },
      ],
      image: { source: { filename: './resources/wakeupcat.jpg' } },
    };
    
    const [ result ] = await visionClient.annotateImage(request);
    

    By doing this, result includes labelAnnotations and safeSearchAnnotation.