|
| 1 | +# Google Cloud Speech to Custom Search Demo |
| 2 | + |
| 3 | +This sample shows you how to use the [Google Cloud Speech API][speech-api] |
| 4 | +to transcribe live audio from your computer's microphone and transmit it to |
| 5 | +a custom search API. |
| 6 | + |
| 7 | +[speech-api]: http://cloud.google.com/speech |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +### Enable the Speech API |
| 12 | + |
| 13 | +If you have not already done so, [enable the Google Cloud Speech |
| 14 | +API][console-speech] for your project. |
| 15 | + |
| 16 | +[console-speech]: https://console.cloud.google.com/apis/api/speech.googleapis.com/overview?project=_ |
| 17 | + |
| 18 | +### Setup the Custom Search API |
| 19 | + |
| 20 | +[Setup a Custom Search API](http://cse.google.com/manage/all) and make note of |
| 21 | +the value in the URL that comes after `cx=`, this is the identifier you will |
| 22 | +need when configuring the sample. |
| 23 | + |
| 24 | +### Authentication |
| 25 | + |
| 26 | +This sample uses a service accounts for authentication to the Speech API. |
| 27 | + |
| 28 | +* Visit the [Cloud Console][cloud-console], and navigate to: |
| 29 | + |
| 30 | + `API Manager > Credentials > Create credentials > Service account key > New |
| 31 | + service account`. |
| 32 | +* Create a new service account, and download the json credentials file. |
| 33 | +* Set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to point to your |
| 34 | + downloaded service account credentials: |
| 35 | + |
| 36 | + export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/credentials-key.json |
| 37 | + |
| 38 | + If you do not do this, the streaming sample will just sort of hang silently. |
| 39 | + |
| 40 | +You also must generate a developer API key that will be used with the Custom |
| 41 | +Search Engine. |
| 42 | + |
| 43 | +See the [Cloud Platform Auth Guide][auth-guide] for more information. |
| 44 | + |
| 45 | +[cloud-console]: https://console.cloud.google.com |
| 46 | +[auth-guide]: https://cloud.google.com/docs/authentication#developer_workflow |
| 47 | + |
| 48 | +### Setup |
| 49 | + |
| 50 | +* Clone this repo |
| 51 | + |
| 52 | + ```sh |
| 53 | + git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git |
| 54 | + cd python-docs-samples/speech/grpc |
| 55 | + ``` |
| 56 | + |
| 57 | +* If you don't have it already, install [virtualenv][virtualenv]. |
| 58 | + |
| 59 | + ```sh |
| 60 | + pip install virtualenv |
| 61 | + ``` |
| 62 | + |
| 63 | +* Create a [virtualenv][virtualenv]. This isolates the python dependencies |
| 64 | + you're about to install, to minimize conflicts with any existing libraries you |
| 65 | + might already have. |
| 66 | + |
| 67 | + ```sh |
| 68 | + virtualenv env |
| 69 | + source env/bin/activate |
| 70 | + ``` |
| 71 | + |
| 72 | +* Install [PortAudio][portaudio]. The sample uses the [PyAudio][pyaudio] |
| 73 | + library to stream audio from your computer's microphone. PyAudio depends on |
| 74 | + PortAudio for cross-platform compatibility, and is installed differently |
| 75 | + depending on the platform. For example: |
| 76 | + |
| 77 | + * For Mac OS X, you can use [Homebrew][brew]: |
| 78 | + |
| 79 | + ```sh |
| 80 | + brew install portaudio |
| 81 | + ``` |
| 82 | + |
| 83 | + * For Debian / Ubuntu Linux: |
| 84 | + |
| 85 | + ```sh |
| 86 | + apt-get install portaudio19-dev python-all-dev |
| 87 | + ``` |
| 88 | + |
| 89 | + * Windows may work without having to install PortAudio explicitly (it will get |
| 90 | + installed with PyAudio, when you run `python -m pip install ...` below). |
| 91 | + |
| 92 | + * For more details, see the [PyAudio installation][pyaudio-install] page. |
| 93 | + |
| 94 | +* Install the python dependencies: |
| 95 | + |
| 96 | + ```sh |
| 97 | + pip install -r requirements.txt |
| 98 | + ``` |
| 99 | + |
| 100 | +* Replace the values for `API_KEY` and `CSE_ID` in `speech_to_search.py` with |
| 101 | + those from the Google Cloud Developer Console and Custom Search Engine you |
| 102 | + created previously. |
| 103 | + |
| 104 | +[pyaudio]: https://people.csail.mit.edu/hubert/pyaudio/ |
| 105 | +[portaudio]: http://www.portaudio.com/ |
| 106 | +[pyaudio-install]: https://people.csail.mit.edu/hubert/pyaudio/#downloads |
| 107 | +[pip]: https://pip.pypa.io/en/stable/installing/ |
| 108 | +[virtualenv]: https://virtualenv.pypa.io/en/stable/installation/ |
| 109 | +[brew]: http://brew.sh |
| 110 | + |
| 111 | +### Troubleshooting |
| 112 | + |
| 113 | +#### PortAudio on OS X |
| 114 | + |
| 115 | +If you see the error |
| 116 | + |
| 117 | + fatal error: 'portaudio.h' file not found |
| 118 | + |
| 119 | +Try adding the following to your `~/.pydistutils.cfg` file, |
| 120 | +substituting in your appropriate brew Cellar directory: |
| 121 | + |
| 122 | + include_dirs=/usr/local/Cellar/portaudio/19.20140130/include/ |
| 123 | + library_dirs=/usr/local/$USER/homebrew/Cellar/portaudio/19.20140130/lib/ |
| 124 | + |
| 125 | +## Run the sample |
| 126 | + |
| 127 | +* To run the `speech_to_search.py` sample: |
| 128 | + |
| 129 | + ```sh |
| 130 | + python speech_to_search.py |
| 131 | + ``` |
| 132 | + |
| 133 | +To demo the sample: |
| 134 | + |
| 135 | +* Speak to transcribe what you say and send it to your Custom Search Engine. |
| 136 | + The sample will speak back the first result. |
| 137 | +* Say "next" to read the next result. |
| 138 | +* Say "quit" or "exit" to quit the sample. |
| 139 | + |
| 140 | +### Deactivate virtualenv |
| 141 | + |
| 142 | +When you're done running the sample, you can exit your virtualenv: |
| 143 | +
|
| 144 | +``` |
| 145 | +deactivate |
| 146 | +``` |
0 commit comments