This sample shows you how to use the Google Cloud Speech API to transcribe live audio from your computer's microphone and transmit it to a custom search API or web browser.
If you have not already done so, enable the Google Cloud Speech API for your project.
Setup a Custom Search API and make note of
the value in the URL that comes after cx=, this is the identifier you will
need when configuring the sample.
This sample uses a service accounts for authentication to the Speech API.
-
Visit the Cloud Console, and navigate to:
API Manager > Credentials > Create credentials > Service account key > New service account. -
Create a new service account, and download the json credentials file.
-
Set the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable to point to your downloaded service account credentials:export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/credentials-key.jsonIf you do not do this, the streaming sample will just sort of hang silently.
You also must generate a developer API key that will be used with the Custom Search Engine.
See the Cloud Platform Auth Guide for more information.
-
Clone this repo
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git cd python-docs-samples/speech/grpc -
If you don't have it already, install virtualenv.
pip install virtualenv
-
Create a virtualenv. This isolates the python dependencies you're about to install, to minimize conflicts with any existing libraries you might already have.
virtualenv env source env/bin/activate -
Install PortAudio. The sample uses the PyAudio library to stream audio from your computer's microphone. PyAudio depends on PortAudio for cross-platform compatibility, and is installed differently depending on the platform. For example:
-
For Mac OS X, you can use Homebrew:
brew install portaudio
-
For Debian / Ubuntu Linux:
apt-get install portaudio19-dev python-all-dev
-
Windows may work without having to install PortAudio explicitly (it will get installed with PyAudio, when you run
python -m pip install ...below). -
For more details, see the PyAudio installation page.
-
-
Install the python dependencies:
pip install -r requirements.txt
-
Replace the values for
API_KEYandCSE_IDinspeech_to_search.pywith those from the Google Cloud Developer Console and Custom Search Engine you created previously.
If you see the error
fatal error: 'portaudio.h' file not found
Try adding the following to your ~/.pydistutils.cfg file,
substituting in your appropriate brew Cellar directory:
include_dirs=/usr/local/Cellar/portaudio/19.20140130/include/
library_dirs=/usr/local/$USER/homebrew/Cellar/portaudio/19.20140130/lib/
-
To run the
speech_to_search.pysample:python speech_to_search.py
To demo the sample:
- Speak to transcribe what you say and send it to your Custom Search Engine. The sample will speak back the first result.
- Say "next" to read the next result.
- Say "quit" or "exit" to quit the sample.
When you're done running the sample, you can exit your virtualenv:
deactivate