nik*_*gan 3 node.js google-cloud-platform google-ai-platform cloud-document-ai
我正在尝试使用 Node.js 应用程序运行 Google 的文档 OCR。所以我使用了 Node JavaScript 的客户端库@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
}
Run Code Online (Sandbox Code Playgroud)
图像编码在这里
const {start: google} = require('./document-ai/index')
if (mimeType === 'application/pdf') {
pdf = true;
encoded = Buffer.from(file).toString('base64');
}
await google(encoded);
Run Code Online (Sandbox Code Playgroud)
结果我得到这个错误
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
}
Run Code Online (Sandbox Code Playgroud)
我的请求中有哪些无效参数?
环境详情
@google-cloud/documentai
版本:2.2.1小智 7
我也为此苦苦挣扎,结果证明解决方案非常简单:apiEndpoint
当您的位置不是 时,您必须设置参数"us"
。
这是位置的示例"eu"
:
const client = new DocumentProcessorServiceClient({
keyFilename,
apiEndpoint: 'eu-documentai.googleapis.com'
});
Run Code Online (Sandbox Code Playgroud)
更多信息请参见:GitHub:googleapis/nodejs-document-ai
归档时间: |
|
查看次数: |
2121 次 |
最近记录: |