跳到主要內容

Give Your Raspberry Pi Smart Vision

Give Your Raspberry Pi Smart Vision

What is Google Cloud Vision API? Check out the video by Google here:

Use Google Cloud Vision on the Raspberry Pi to take a picture with the Raspberry Pi Cameraand classify it with the Google Cloud Vision API.   First, we’ll walk you through setting up the Google Cloud Platform.
Next, we will use the Raspberry Pi Camera to take a picture of an object, and then use the Raspberry Pi to upload the picture taken to Google Cloud.
Finally, we can analyze the picture in the cloud, and the cloud will either try to label the picture (tell us in text what’s going on in the picture), or try to find logos in the picture (company logos that are in the picture) or analyze faces in the picture (tell us everything from emotions on the face to where the face is on the picture).  Skynet here we come!

raspberry-pi-camera-and-ribbon A camera for the Google Cloud Vision on the Raspberry Pi
These directions are meant to simplify getting started with Cloud Vision on your Raspberry Pi.  They are derived from directions found here but modified to work with the Raspberry Pi, and simplify set up.  We will use Google Cloud Vision on the Raspberry Pi to detect images with these three python examples.

raspberry-pi-b-angled-stylizedTools

The Google Cloud Account is free for 60 days.  After 60 days, you will be charged based on usage.  You can create an account here with either your Google or Gmail login. Google offers the first 60 days for free.
GoPiGo is analyzed by Google Cloud Vision using the Raspberry PI.
In this picture we’ve asked Google Cloud Vision to analyze the GoPiGo on the left, with the returned text response on the right.  It returns “labels” (the things it sees in the picture) such as “vehicle”, “wheel”, “toy”, “product”, and “model car”.  Pretty good job Google!

Google Cloud Set Up

The first step is to set up a Google Cloud Account.  You can create an account here. You can create an account here with either your Google or Gmail login. Google offers the first 60 days for free.
After you’ve created your account, you can set up a project and enable billing.  First, click here to go to the Google Cloud Projects Page.
Create a new project.
Create a new Project in Google Projects
In our example, we named it “vision1”
Create a new Project in Google Projects

Name the new project vision1
You will need to enable billing for your account (you won’t be charged).  You can do this by clicking here.  Go to the Cloud Platform Console here.  Select the project “vision1” and click the hamburger in the upper left hand corner of the page.
Select Billing on the left hand side.  From here, you can enable billing on your Google Cloud account.
Finally, we’ll enable the API.  Click here to enable the API.  Select the “vision1” account we created.  Click “Continue”.  You should get a message that API is enabled.

Optional: Test the Account

Now that the account is setup, why not upload an image from your computer and test it here?  You can follow this tutorial to try out a few images and see what Google thinks they are!
camel
Use our picture above.  Is it an animal? A reptile?  Google will tell you!

Getting a JSON Key

Now we want to get a JSON key to put on our Raspberry Pi.  This JSON key will handle all the authentication to use our Google Cloud Account.  Instead of a password, we can use the file to authenticate our account on the Raspberry Pi.
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!  In the command line type:
sudo passwd
And follow the prompts to change the password on the Raspberry Pi.
Head back to the Console in Google Cloud.  Find the box titled “Use Google APIs” and click “Enable and manage APIs”
Enable Google APIs for the Raspberry Pi
Click “Credentials” and Create Credentials.  Credentials is on the left hand side, with a picture of a key next to it.
Create Credentials
a3

Select “Create an Service Account Key”. See more about what we’re doing here.
Create Service Account Key for Raspberry Pi Google Cloud
Under “Service Account” Select “New Service Account”.  We’ll give it a name, “vision”
Create New Service account.
Finally, create a role.  We’ll give it full access, so select “Project” and “Owner” to give the Pi Full access to all resources.
Create a role
A popup should come up telling you you have created a new key, and an automatic download of the JSON key should begin.  Keep track of this file!
json-key-has-been-generated
That should be it, you should have a service account key!
You should now use an FTP program (such as FileZilla) or Samba (see our tutorial here!) to move the JSON file over to your Raspberry Pi.  Place the JSON file in the home directory/home/pi

Set Up the Hardware

