使用base64编码对Node.js进行Google Vision文本检测
刚刚开始探索谷歌云视觉API。根据他们的指南:使用base64编码对Node.js进行Google Vision文本检测,node.js,google-cloud-platform,google-cloud-vision,google-vision,Node.js,Google Cloud Platform,Google Cloud Vision,Google Vision,刚刚开始探索谷歌云视觉API。根据他们的指南: const client = new vision.ImageAnnotatorClient(); const fileName = 'Local image file, e.g. /path/to/image.png'; const [result] = await client.textDetection(fileName); 但是,我想使用base64表示二进制图像数据,因为他们声称可以使用 我发现这个参考是这样的: 我没有使用image
const client = new vision.ImageAnnotatorClient();
const fileName = 'Local image file, e.g. /path/to/image.png';
const [result] = await client.textDetection(fileName);
但是,我想使用base64表示二进制图像数据,因为他们声称可以使用
我发现这个参考是这样的:
我没有使用imageUri
,而是使用了“content”:string
,如前所述。但是SO示例使用const[result]=wait client.batchAnnotateImages(请求)代码>方法。我试着在const[result]=wait client.textDetection(
method)上使用相同的技术,但它给了我一个错误
所以我的问题是:是否可以使用base64编码字符串来表示图像以执行文本检测?如果可以,如何实现
非常感谢您的帮助。内容
字段需要是缓冲区
您使用nodejs客户端库。该库在内部使用grpc API,grpc API预期字节
在内容
字段键入
但是,JSON API需要base64字符串
工具书类
内容
字段需要是缓冲区
您使用nodejs客户端库。该库在内部使用grpc API,grpc API预期字节
在内容
字段键入
但是,JSON API需要base64字符串
工具书类
您可以使用指南,然后在创建后编辑以下行:
// Value of the image in base64
const img_base64 = '/9j/...';
const request = {
image: {
content: Buffer.from(img_base64, 'base64')
}
};
const [result] = await client.textDetection(request);
console.log(result.textAnnotations);
console.log(result.fullTextAnnotation);
您可以查看函数,阅读请求参数的说明,特别是以下部分:
表示图像的类似字典的对象。该对象应具有
单键(源代码
,内容
)
如果键为content
,则该值应为缓冲区
这导致以前的示例代码中使用的结构。与使用imageUri
或filename
时相反,它们必须位于另一个对象的内部,该对象的键是源代码,如中所示。您可以使用指南,并在创建后从中编辑以下行:
// Value of the image in base64
const img_base64 = '/9j/...';
const request = {
image: {
content: Buffer.from(img_base64, 'base64')
}
};
const [result] = await client.textDetection(request);
console.log(result.textAnnotations);
console.log(result.fullTextAnnotation);
您可以查看函数,阅读请求参数的说明,特别是以下部分:
表示图像的类似字典的对象。该对象应具有
单键(源代码
,内容
)
如果键为content
,则该值应为缓冲区
这导致了以前的示例代码中使用的结构。与使用imageUri
或filename
时相反,它们必须位于另一个对象的内部,该对象的键是source
,如图所示。我想我首先尝试使用字节数组…由于某种原因,它不起作用,所以我选择base64路径…我想首先尝试使用字节数组…由于某种原因,它不起作用,所以我选择了base64路由。。。