首页 > 解决方案 > 谷歌视觉 api annotateImage 返回空

问题描述

我正在尝试使用 google vision api 对单个图像进行多种类型检测。但是它返回空响应。如果我使用特定方法做单一类型,它会起作用,例如labelDetection检测标签。

我在做什么错await client.annotateImage(request);?我尝试通过content以及filename在两种情况下定义它都失败了

    const client = new vision.ImageAnnotatorClient({ fallback: true, auth })
 
    const content = await readFileSync(imagePath, 'base64');

    const image = {
      source: {
        // Try filename / path
        filename: path.join(__dirname, '..', imagePath)
      },
      // Try base64
      content
    }
    const features = [
      {type: 'FACE_DETECTION'},
      {type: 'LABEL_DETECTION'},
      {type: 'SAFE_SEARCH_DETECTION'},
    ]
    const request = {
      image,
      features,
    };
    const labelResults = await client.labelDetection(imagePath);
    console.log(labelResults)
// RESPONSE
[
  AnnotateImageResponse {
    faceAnnotations: [],
    landmarkAnnotations: [],
    logoAnnotations: [],
    labelAnnotations: [
      [EntityAnnotation],
      [EntityAnnotation],
      [EntityAnnotation],
      [EntityAnnotation],
      [EntityAnnotation],
      [EntityAnnotation],
      [EntityAnnotation],
      [EntityAnnotation],
      [EntityAnnotation],
      [EntityAnnotation]
    ],
    localizedObjectAnnotations: [],
    textAnnotations: []
  }
]

    const result = await client.annotateImage(request);
    console.log(result)

// RESPONSE 
[
  AnnotateImageResponse {
    faceAnnotations: [],
    landmarkAnnotations: [],
    logoAnnotations: [],
    labelAnnotations: [],
    localizedObjectAnnotations: [],
    textAnnotations: []
  }
]

标签: javascripttypescriptgoogle-vision

解决方案


推荐阅读