Welcome to Robot Rebels, an online robot maker community.

Register today to post your projects, ask questions, share knowledge and meet like-minded people from around the world.

The Community

Author Topic: Google Vision API using Raspberry Pi and Node  (Read 2739 times)


  • Alabtu-ian Refugee
  • Member
  • *
  • Posts: 57
Google Vision API using Raspberry Pi and Node
« on: April 20, 2018, 02:38:12 PM »
Google Vision API using Raspberry Pi and Node

This is a jumpstart guide to connecting a Raspberry Pi Zero W to the Google Vision API.

1. Get an Account
Sadly, Google Vision API is not a completely free service.  At the time of writing an API account provides 1000 free Google Vision API calls a month.  Then, it's a $1.00 for each 1000 calls.

I know, I know, not too bad.  But this isn't a commercial project.  I'm wanting to use it for a puttering little house bot.  If my wife gets a bill for $40 because I decided to stream images to the API, well, it'll be a dead bot. Anyway, I thought I'd still explore the service for poo-and-giggles.

To get an account visit

* Google Console

And sign-in with an existing Google account or create one.

2. Enter Billing Information
Now, here's the scary part, you've must enter your billing information before getting going.  Remember, you will be charged if you go over 1000 calls.

Again, if you exceed your 1,000 free calls you will be charged. (What? I said that already? Oh.)

2. Enable Cloud Vision API
After setting up billing information we still need to enable the Cloud Vision API.  This is a security feature, essentially, all Google APIs are disabled by default so if someone accidentally gets access they don't unleash hell everywhere.

Now search for [code single]Vision[/code] and click the button.  Here there should be a glaring [code single]Enable[/code] button.  Press it.

The last thing we need to do is get the API key.  This needs to be included in the API call headers for authentication.

Do not let anyone get your API key. And do not hardcode it in your code.  Trust me, this will bite you.  If this accidentally gets pushed onto the web, a web crawler will find it quickly and you will be paying bajillions of dollars.

Let this article scare you a bit.

* Dev Puts AWS Keys on Github

Let's go get your API Key.  Find the [code single]Credentials[/code] section

You probably wont see any credentials created, as you've probably have not created any yet.

Let's create a new API Key.

I'd name the key something meaningful and limit it to only the Google Cloud API.

Go ahead and copy your API key, as we will need it in the next step.

3. Raspberry Pi Side Setup
The articles listed at the top of this one will help you setup the Raspberry Pi for this step.  But if you are doing things different, most of this should still work for you.  However, when we get to the part of about environment variables, that'll be different for other Linux flavors.

Start by SSH'ing into your Pi.

And update all packages
Code: [Select]
sudo pacman -SyuWe're going to create an environment variable for the Google Cloud Vision API.  This is to avoid hardcoding your API key into the code further down.  _That will work_, but I highly recommend you stick with me and setup an environment variable manager to handle the API.

Switch to the root user by typing
Code: [Select]
suEnter your password.

The next thing we do is add your Google Vision API Key as an environment variable to the [code single]/etc/profile[/code] file, this should cause it to be intialized at boot.

Type, replacing [code single]YOUR_API_KEY[/code] with your actual API Key.
Code: [Select]
echo 'export GOOGLE_CLOUD_VISION_API_KEY=YOUR_API_KEY' >> /etc/profileNow reboot the Pi so that takes effect.

Code: [Select]
sudo rebootLog back in.  Let's check to make sure it's loading the API key.
Code: [Select]
echo $GOOGLE_CLOUD_VISION_API_KEYIf your API key is echoed back, you should be good to go.

4. Project Setup

Let's create a project directory.

Code: [Select]
mkdir google-vis
cd google-vis
Now let's initialize a new Node project.
Code: [Select]
npm initFeel free to customize the package details if you like.  If you're lazy like me, hit enter until you are back to the command prompt.

Let's add the needed Node libraries.  It's one.  The axios library, which enables async web requests.

Code: [Select]
npm axios
Also, let's create a resource directory and download our lovely test image.  Ah, miss Hepburn!

Make sure you are in the [code single]google-vis/resources[/code] project directory when downloading the image.
Code: [Select]
mkdir resources
cd resources

5. NodeJS Code

Create a file in the [code single]go-vis[/code] directory called [code single]app.js[/code]

Code: [Select]
nano app.jsThen paste in the code below and save the file by typing CTRL+O and exiting using CTRL+X.

Code: [Select]
const axios = require('axios');
const fs = require('fs');


