如何使用Google Cloud Vision API客户端来节点检测多种类型

Kaa*_*kci 3 node.js google-cloud-platform google-vision

我正在尝试使用@ google-cloud/vision软件包向Google Vision API发送请求.有没有办法在不执行以下操作的情况下执行多次检测:

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

client
  .safeSearchDetection(link)
  .then(...)
Run Code Online (Sandbox Code Playgroud)

谢谢!

dse*_*sto 6

您正在使用的@google-cloud/vision程序包(或者至少是写作时可用的最新版本0.19.x)提供了多种方法,每种方法都执行不同类型的操作.

在您的问题中,您可以参考方法labelDetectionsafeSearchDetection.这些是分别执行Cloud Vision API 的标签检测安全搜索检测实施的特定方法.

您可以使用annotateImage而不是使用这些特定于功能的方法,而是使用您在request.feature参数中指定的功能来注释单个图像.此参数是一个数组对象,可以包含任意数量的功能.

下面是一个示例代码,可以在单个调用中对单个图像执行标签检测和安全搜索检测:

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(...);
Run Code Online (Sandbox Code Playgroud)

最后,您可以此处获得可用功能类型的列表.