简体   繁体   English

Google Cloud Vision - 如何使用 Node.js 发送请求属性

[英]Google Cloud Vision - How to Send Request Properties with Node.js

I'm using Google Cloud Vision to detect text on an image.我正在使用 Google Cloud Vision 检测图像上的文本。 This works about 80% of the time.这在大约 80% 的时间里都有效。 The other 20%, I get this error:另外 20%,我得到这个错误:

Error: 3 INVALID_ARGUMENT: Request must specify image and features.
    at Object.callErrorFromStatus (C:\Users\emily\workspace\bot\node_modules\@grpc\grpc-js\build\src\call.js:31:26)
    at Object.onReceiveStatus (C:\Users\emily\workspace\bot\node_modules\@grpc\grpc-js\build\src\client.js:180:52)
    at Object.onReceiveStatus (C:\Users\emily\workspace\bot\node_modules\@grpc\grpc-js\build\src\client-interceptors.js:336:141)
    at Object.onReceiveStatus (C:\Users\emily\workspace\bot\node_modules\@grpc\grpc-js\build\src\client-interceptors.js:299:181)
    at C:\Users\emily\workspace\bot\node_modules\@grpc\grpc-js\build\src\call-stream.js:160:78
    at processTicksAndRejections (node:internal/process/task_queues:78:11) {
  code: 3,
  details: 'Request must specify image and features.',
  metadata: Metadata { internalRepr: Map(0) {}, options: {} },
  note: 'Exception occurred in retry method that was not classified as transient'

When I googled this issue, it seems I need to send specific headers with my request to resolve this, basically like as specified here: https://cloud.google.com/vision/docs/ocr#specify_the_language_optional当我用谷歌搜索这个问题时,似乎我需要在我的请求中发送特定的标题来解决这个问题,基本上就像这里指定的那样: https://cloud.google.com/vision/docs/ocr#specify_the_language_optional

However, I have no idea how to send these request parameters with the Node.js code I'm using and I can't find any examples anywhere.但是,我不知道如何使用我正在使用的 Node.js 代码发送这些请求参数,而且我在任何地方都找不到任何示例。 Can someone please help me figure out how to use this?有人可以帮我弄清楚如何使用它吗? My current code is this:我目前的代码是这样的:

                // Performs text detection on the image file using GCV
                (async () => {
                    await Jimp.read(attachment.url).then(image => {
                        return image
                            .invert()
                            .contrast(0.5)
                            .brightness(-0.25)
                            .write('temp.png');
                    });

                    const [result] = await googleapis.textDetection('temp.png');
                    const fullImageResults = result.textAnnotations;

Thanks!谢谢!

If you are using Node.js with Vision API you can refer to this sample quickstart code for using Node.js Client Library in Vision API for TEXT_DETECTION. If you are using Node.js with Vision API you can refer to this sample quickstart code for using Node.js Client Library in Vision API for TEXT_DETECTION.

For the error that you are facing, you can refer to the below code to add request parameters:对于您遇到的错误,您可以参考以下代码添加请求参数:

index.js: index.js:

async function quickstart() {
   const vision = require('@google-cloud/vision');
    const client = new vision.ImageAnnotatorClient();
    const request = {
     "requests": [
       {
         "image": {
           "source": {
             "imageUri": "gs://bucket1/download.png"
           }
         },
         "features": [
           {
             "type": "TEXT_DETECTION"
           }
         ],
         "imageContext": {
           "languageHints": ["en"]
         }
       }
     ]
   };
    const [result] = await client.batchAnnotateImages(request);
   const detections = result.responses[0].fullTextAnnotation;
   console.log(detections.text);
 }
  quickstart().catch(console.error);

Here in the above code I have stored the image in GCS and used the path of that image in my code.在上面的代码中,我将图像存储在 GCS 中,并在我的代码中使用了该图像的路径。

Image:图片:

在此处输入图像描述

Output: Output:

It was the best of
times, it was the worst
of times, it was the age
of wisdom, it was the
age of foolishness...

If you want to use the image file stored in the local system you can refer to the below code.如果要使用存储在本地系统中的图像文件,可以参考以下代码。

Since your file is in the local system , first you need to convert it to a base64 encoded string format and pass the same in the request parameters in your code.由于您的文件在本地系统中,首先您需要将其转换为base64 编码的字符串格式,并在代码中的请求参数中传递相同的格式。

index.js: index.js:

async function quickstart() {

   const vision = require('@google-cloud/vision');

   const client = new vision.ImageAnnotatorClient();
  
   const request ={
     "requests":[
       {
         "image":{
           "content":"/9j/7QBEUGhvdG9...image contents...eYxxxzj/Coa6Bax//Z"
                },
         "features": [
           {
             "type":"TEXT_DETECTION"
           }
         ],
         "imageContext": {
           "languageHints": ["en"]
         }
       }
     ]
   };
   const [result] = await client.batchAnnotateImages(request);
   const detections = result.responses[0].fullTextAnnotation;
   console.log(detections.text);
  
   }
  
   quickstart();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Node.js 中的单个请求中运行 Google Vision API 标签检测和安全搜索检测? - How to run Google Vision API label detection and safe search detection in a single request in Node.js? 无法从 API 响应中提取 JSON - Google Cloud Vision API(Node.js 库) - Can't extract JSON from API response - Google Cloud Vision API (Node.js library) 使用谷歌云 API、Node.js 和 request.del() 删除谷歌云存储对象 - Google cloud storage object deletion with google cloud API, Node.js, and request.del() 如何使用 node.js 在 heroku 上验证 google-vision api 凭据? - How to authenticate google-vision api credentials on heroku using node.js? 如何在Google Cloud上构建Node.JS Mobile后端 - How to build Node.JS Mobile backend on google cloud 如何下载部署在Google Cloud上的Node.js项目 - How to download Node.js project deployed on Google Cloud 如何在谷歌云上托管 node.js 应用程序? - How to host node.js app on google cloud propertly? 如何在 Google Cloud App Engine for Node.js 中使用 Memcache - How to use Memcache in Google Cloud App Engine for Node.js 如何在Google Cloud Function上运行服务器-使用Node.js - How to run server on Google Cloud Function - with Node.js 如何在 Google Cloud Storage (Node.js) 中管理对 object 的权限? - How to manage permissions to an object in Google Cloud Storage (Node.js)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM