如何运行 Google Vision API标签检测和安全搜索检测在Node.js中的单个请求中?

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

每月最多 5,000,000 个请求的 Google Vision API pricing page states that Safe Search Detection is free with Label Detection。对我来说,这意味着应该有一种方法可以在单个请求中 运行 进行标签检测和安全搜索检测,但我找不到该怎么做。

我正在使用 Node.js client provided by Google.

从 Node.js 程序调用 Google Vision API

当调用 safeSearchDetection 时,只传递一个图像路径作为参数,我得到了安全的搜索结果,但是一个空的标签数组 (labelAnnotations),如下所示。

代码片段:

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

输出:

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

当我调用 labelDetection 函数时,情况正好相反。我们得到有效的 labelAnnotations,但我们得到 null 作为 safeSearchAnnotation.

如何使用 Node.js 客户端调用 运行 安全搜索检测,同时返回 labelAnnotations

我怕打两个电话(一个是标签检测,一个是搜索安全检测),会被扣两次。

事实证明,确实可以通过一次请求同时获得标签和安全搜索结果(在这种情况下,安全搜索是免费的,每月最多 500 万次请求)。

我们只需要使用annotateImage function, passing an AnnotateImageRequest参数,指定标签检测和安全搜索检测作为特征:

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);

通过这样做,result 包括 labelAnnotationssafeSearchAnnotation