Firebase Cloud function & Cloud Vision API: TypeError: vision.detectText is not a function

Firebase Cloud function & Cloud Vision API: TypeError: vision.detectText is not a function

我尝试使用 Firebase 云功能中的 Cloud Vision API 对存储在 Firebase 存储中的图像进行 OCR。

我导入 Google Cloud vision 客户端库如下

const vision = require('@google-cloud/vision');

然后我打电话给

vision.detectText({ source: { imageUri: 'gs://xxxx.appspot.com/yyyy.JPG' } }) 

但是我得到一个错误

TypeError: vision.detectText 不是函数

最初我使用

vision.textDetection({ source: { imageUri: ... } })

从这个例子 https://cloud.google.com/vision/docs/reference/libraries#client-libraries-install-nodejs 但我得到了完全相同的错误。然后我读到 textDetection 已被 detectText 取代,但不再成功

提前致谢

您似乎没有按照记录调用 API。首先看一下文档中提供的sample code

const vision = require('@google-cloud/vision');

// Creates a client
const client = new vision.ImageAnnotatorClient();

/**
 * TODO(developer): Uncomment the following line before running the sample.
 */
// const fileName = 'Local image file, e.g. /path/to/image.png';

// Performs text detection on the local file
client
  .textDetection(fileName)
  .then(results => {
    const detections = results[0].textAnnotations;
    console.log('Text:');
    detections.forEach(text => console.log(text));
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

您必须先创建一个可以调用的 ImageAnnotatorClient object, which as the textDetection() 方法。