The hardware is pretty simple!  We’ll be using a Raspberry Pi Camera with the Raspberry Pi, here are some directions on setting up the camera.
close_camera_port
These Raspberry Pi Camera setup directions assume you’re using Raspbian for Robots on your SD Card.  You can buy an SD Card with Raspbian for Robots on itor you can download it for free and install it using our directions here.  If you’re not using Raspbian for Robots, you will need to take additional steps to enable and setup your camera; here are some directions on setting up the camera.

Prepare the Raspberry Pi

In this project, we’re assuming you’re using our image, Raspbian for Robots.  If you’re not, you can download it here!
Next, we’ll upgrade Pip.  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.   Again in the command line, run:
sudo pip install --upgrade google-api-python-client
Next, install Python Imaging Library.  Again in the command line, run:
sudo pip install --upgrade Pillow
Install Python Picamera:
sudo apt-get install python-picamera
Turn on Super User.  In 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.
In the home directory, we will make the JSON file available to any application we’re running.  Run the command:
export GOOGLE_APPLICATION_CREDENTIALS=filename.json
Be sure to substitute your JSON filename in this command with the name of the file you have on your Raspberry Pi.

Optional: Make Credentials Permanent

You can also make the Google Application Credentials permanent.  You can make the JSON credentials from Google accessible to any python program running on your Raspberry Pi.
First, open the bashrc file:
sudo nano ~/.bashrc
Scroll to the very bottom of the file, and add the line:
GOOGLE_APPLICATION_CREDENTIALS="/home/pi/vision123.json"
Be sure to replace the variable “vision123” with the name of your file.
Now, load the credential by typing in the command line:
source ~/.bashrc
Finally, confirm that the credentials are set:
echo $GOOGLE_APPLICATION_CREDENTIALS.
This should return the file path and name:  ‘/home/pi/vision123.json”

Get the Example Code

sudo git clone https://github.com/DexterInd/GoogleVisionTutorials

Detect a Logo

raspberry-pi-gopigo-robot-with-google-vision-reading-raspberry-pi-logo
First, we’ll try to get the Raspberry Pi to detect the Raspberry Pi Logo.  We’ll use the big Raspberry Pi label on the Raspberry Pi Box.  Set the Pi box about 1 ft from the camera.  We’ll be propping up the camera with a Raspberry Pi Robot, the GoPiGo.  It holds the camera in place with the acrylic body, and we will use it later for some fun projects!
You might want to take a test picture to make sure the label is visible.  In the command line, run
raspistill -o cam.jpg

If the picture looks ok, we’ll move along!
image

We’ll go into super user mode on the Pi.
su
You may need to runsudo su.  We’ll make our JSON credentials available.  In the command line, type:
export GOOGLE_APPLICATION_CREDENTIALS=filename.json
And now, in the example directory, run:
python camera-vision-logo.py
You should get “Raspberry Pi” back!
raspberry-pi-box-with-google-vision
Exciting stuff!  Now you can go around the house looking for more logos!

Label Pictures

One of the cool things you can do with Google Vision is get the cloud to figure out what’s in the picture you take.  Let’s take a picture of a GoPiGo Wheel and see what the cloud thinks it is.
Set up the camera and run the sample program.  Again, we’ll be holding the camera in place with the GoPiGo robot kit.
raspberry-pi-gopigo-robot-with-google-vision
With the camera set up, we’ll take a test picture.  Looks like the wheel is in there.
image-of-gopigo-wheel-with-raspberry-pi-camera-using-google-cloud-vision
In this sample program we’ll return the entire JSON response.  We get a few interesting results from the picture:  “yellow” (with an 80% score), “wheel”, “product”, “tire”, “toy” and “automotive tire”.

google-vision-label-detection-wheel-detection

Facial Detection With the Raspberry Pi

Here’s the scary part!  Let’s detect a face and see if Google can detect some emotions!  With Google Cloud Vision you can detect a range of emotions, headwear, some head coordinates, and other fun data.  For this part, I made a special selfie-Pi using the GrovePi Case.
raspberry-pi-grovepi-robot-with-google-vision Google Cloud Vision on the Raspberry Pi selfie hardware!
I ran the program in the command line:
python camera-vision-face.py
Here’s the picture I took:
Work is so much fun! Google Cloud Vision on the Raspberry Pi takes a picture an analyzes a face!
“Work is so much fun!”
Clearly I’m trying to smile a little for the camera!  The response Google gives for facial analysis is pretty long, the JSON is very detailed, so I’m pulling the highlights below:
face-annotations-1
According to Google Vision, I’m not likely to be Angry (VERY_UNLIKELY)
face-annotations-2
I’m not likely to be wearing headwear,  however, it is possible that I’ve got joy (joyLikelihood is “POSSIBLE”).
face-annotations-3
Finally, I’m not likely to be sorrowful, and I’m not likely to be surprised.  These are just some of the emotions that Google Cloud Vision will scan faces for and return information on.

