首页 > 解决方案 > TypeError:无法读取未定义的属性“ImageAnnotatorClient”

问题描述

我正在尝试使用 Google-cloud/vision 来检测图像中的手写文本识别,(pdf)。从过去 6 小时开始,我一直面临错误消息。如果我必须更改代码中的任何内容,请建议我。

    'use strict';

function detectHandwritingOCR(fileName) {
  const vision = require('@google-cloud/vision').v1p3beta1;
  const fs = require('fs');
  // Creates a client
  const client = new vision.ImageAnnotatorClient();

  const request = {
    image: {
    content: fs.readFileSync(fileName),
    },
    feature: {
    languageHints: ['en-t-i0-handwrit'],
    },
};
 client
   .documentTextDetection(request)
  .then(results => {
     const fullTextAnnotation = results[0].fullTextAnnotation;
     console.log(`Full text: ${fullTextAnnotation.text}`);
})
.catch(err => {
  console.error('ERROR:', err);
});
 // [END vision_handwritten_ocr_beta]

}

console.log(detectHandwritingOCR('C:/Users/sandrpa/Downloads/handwrittenimage.jpg'));

我得到的错误信息是:

TypeError:无法读取未定义的属性“ImageAnnotatorClient”。**

标签: node.jsgoogle-cloud-vision

解决方案


我试图复制这种情况;但是,我能够毫无问题地执行几乎完全相同的代码。我建议您验证您是否已安装最新的Vision Client 库版本,并在您的代码中明确指定身份验证凭据,以放弃任何身份验证问题。

'use strict';

function detectHandwritingOCR(fileName) {
  const vision = require('@google-cloud/vision').v1p3beta1;
  const fs = require('fs');
  // Creates a client
  const client = new vision.ImageAnnotatorClient({keyFilename: '<CREDENTIALS_FILE_NAME>.json'});

  const request = {
    image: {
    content: fs.readFileSync(fileName),
    },
    feature: {
    languageHints: ['en-t-i0-handwrit'],
    },
};
 client
   .documentTextDetection(request)
  .then(results => {
     const fullTextAnnotation = results[0].fullTextAnnotation;
     console.log(`Full text: ${fullTextAnnotation.text}`);
})
.catch(err => {
  console.error('ERROR:', err);
});
 // [END vision_handwritten_ocr_beta]
}

console.log(detectHandwritingOCR('./<IMAGE_FILE_NAME>'));

推荐阅读