Using the ‘Google Cloud Vision API’ with your Raspberry Pi

en.pdf24.org    Send article as PDF   

What is the ‘Google Cloud Vision API’?

Google Cloud Vision API enables your robot to understand the content of an image by encapsulating powerful machine learning models in an easy to use REST API. It quickly classifies images into thousands of categories such as “robot”, “elephant”, “flower”. It detects individual objects and faces within images. It capable of finding and reading printed words contained within images, and even determines the language it is written in. You can use it to build metadata for your image collection, and can be used to moderate offensive content through image analysis.
The Vision API enables you to detect different types of inappropriate content from adult to violent content. It analyzes images uploaded by the request, or integrate with your image storage on Google Cloud Storage.


The Raspberry Pi robot demo application consists of a “few hundred” lines of Python code that calls the Vision API, says Google. The YouTube video below shows the wheeled GoPiGo bot running the demo, but it’s said to work with any Raspberry Pi based robot. The demo enables a robot to identify objects and basic emotions as it rolls about. You can even teach the robot to follow a face.

 

The Google Cloud Vision API easily detect broad sets of objects from within images, and then classifies them into object categories commonly found within images. The Google Cloud Vision API improves over time as new concepts are introduced and accuracy is improved.

Google Cloud Platform Logo

The Vision API can analyze emotional facial attributes of people in your images, like joy, sorrow, and anger. Combine this with object detection and product logo detection, so you can assess how people feel about your logo.

/what-is-it

The following set of Google Cloud Vision API features can be applied in any combination on an image:
  • Label/Entity Detection picks out the dominant entity (e.g., a car, a cat) within an image, from a broad set of object categories. You can use the API to easily build metadata on your image catalog, enabling new scenarios like image based searches or recommendations.
  • Optical Character Recognition to retrieve text from an image. Cloud Vision API provides automatic language identification, and supports a wide variety of languages.
  • Safe Search Detection to detect inappropriate content within your image. Powered by Google SafeSearch, the feature enables you to easily moderate crowd-sourced content.
  • Facial Detection can detect when a face appears in photos, along with associated facial features such as eye, nose and mouth placement, and likelihood of over 8 attributes like joy and sorrow. We don’t support facial recognition and we don’t store facial detection information on any Google server.
  • Landmark Detection to identify popular natural and manmade structures, along with the associated latitude and longitude of the landmark.
  • Logo Detection to identify product logos within an image. Cloud Vision API returns the identified product brand logo, with the associated bounding polybox.

Install the Google Cloud Vision API on the Raspberry Pi:

We can install Google Cloud Vision on the Raspberry Pi, to classify images captured by the Raspberry Pi Camera using the Google Cloud Vision AP. We’ll start initially with setting up the Google Cloud Platform. Next, we will use the Raspberry Pi Camera to capture an image of an object, and then use the Raspberry Pi to upload this image to Google Cloud. Finally, we can analyze the picture in the cloud, and it will try to find logos from within in the image.

What is needed:

  • Raspberry Pi
  • Raspbian for Robots SD Card
  • Raspberry Pi Camera
  • Power Supply
  • Google Cloud Account

The Google Cloud Account is free for 60 days. After 60 days, you will be charged based on your usage. You can create an Google Cloud Account with either your Google or Gmail login details.

These directions assume you’re using Raspbian for Robots on your SD Card. You can you can download it for free and install it by following the directions on the Raspbian for Robots page.

Google Cloud Set Up

Setting up a Google Cloud Account

The first step is to set up a Google Cloud Account. You can create an account here with either your Google or Gmail login. After you’ve created your account, you can set up a project and enable billing. But first, go to the Google Cloud Projects Page.

Create a new project.

In our example, we named it “vision1

Enable billing for the account (you won’t be charged) by clicking here. Next go to the Cloud Platform Console and select the project “vision1”, followed by clicking the hamburger menu in the upper left hand corner of the page.

From this menu select, Billing on the left hand side. From here, enable billing for your Google Cloud account. Finally, enable the API by clicking here, and select the “vision1” account. Click “Continue”, and you should get the message that API is enabled.

Test Google Cloud Set Up:

Now that the account is setup, why not upload a few images from your computer, and test them? You can follow this tutorial in order to test out a few images, and be codepared to be amazed with Google’s response!


In this picture, I asked Google Cloud Vision to analyze this ‘LEGO Mindstorms EV3 Dinosaur’, I captured on the left. In the returned text response on the right shows the category “labels” (the things it sees in the picture) such as “Mecha (Robot)”, “Toy”, “Machine”, “Vehicle“, and “Lego”. The results from Google’s “Machine Learning Algorithm” are scarily accurate! There was a 97% chance it would be classified as a robot, with a 54% chance that it was made from LEGO.

 

