Google Assistant on Ubuntu 18.04

NI wanted an easy way to add voice control to my linux laptop.  There are a few ways to do it using stand-alone programs and huge local speech recognition databases that need training, so I thought I'd try and add Google's Assistant instead so I could get just as frustrated with my laptop as I do with my android phone.

You will need:

  • PC/laptop/SoC running Ubuntu 18.04
  • A Google account
  • A working microphone (or headset)
  • A working speaker (or headset)

 


Step 1: Check ALSA is working and configured

 

Skip to step 2 if you know ALSA is fine.
Firstly, check speakers!

speaker-test -t wav 

If you don't hear anything, check your levels with the command alsamixer, which will also show you available sound devices - make sure your speakers are selected (or headset or whichever output you should be using).
Next check ALSA can record:

arecord --format=S16_LE --duration=5 --rate=16000 --file-type=raw out.raw

And to play back the recording:

aplay --format=S16_LE --rate=16000 out.raw

Again, if anything needs changing like mic levels then use alsamixer


Step 2: Create a new Google Project


Go to https://console.actions.google.com/ and log in with your Google account.  Go to Add/Import project and fill in the Name (choose something like GoogleAssistant or LinuxOKGoogle or something at least vaguely descriptive!), language and country.
On the next page, go to the bottom and More Options, and select Device Registration.  Click on the little gear icon and go to Project Settings and make a note of the Project ID -- we'll need it in Step 5.
Next go to https://console.developers.google.com/apis/api/embeddedassistant.googleapis.com/overview.  In the search box, enter Google Assistant API and hit search.  When it comes up with the API click on Enable.
Next up we need to configure our Oauth Credentials -- it's on the left menu.  Click the Configure Consent Screen button on the right.  Enter just your email address and a name for the product, leave the rest blank for now and hit Save at the bottom.
Next up, go to https://myaccount.google.com/activitycontrols and turn all the sliders to On.
Go back to https://console.actions.google.com/ and go to Device Registration.  Click Register Model, and fill in Product Name, Manufacturer name and select Speaker as the device type.  Make a note of the Model ID -- it's needed in Step 5.
Click Register Model and proceed to download the clients_secret.json to your PC/laptop/whatever - this will register our "device" with our Googley crap in the cloud in step 4.  You can skip the Traits bit, just leave it all as-is.

Step 3: Install Google Assistant SDK


The Google Assistant SDK is a set of Python scripts to allow the use of the Assistant with various bits of kit.  Start by installing Python:

sudo apt-get install python3-dev python3-pip

Next make sure all the dependencies are installed:

sudo apt-get install portaudio19-dev libffi-dev libssl-dev libmpg123-dev python3-openssl

Now get the Assistant SDK libraries:

pip3 install --user google-assistant-library
pip3 install --user google-assistant-sdk[samples]
pip3 install --user google-auth-oauthlib[tool]

Step 4: Authorise our Assistant


We now need our clients_secret.json we downloaded in step 2.  Make sure you give the full path of the file.  Careful of the formatting of the command - it should all be on one line:

./.local/bin/google-oauthlib-tool --scope https://www.googleapis.com/auth/assistant-sdk-prototype --scope https://www.googleapis.com/auth/gcm --save --headless --client-secrets /path/to/client_secret_client-id.json

Right-click and copy the link and paste it in a browser.  Log in if needed, and copy the authorisation code it returns and paste it back into the terminal.

Step 5: Let the frustration begin! (aka testing)


You did make a note of the Project ID and Device ID, right?  Try this command:

./.local/bin/google-assistant-demo --credentials ~/.config/google-oauthlib-tool/credentials.json --device_model_id your-device-id --project_id your-project-id

Now try saying, "OK Google" and ask a question.  You'll probably want to start by saying "set volume to 10" to set the internal Assistant volume to max.  If all goes well you should get some response.  If not, look for errors, check logs.
Congratulations! You now have a super frustrating voice interface.  We'll look at extending this functionality in later projects.

No comments:

Post a Comment