3

我正在尝试使用 Node js 应用程序从 Google 运行 Document OCR。所以我使用了 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'
}

我的请求中有哪些无效论点?

环境细节

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

1 回答 1

5

我也为此苦苦挣扎,结果证明解决方案非常简单:apiEndpoint当您的位置不是时,您必须设置参数"us"

这是 location 的示例"eu"

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

更多信息在这里:GitHub:googleapis / nodejs-document-ai

于 2021-03-23T14:45:39.390 回答