Search code examples
node.jsgoogle-cloud-vision

How to enable Text Detection Confidence Score for Node JS, using Google Cloud Vision API


My text detections, using Google Cloud Vision API, are missing confidence scores. More specifically, the scores are always 0 except for the language feature. I am using Node JS to build a firebase cloud function.

My issue is similar to https://github.com/googleapis/python-vision/issues/266. The offered solution is for Python but I need the same for Node JS.

I found the right Node JS reference for it at https://cloud.google.com/nodejs/docs/reference/vision/latest/vision/protos.google.cloud.vision.v1p3beta1.textdetectionparams#_google_cloud_vision_protos_google_cloud_vision_v1p3beta1_TextDetectionParams_enableTextDetectionConfidenceScore_member

but I do not know how to implement it. These references confuse me. I have yet to learn how to use them. So, any tips on how to use these references at the same time would be appreciated.

I tried to improvise and got this:

const imageBucket = `gs://${object.bucket}/${object.name}`;

 const options = {
  "requests": [
    {   
      "features": [
        {
          "type": "DOCUMENT_TEXT_DETECTION"
        }
      ]
    }
  ]
}
 
 const client2 = new vision.ImageAnnotatorClient(options);

  const textDetectionParams = vision.TextDetectionParams({enableTextDetectionConfidenceScore: true})
  const imageContext = vision.ImageContext(textDetectionParams);
  
  
  const [textDetections] = await client2.textDetection(imageBucket, imageContext);

but this is returning an error: TypeError: vision.TextDetectionParams is not a function.

Any help would be appreciated.


Solution

  • There is a simple solution to that:

     const client = new vision.ImageAnnotatorClient();
      const imageBucket = `gs://${object.bucket}/${object.name}`;
      
      const [textDetections] = await client.documentTextDetection(imageBucket);
      
    

    Basically, change textDetection to documentTextDetection. This works perfectly and I can see the confidence scores now. Learn more here: https://cloud.google.com/vision/docs/handwriting