The MagPi - July 2018

(Steven Felgate) #1

Tutorial


(^66) July 2018 raspberrypi.org/magpi
WALKTHROUGH
API before tweeting (Listing 2). We can do this now in
preparation of setting up our Google and Twitter API.
You should still be in the Pi-timolo folder, so type nano
pi-timolo.py and add the imports at the top of the
file. Next, press CTRL+W to use the search option to
find the UserMotionCodeHere() function and where
it’s called from. Add the new code into the function
(line 240), before the return line. Also locate where
the function is being called from (line 1798), to pass the
image file name and path. Press CTRL+X then Y and
RETURN to save. Next, we’ll set up the APIs.
Animal detection and tweeting
We will be using Google Label Detection, which
returns a list it associates with the image. First off,
you will need to install the Google Cloud Vision
libraries on your Raspberry Pi, so type pip install
--upgrade google-cloud-vision into your
Terminal window. Once finished, run pip install
google-cloud-storage.
Now you need authorisation, by going to
magpi.cc/xVLSVa to set up an account. Click on the
Manage Resources link and create a new project (you
may need to log in or create a Google account). Go
to the API Dashboard and search for and enable the
Vision PI. Then go to API & Services > Credentials,
click on Create Credentials > Service Account Key



New Service Account from the drop-down. Don’t
worry about choosing a Role. Click Create and you’ll
be prompted to download a JSON file. You need this
as it contains your service account key to allow you to
make calls to the API locally. Rename and move the
JSON file into your Pi-timolo folder and make a note
of the file path. Next, go back to pi-timolo.py and
add the line: os.environ["GOOGLEAPPLICATION
CREDENTIALS"] = "path_toyour.json_
credential_file" below import os to reference the
credentials in your JSON file.
Finally, set up a Twitter account if you haven’t
already and install Tweepy by entering sudo pip
install tweepy into a Terminal window. Once set
up, visit apps.twitter.com and create a new app,
then click on Keys and Access Tokens. Edit the code
in userMotionCodeHere() with your own consumer
and access info, labelled as ‘XXX’ in the code listing.
Finally, place your camera in front of your bird feeder
and run ./pi-timolo.py. Any pictures taken of a
bird should now be tweeted! If you want to identify
a different animal, change the line if "bird" in
tweetText: animalInPic = true.
Please note that although the API works well, it
can’t always discern exactly what’s in the picture,
especially if partially in view. It also won’t distinguish
between types of bird, but you should have more
success with mammals. You can test the API out with
some of your pictures at magpi.cc/EBzDam and visit
twitter.com/pibirdbrain to see example tweets. Good
luck and happy tweeting!
We will be using Google Label
Detection, which returns a list
it associates with the image
Above Get great
photos with night-
vision cameras


Free download pdf