4

I am trying to use the @google-cloud/vision package to send requests to the Google Vision API. Is there a way to do multiple detections without doing something like:

client
  .labelDetection(link)
  .then(...)

client
  .safeSearchDetection(link)
  .then(...)

Thanks!

2 Answers2

9

The @google-cloud/vision package that you are using (or at least the latest version available as of writing, which is 0.19.x) offers a variety of methods, each of which performs a different type of operation.

In your question, you refer to the methods labelDetection and safeSearchDetection. These are specific methods that perform the label detection and the Safe Search detection implementations of the Cloud Vision API, respectively.

Instead of using those feature-specific methods, you are interested in using annotateImage instead, which annotates a single image with the features that you specify in the request.feature parameter. This parameter is an array object that can include as many features as you want.

Below there is a sample code that would perform Label Detection and Safe Search Detection over a single image in a single call:

const vision = require('@google-cloud/vision');
const client = new vision.ImageAnnotatorClient();

const request = {
  image: {source: {imageUri: 'gs://your/GCS/image.jpg'}},
  features: [{type: vision.types.Feature.Type.LABEL_DETECTION}, {type: vision.types.Feature.Type.SAFE_SEARCH_DETECTION}],
};

client
  .annotateImage(request)
  .then(...);

Finally, here you have the list of available feature types.

dsesto
  • 7,864
  • 2
  • 33
  • 50
3

Solved it this way for version 0.21.0

import * as vision from '@google-cloud/vision';
const visionClient = new vision.ImageAnnotatorClient();

const request = {
                "image": {
                    "source": {
                        "imageUri": imageUri
                    }
                },
                "features": [
                    {
                        "type": "FACE_DETECTION"
                    },
                    {
                        "type": "LABEL_DETECTION"
                    },
                    {
                        "type": "SAFE_SEARCH_DETECTION"
                    },
                    {
                        "type": "WEB_DETECTION"
                    },
                    {
                        "type": "CROP_HINTS"
                    },
                    {
                        "type": "IMAGE_PROPERTIES"
                    },
                    {
                        "type": "DOCUMENT_TEXT_DETECTION"
                    },
                    {
                        "type": "TEXT_DETECTION"
                    },
                    {
                        "type": "LOGO_DETECTION"
                    },
                    {
                        "type": "LANDMARK_DETECTION"
                    },
                    {
                        "type": "TYPE_UNSPECIFIED"
                    },
                    // Other detection types here...
                ]
        };

        return await visionClient.annotateImage(request).then((res) => {
            console.log(JSON.stringify(res));
        });

with the help of @dsesto

Yushin
  • 1,684
  • 3
  • 20
  • 36
  • 3
    While this code may answer the question, providing information on how and why it solves the problem improves its long-term value – L_J Jul 25 '18 at 07:22