Getting a JSON Key:

To acquire a JSON Key to put on our Raspberry Pi, which will handle all the authentication to use the Google Cloud Account instead of a password, we can use the file to authenticate the accoun.

Caution: You should be careful where you store this key. Be sure to change the password on your Raspberry Pi before putting the JSON key on it, leaving your JSON key unprotected could expose you to something malicious!

At the command line type:

sudo passwd

Follow the prompts to change the password of the Raspberry Pi.

  • Head back to the Console in Google Cloud and find the box titled “Use Google APIs”, then click “Enable and manage APIs

  • Click “Credentials” and Create Credentials. Credentials is located on the left hand side, with an associated picture of a key.

  • Select “Create a Service Account Key”. More details about the action is here.

  • Under “Service Account” Select “New Service Account”, and call it, “vision

  • Finally, create a role. Give it full access, by selecting “Project” and “Owner”, and give the Raspberry Pi Full access to all the resources.

  • A popup should appear informing you that a a new key has been created, followed by an automatic download of the JSON key should begin. Store this file in a safe place for future reference!

If all went to plan, you should have a service account key!

Next use an FTP program (such as FileZilla) or Samba (view tutorial here!) to move the JSON file over to your Raspberry Pi, and place the JSON file in the home directory.

/home/pi

Set Up the Hardware:

Picture of Set Up the Hardware

The hardware setup is pretty simple as we’ll be using a Raspberry Pi Camera with the Raspberry Pi. Here are some directions on setting up the camera.

These Raspberry Pi Camera setup directions assume you’re using Raspbian for Robots on the SD Card. You can download it for free, and install it using these directions here. If you’re not using Raspbian for Robots, you will need to take additional steps to enable the setup of the camera; here are some directions on setting up the camera.

​Prepare the Raspberry Pi:

Start by upgrading Pip using the Linux Command Line. Pip is a package manager for python language installations. Note, you should have Pip installed (it comes installed on Raspbian for Robots); if you don’t upgrade, you will get an error on installation!

sudo pip install --upgrade pip sudo apt-get install libjpeg8-dev

Next, install Google API Python Client using the command line:

sudo pip install --upgrade google-api-python-client

Install Python Imaging Library from the command line:

sudo pip install --upgrade Pillow

Install Python Picamera:

sudo apt-get install python-picamera

To turn on the Linux Super User, at the command line, type the command “su”:

su

You’ll be prompted for your password; this is the password you used to login to your Raspberry Pi.

From the home directory (/home/pi), make the JSON file available to any application you wish to run, by running this command:

export GOOGLE_APPLICATION_CREDENTIALS=filename.json

Ensure to substitute your filename.json, in this command with the name of the file you have on your Raspberry Pi.

Get the Example Code:

Download the example code from Dexter Industries:  three python examples are available on Github here.

sudo wget https://github.com/DexterInd/GoogleVisionTutorials

Detect a Logo:

Picture of Detect a Logo

To begin testing the installation,  try getting the Raspberry Pi to detect the Raspberry Pi Logo. As an example use the big Raspberry Pi label on the Raspberry Pi’s Box. Set the Raspberry Pi’s box to about 300mm from the camera, making sure the Raspberry Pi Camera is mounted securely!

Take a test picture to ensure that the label is visible by issuing the command:

raspistill -o cam.jpg

If the picture appeares focused, we can move to the real test!

Enter Super User mode on the Raspberry Pi.

su

You may need to run is you have issues:

sudo su

Ensure the JSON credentials are available using the command:

export GOOGLE_APPLICATION_CREDENTIALS=filename.json

Now move to the examples directory, and run:

python camera-vision-logo.py

You should receive the message, “Raspberry Pi”! This means that the Python Code has identified the Raspberry Pi logo on the box.

 Now you can search for more logos in your environment to identify.

Conclusion:

Google Cloud Vision is a fun service to experiment with! Google Cloud Vision on the Raspberry Pi is perfect for projects with the Raspberry Pi, the Raspberry Pi Camera, the BrickPi, by and adding in-context vision to Raspberry Pi based Robots.

BrickPi with Camera

Previous Post

Raspberry Pi Cheat Sheet - Learning Basic Linux Commands

A big part of using a Raspberry Pi is also using the terminal. The terminal is something that a lot of people try to avoid, because they feel like it ... Read more

Next Post

PID Control Theory Explained

Overview Proportional-Integral-Derivative (PID) control is the most common control algorithm used in industry and has been universally accepted in industrial control. The popularity of PID controllers can be attributed partly ... Read more

Short URL: http://tinyurl.com/hzo84f5

Leave a Reply

Your email address will not be published. Required fields are marked *



Do NOT follow this link or you will be banned from the site!
error: Content is protected !!