raspberry-pi-camera-on-gopigo-no-servo-front-view Google Cloud Vision on the Raspberry PiConclusion

Google Cloud Vision is a really fun service to just play around with!  Google Cloud Vision on the Raspberry Pi is perfect for projects with the Raspberry Pi and Raspberry Pi Camera, and adds in-context vision for the Raspberry Pi.
dex-black-with-white-face-analyzed-by-google-cloud-vision
Above is a picture of “Dex” our company mascot, analyzed by Google for labels.  You can see the computer correctly analyzes it as “electronics” and “technology”!
Troubleshooting
A few issues we’ve run into that might help you!
Optimal image size:  The optimal image size for face recognition with Google Vision is 1600 x 1200 – you can set this using the python lines
camera = picamera.PiCamera()
camera.resolution = (1600, 200)
Clock and Date Issues:  For proper authentication with the Google Cloud services, your clock must be correct (or darn near correct within about 12 hours).  If it is not, you will see the following error:
raise HttpAccessTokenRefreshError(error_msg, status=resp.status)
oauth2client.client.HttpAccessTokenRefreshError: invalid_grant: Invalid JWT: Token must be a short-lived token and in a reasonable timeframe
If the python example throws this error, you need to update your clock.  This can be tricky: it should be automatically updated if you boot the Pi while plugged into an ethernet network.  However, if you are using wifi, or your network isn’t working properly, you can set the Raspberry Pi time manually with the command:
date -u 110316262016
This is using the format “MMDDhhmmCCYY” where M is the month, D is the day, h is the hour, m is the minute, C is the century (probably 20), and Y is the year (in 2016, this is 16).

Questions?

Learn More!

If you liked this tutorial, consider buying the SD Card with Raspbian for Robots hereRaspberry Pi Camera here and the GoPiGo Starter kit here.

留言

這個網誌中的熱門文章

opencv4nodejs Asynchronous OpenCV 3.x Binding for node.js   122     2715     414   0   0 Author Contributors Repository https://github.com/justadudewhohacks/opencv4nodejs Wiki Page https://github.com/justadudewhohacks/opencv4nodejs/wiki Last Commit Mar. 8, 2019 Created Aug. 20, 2017 opencv4nodejs           By its nature, JavaScript lacks the performance to implement Computer Vision tasks efficiently. Therefore this package brings the performance of the native OpenCV library to your Node.js application. This project targets OpenCV 3 and provides an asynchronous as well as an synchronous API. The ultimate goal of this project is to provide a comprehensive collection of Node.js bindings to the API of OpenCV and the OpenCV-contrib modules. An overview of available bindings can be found in the  API Documentation . Furthermore, contribution is highly appreciated....

2017通訊大賽「聯發科技物聯網開發競賽」決賽團隊29強出爐!作品都在11月24日頒獎典禮進行展示