if (!API_KEY) {
  console.log('No API key provided')

function base64_encode(file) {
    // read binary data
    var bitmap = fs.readFileSync(file);
    // convert binary data to base64 encoded string
    return new Buffer(bitmap).toString('base64');
var base64str = base64_encode('./resources/audrey.jpg');

const apiCall = `${API_KEY}`;

const reqObj = {
            "content": base64str
                    "type": "IMAGE_PROPERTIES",
}, reqObj).then((response) => {
    console.log(JSON.stringify(, undefined, 4));
}).catch((e) => {

This code grabs the API key environment variable and creates a program constant from it.

Code: [Select]
const API_KEY = process.env.GOOGLE_CLOUD_VISION_API_KEYThis is how we avoid hardcoding the API key.

6. Run
Let's run the program.

Code: [Select]
node app.jsIf all went well you should get similar output to below

Code: [Select]
data: { responses: [ [Object] ] } }
        "labelAnnotations": [
                "mid": "/m/03q69",
                "description": "hair",
                "score": 0.9775374,
                "topicality": 0.9775374
                "mid": "/m/027n3_",
                "description": "eyebrow",
                "score": 0.90340185,
                "topicality": 0.90340185
                "mid": "/m/01ntw3",
                "description": "human hair color",
                "score": 0.8986981,
                "topicality": 0.8986981
                "mid": "/m/0ds4x",
                "description": "hairstyle",
                "score": 0.8985265,
                "topicality": 0.8985265
                "mid": "/m/01f43",
                "description": "beauty",
                "score": 0.87356544,
                "topicality": 0.87356544

7. And so much more...
This article is short--a jump start.  However, there is lots of potential here.  For example, sending your own images using the Raspberry Pi Camera


Please feel free to ask any questions regarding how to use the output.

There are other feature detection requests.

Google Vision API -- Other Features

However, I'm going to end the article and move on to rolling my on vision detection systems.  As soon as I figure out stochastic gradient descent.


* Search

* Recent Topics

Servo Calibrator by BaldwinK
[Today at 10:20:42 AM]

MKS Gen L 1.0 by jinx
[Today at 05:09:27 AM]

ESP32 "Life is Life" 64x32 flexible P4 RGB LED by jinx
[May 18, 2019, 11:19:44 AM]

Warco HV-4" Rotary Table by jinx
[May 08, 2019, 03:12:58 PM]

Warco WM14 milling machine by jinx
[April 28, 2019, 09:41:40 AM]

MKS GEN L Servo Issue by Reywas
[April 22, 2019, 09:10:08 PM]

Meowth Abandoned by Nemesis
[April 11, 2019, 11:08:00 AM]

Bench Power: Help a rebel out by jinx
[April 03, 2019, 04:20:10 AM]

Just a short intro... by MEgg
[March 18, 2019, 05:49:28 PM]

Yes I am alive! by jinx
[March 10, 2019, 04:38:53 AM]

CZ-1 2.0: problems, calibration and printing, part2 by MEgg
[February 26, 2019, 03:35:15 PM]

Hi, Mushroom glue here. by nschreiber0813
[February 25, 2019, 02:09:13 PM]

Arduino IK by BaldwinK
[February 25, 2019, 05:54:44 AM]

Raspberry Pi / Python eBook - free for the next 11 hours! by MEgg
[February 24, 2019, 09:49:43 AM]

Fun Fact by Impala
[February 13, 2019, 11:05:01 PM]

* Recent Posts

Servo Calibrator by BaldwinK
[Today at 10:20:42 AM]

Re: MKS Gen L 1.0 by jinx
[Today at 05:09:27 AM]

Re: MKS Gen L 1.0 by Bappi
[Today at 03:34:54 AM]

Re: MKS Gen L 1.0 by jinx
[Today at 02:28:17 AM]

Re: MKS Gen L 1.0 by Bappi
[May 20, 2019, 07:11:31 PM]

Re: MKS Gen L 1.0 by jinx
[May 20, 2019, 03:16:02 PM]

Re: MKS Gen L 1.0 by Bappi
[May 20, 2019, 02:53:00 PM]

Re: MKS Gen L 1.0 by jinx
[May 20, 2019, 03:31:49 AM]

Re: MKS Gen L 1.0 by Bappi
[May 19, 2019, 07:16:21 PM]

Re: MKS Gen L 1.0 by Bappi
[May 19, 2019, 04:04:47 PM]

Re: MKS Gen L 1.0 by jinx
[May 19, 2019, 08:50:41 AM]

Re: MKS Gen L 1.0 by Bappi
[May 18, 2019, 04:38:59 PM]

Re: MKS Gen L 1.0 by Bappi
[May 18, 2019, 04:25:37 PM]

Re: MKS Gen L 1.0 by jinx
[May 18, 2019, 04:10:22 PM]

Re: MKS Gen L 1.0 by Bappi
[May 18, 2019, 02:01:41 PM]