Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Google Cloud Vision API client for nodejs to detect multiple types

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!

like image 295
Kaan Dönbekci Avatar asked Dec 13 '22 17:12

Kaan Dönbekci


1 Answers

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.

like image 131
dsesto Avatar answered Dec 21 '22 09:12

dsesto