2017通訊大賽「聯發科技物聯網開發競賽」決賽團隊29強出爐!作品都在11月24日頒獎典禮進行展示 LIS   發表於 2017年11月16日 10:31   收藏此文 2017通訊大賽「聯發科技物聯網開發競賽」決賽於11月4日在台北文創大樓舉行,共有29個隊伍進入決賽,角逐最後的大獎,並於11月24日進行頒獎,現場會有全部進入決賽團隊的展示攤位,總計約為100個,各種創意作品琳琅滿目,非常值得一看,這次錯過就要等一年。 「聯發科技物聯網開發競賽」決賽持續一整天,每個團隊都有15分鐘面對評審團做簡報與展示,並接受評審們的詢問。在所有團隊完成簡報與展示後,主辦單位便統計所有評審的分數,並由評審們進行審慎的討論,決定冠亞季軍及其他各獎項得主,結果將於11月24日的「2017通訊大賽頒獎典禮暨成果展」現場公佈並頒獎。 在「2017通訊大賽頒獎典禮暨成果展」現場,所有入圍決賽的團隊會設置攤位,總計約為100個,展示他們辛苦研發並實作的作品,無論是想觀摩別人的成品、了解物聯網應用有那些新的創意、尋找投資標的、尋找人才、尋求合作機會或是單純有興趣,都很適合花點時間到現場看看。 頒獎典禮暨成果展資訊如下: 日期:2017年11月24日(星期五) 地點:中油大樓國光廳(台北市信義區松仁路3號) 我要報名參加「2017通訊大賽頒獎典禮暨成果展」>>> 在參加「2017通訊大賽頒獎典禮暨成果展」之前,可以先在本文觀看各團隊的作品介紹。 決賽29強團隊如下: 長者安全救星 可隨意描繪或書寫之電子筆記系統 微觀天下 體適能訓練管理裝置 肌少症之行走速率檢測系統 Sugar Robot 賽亞人的飛機維修輔助器 iTemp你的溫度個人化管家 語音行動冰箱 MR模擬飛行 智慧防盜自行車 跨平台X-Y視覺馬達控制 Ironmet 菸消雲散 無人小艇 (Mini-USV) 救OK-緊急救援小幫手 穿戴式長照輔助系統 應用於教育之模組機器人教具 這味兒很台味 Aquarium Hub 發展遲緩兒童之擴增實境學習系統 蚊房四寶 車輛相控陣列聲納環境偵測系統 戶外團隊運動管理裝置 懷舊治療數位桌曆 SeeM智能眼罩 觸...
2019全台精選3+個燈會,週邊順遊景點懶人包 2019燈會要去哪裡看?全台精選3+個燈會介紹、週邊順遊景點整理給你。 東港小鎮燈區-鮪鮪到來。 2019-02-15 微笑台灣編輯室 全台灣 各縣市政府 1435 延伸閱讀 ►  元宵節不只看燈會!全台元宵祭典精選、順遊景點整理 [屏東]2019台灣燈會在屏東 2/9-3/3:屏東市 · 東港鎮 · 大鵬灣國家風景區 台灣燈會自1990年起開始辦理,至2019年邁入第30週年,也是首次在屏東舉辦,屏東縣政府與交通部觀光局導入創新、科技元素,融入在地特色文化設計,在東港大鵬灣國家風景區打造廣闊的海洋灣域燈區,東港鎮結合漁港及宗教文化的小鎮燈區,及屏東市綿延近5公里長的綵燈節河岸燈區,讓屏東成為璀璨的光之南國,迎向國際。 詳細介紹 ►  2019台灣燈會在屏東 第一次移師國境之南 大鵬灣燈區 主題樂園式燈會也是主燈所在區,區內分為農業海洋燈區、客家燈區、原住民燈區、綠能環保燈區、藝術燈區、宗教燈區、競賽花燈及317個社區關懷據點手作的萬歲光廊等。 客家燈籠隧道。 平日:周一~周四14:00-22:30(熄燈) 假日:周五~周六10:00-22:30(熄燈)  屏東燈區: 萬年溪畔 屏東綵燈節藍區-生態。 綵燈節--每日17:30 - 22:00(熄燈) 勝利星村--平日:14:00 - 22:30(熄燈) 假日:10:00 - 22:30(熄燈) 燈區以「彩虹」為主題,沿著蜿蜒市區的萬年溪打造近5公里長的光之流域,50組水上、音樂及互動科技等不同類型燈飾,呈現紅色熱情、橙色活力、黃色甜美、綠色雄偉、藍色壯闊、靛色神祕、紫色華麗等屏東風情。勝利星村另有懷舊風的燈飾,及屏東公園聖誕節燈飾。 東港小鎮燈區 東港小鎮燈區-鮪鮪到來。 小鎮燈區以海的屏東為主題,用漁港風情及宗教文化內涵規劃4個主題區,分別為張燈結綵趣、東津好風情、神遊幸福海、延平老街區。每日17:00~22:30(熄燈) 以上台灣燈會資料來源: 2019台灣燈會官網 、 i屏東~愛屏東 。 >> 順遊行程 小吃旅行-東港小鎮 東港小吃和東港人一樣,熱情澎湃...