对于Node js的Google Document AI客户端库,哪个参数无效? [英] What argument is invalid for Google Document AI client library for Node js?

查看:111
本文介绍了对于Node js的Google Document AI客户端库,哪个参数无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Node js应用程序从Google运行文档OCR.所以我将客户端库用于Node js @ google-cloud/documentai

I'm trying to run Document OCR from Google with a Node js app. So i used the client library for Node js @google-cloud/documentai

我做了所有类似文档样本

有我的代码

const projectId = '*******';
const location = 'eu'; // Format is 'us' or 'eu'
const processor = '******'; // Create processor in Cloud Console
const keyFilename = './secret/******.json';

const { DocumentProcessorServiceClient } = require('@google-cloud/documentai').v1beta3;

const client = new DocumentProcessorServiceClient({projectId, keyFilename});

async function start(encodedImage) {

  console.log("Google AI Started")
  const name = `projects/${projectId}/locations/${location}/processors/${processor}`;

  const request = {
    name,
    document: {
      content: encodedImage,
      mimeType: 'application/pdf',
    },
  }

  try {
    const [result] = await client.processDocument(request);

    const { document } = result;

    const { text } = document;

    const getText = textAnchor => {
      if (!textAnchor.textSegments || textAnchor.textSegments.length === 0) {
        return '';
      }

      // First shard in document doesn't have startIndex property
      const startIndex = textAnchor.textSegments[0].startIndex || 0;
      const endIndex = textAnchor.textSegments[0].endIndex;

      return text.substring(startIndex, endIndex);
    };

    const [page1] = document;
    const { paragraphs } = page1;

    for (const paragraph of paragraphs) {
      const paragraphText = getText(paragraph.layout.textAnchor);
      console.log(`Paragraph text:\n${paragraphText}`);
    }
    return paragraphs;
  }
  catch (error) {
    console.error(error);
  }
}

module.exports = {
  start
}

图像编码在这里

const {start: google} = require('./document-ai/index')

if (mimeType === 'application/pdf') {
        pdf = true;
        encoded = Buffer.from(file).toString('base64');
      }

await google(encoded);

结果我得到这个错误

Google AI Started
Error: 3 INVALID_ARGUMENT: Request contains an invalid argument.
    at Object.callErrorFromStatus (C:\Users\NIKIGAN\WebstormProjects\papper-project\server\node_modules\google-gax\node_modules\@grpc\grpc-js\build\src\call.js:31:26)
    at Object.onReceiveStatus (C:\Users\NIKIGAN\WebstormProjects\papper-project\server\node_modules\google-gax\node_modules\@grpc\grpc-js\build\src\client.js:176:52)
    at Object.onReceiveStatus (C:\Users\NIKIGAN\WebstormProjects\papper-project\server\node_modules\google-gax\node_modules\@grpc\grpc-js\build\src\client-interceptors.js:342:141)
    at Object.onReceiveStatus (C:\Users\NIKIGAN\WebstormProjects\papper-project\server\node_modules\google-gax\node_modules\@grpc\grpc-js\build\src\client-interceptors.js:305:181)
    at C:\Users\NIKIGAN\WebstormProjects\papper-project\server\node_modules\google-gax\node_modules\@grpc\grpc-js\build\src\call-stream.js:124:78
    at processTicksAndRejections (internal/process/task_queues.js:79:11) {
  code: 3,
  details: 'Request contains an invalid argument.',
  metadata: Metadata {
    internalRepr: Map { 'grpc-server-stats-bin' => [Array] },
    options: {}
  },
  note: 'Exception occurred in retry method that was not classified as transient'
}

我的请求中有哪些无效参数?

What invalid arguments do I have in my request?

环境详细信息

  • 操作系统:Windows 10
  • Node.js版本:12.18.3
  • npm版本:6.14.8
  • @ google-cloud/documentai 版本:2.2.1
  • OS: Windows 10
  • Node.js version: 12.18.3
  • npm version: 6.14.8
  • @google-cloud/documentai version: 2.2.1

推荐答案

我也为此感到困惑,结果很简单:您必须设置参数 apiEndpoint 如果您的位置不是我们" .

I've strugglid with this as well and the solution is quite simple as it turns out: you have to set the parameter apiEndpoint when your location is not "us".

以下是位置"eu" 的示例:

const client = new DocumentProcessorServiceClient({ 
  keyFilename, 
  apiEndpoint: 'eu-documentai.googleapis.com' 
});

此处的更多信息: GitHub:googleapis/nodejs-document-ai

这篇关于对于Node js的Google Document AI客户端库,哪个参数无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