Firebase Cloud功能和Cloud Vision API:TypeError:vision.detectText不是函数
我尝试在Firebase Cloud功能中使用Cloud Vision API来OCR存储在Firebase存储中的图像。
我导入了Google Cloud Vision客户端库,如下所示
const vision = require('@google-cloud/vision');
然后我打电话
vision.detectText({ source: { imageUri: 'gs://xxxx.appspot/yyyy.JPG' } })
但是我收到了一个错误
TypeError:vision.detectText不是函数
最初我用过
vision.textDetection({ source: { imageUri: ... } })
从这个例子。然后我读到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对象,你可以调用textDetection()方法。