Search code examples
node.jsfirebasegoogle-cloud-functionsgoogle-cloud-vision

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


I try to use the Cloud Vision API in a Firebase Cloud function to OCR an image stored in Firebase Storage.

I import the Google Cloud vision client library as follow

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

and then I call

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

However I get an error

TypeError: vision.detectText is not a function

Initially I used

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

from this example https://cloud.google.com/vision/docs/reference/libraries#client-libraries-install-nodejs but I got the exact same error. I then read that textDetection has been replaced by detectText but no more success

Thanks in advance


Solution

  • It looks like you're not calling the APIs as documented. First, take a look at the sample code provided in the documentation:

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

    You have to first create an ImageAnnotatorClient object, which as the textDetection() method you can call.