Search code examples
javascriptnode.jsgoogle-cloud-vision

Empty response from Google Vision API with an image stored in Google Cloud Storage


I am trying to use the cloud vision API and I am able to make a successful request, but my response comes back empty, even with the test image provided on the API docs.

Request Body:

const imagePath = `gs://[bucket_name]/faulkner.jpg`;
const requestObject = {
  requests: [
    {
      image: {
        source: {
          gcsImageUri: imagePath
        }
      },
      features:[
        {
          type: 'LABEL_DETECTION',
          maxResults: 100
        }
      ]
    }
  ]
};

faulkner.jpg

Response Body:

{
  "responses": [{}]
}

I have even tried using the cloud API console and copy the request fields, and that too does not work

const apiKey = 'myAPIKey';
const fields = `fields=responses(labelAnnotations)&`;
const visionAPI = `https://vision.googleapis.com/v1/images:annotate?${fields}key=${apiKey}`;

Any help would be greatly appreciated.


Solution

  • I was having this exact issue, this is what worked for me...

    1. You do not need OAuth, just an API key.

    This is what I was doing wrong...

    In my HTTP call I needed to wrap my request in a new object literal as in

    {data: requestBody }
    

    To clarify,

    // My old call
    HTTP.call("POST", "https://vision.googleapis.com/v1/images:annotate?key=myAPIKey", requestBody, myCallback);
    
    // To my new call
    HTTP.call("POST", "https://vision.googleapis.com/v1/images:annotate?key=myAPIKey", {data: requestBody}, myCallback);
    
    // reqeustBody example
    
    {
      "requests":
        [
          {
            "features":
              [
                {
                  "type": "LABEL_DETECTION"
                } 
              ],
            "image":
              {
                "source":
                  {
                    "gcsImageUri": "gs://myBucketNameHere/myDemoImageNameHere.jpg"
                  }
              }
          }
        ]
      }
    

    NOTE: A few things that need to be done.

    1. Image is in your Google Cloud Platform Storage Bucket.
    2. The image name is exact in the call as it is in storage.
    3. The image must have something to detect i.e. if using FACE_DETECTION the image must have a human face.
    4. The image in Google Cloud Platform Storage MUST be checked to Share publicly.

    I am using the very same call above with my image named demo-image.jpg and everything works now that I wrapped the requestBody.