How to make batch requests to Google Cloud Vision using Node.js and cURL

Hann Jo Hopp
Google Cloud - Community
3 min readMay 6, 2018

What is Cloud Vision?

Cloud Vision is a RESTful API that detects objects, colors, and faces (among other things) within an image. Here’s a summary of the kind of information Cloud Vision gives you:

  • Image content labels
  • A guess at the image’s intended use (desktop background, avatar, etc)
  • Sites Google has seen similar or identical images
  • A swatch of the dominant colors in the image
  • NSFW-likelihood content ratings

All of this information is given in JSON format, which the user can then use for whatever they like, be it moderating offensive content or using sentiment analysis to open new marketing scenarios.

The documentation given by Google seems to be intended for using their console and Google Cloud Storage API. Here, I outline a way to make batch cURL requests with JSONs generated with Node.js.

Step one: Acquire an API key

Go to the Cloud Vision main page and click on their Try it Free button located near the top of the page. You’ll have a year to try out the Google Cloud API services, as well as $300 in credit to play around with any other APIs within their library you like.

You’ll be taken to the Google Cloud Platform console, where an empty product has initially been created for you.

Next, you’ll want to navigate to the API library either through the sidepanel or in one of the buttons of the dash on the main screen.

Click +Enable Apps and Services, find the Cloud Vision API and click Enable.

Great! Now go to the Credentials tab on the sidebar. Once there, click on the Create credentials button. This generates an API key for you. Keep it safe. If you lose it, you can always come back here and find it again, or generate a new one.

Step two: Generate JSONs with Node.js

Using Node, we’ll create a list of JSONs that contain the body of the requests we send to Cloud Vision. In this guide, I use Node’s fs module to read through my directory of images, loop through the files, convert the images to Base64, specify the analysis type I want Cloud Vision to perform, and save this in a JSON we’ll later submit using cURL.

Here is the code:

Break down:

  • The function writeRequests uses the fs module to first create an array of the files we are looking at within the folder we specify (replace PATH_TO_YOUR_FOLDER with your own path)
  • Then we loop through each file, check if the file extension is an image
  • If it is, we first read the file and convert it to Base64
  • We create an object called req that is formatted the way Cloud Vision specifies in their documentation. Within the object, we have the requests property that is an array of objects containing information about the image and the features that we want analyzed (in this case, I only specified label detection)
  • Finally, this request body is written to a file, the req object converted to a string using JSON.stringify()

Important note: the outputted JSON files will appear in the folder your script is located in.

Step three: Make requests using cURL and save the output

Finally, we are ready to send our requests and get the analysis back.

I used a bash script to do this:

for file in *; 
do
curl -v -s -H "Content-Type: application/json" \
https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY \
--data-binary @$file -o "${file%.*}"-labels.json; \
done

This script loops through your JSON files, sends a request to Cloud Vision using your API key as a credential (replace YOUR_API_KEY with your API key). It then outputs the results within a new JSON file with -labels appended to the filename.

If all went well, you should have a JSON output file that looks something like this:

{
"responses": [
{
"labelAnnotations": [
{
"mid": "/m/01bqvp",
"description": "sky",
"score": 0.9749082,
"topicality": 0.9749082
},
{
"mid": "/m/086mh",
"description": "winter",
"score": 0.973379,
"topicality": 0.973379
},
{
"mid": "/m/06_dn",
"description": "snow",
"score": 0.9580734,
"topicality": 0.9580734
},
...
]
}
]
}

Ellipses were added for brevity.

I hope this helps!

--

--

Hann Jo Hopp
Google Cloud - Community

👋Just an ex-scientist dreamer breaking their way into tech