These examples demo accessing the Google Cloud Speech API in streaming mode (via its gRPC API) and in non-streaming mode (via its REST API).
If you have not already done so, enable the Google Cloud Speech API for your project. You must be whitelisted to do this.
The example uses a service account for OAuth2 authentication. So next, set up to authenticate with the Speech API using your project's service account credentials.
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.
Then, set
the GOOGLE_APPLICATION_CREDENTIALS
environment variable to point to your
downloaded service account credentials before running this example:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/credentials-key.json
If you do not do this, the REST api will return a 403. The streaming sample will just sort of hang silently.
See the Cloud Platform Auth Guide for more information.
Before running these samples perform the steps:
-
Clone this repo
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git cd python-docs-samples/speech/api
-
Create a virtualenv
virtualenv env source env/bin/activate
The sample uses the PyAudio library to stream audio from your computer's microphone. PyAudio depends on PortAudio, which may need to be installed separately, depending on your platform:
-
Install the pyAudio dependencies.
-
If you're running the
speech_rest.py
sample:$ pip install -r requirements-speech_rest.txt
-
If you're running the
speech_streaming.py
sample:$ pip install -r requirements-speech_grpc.txt
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/YourUsername/homebrew/Cellar/portaudio/19.20140130/lib/
-
To run the
speech_rest.py
sample:$ python speech_rest.py resources/audio.raw
You should see a response with the transcription result.
-
To run the
speech_async_rest.py
sample:$ python speech_async_rest.py resources/audio.raw
You should see a response with the transcription result.
-
To run the
speech_streaming.py
sample:$ python speech_streaming.py
The sample will run in a continuous loop, printing the data and metadata it receives from the Speech API, which includes alternative transcriptions of what it hears, and a confidence score. Say "exit" to exit the loop.
Note that the
speech_streaming.py
sample does not yet support python 3, as the upstreamgrpcio
library's support is not yet complete.
deactivate