Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud Speech API samples #138

Merged
merged 3 commits into from
Mar 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<module>managed_vms/twilio</module>
<module>monitoring/v2</module>
<module>monitoring/v3</module>
<module>speech/grpc</module>
<module>storage/json-api</module>
<module>storage/storage-transfer</module>
<module>storage/xml-api/cmdline-sample</module>
Expand Down
8 changes: 8 additions & 0 deletions speech/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Cloud Pub/Sub samples for Java

This directory contains several samples for the [Cloud Speech API](https://cloud.google.com/speech/)
with Java.

- [grpc](grpc)

A sample for accessing Cloud Speech streaming and non streaming apis with [gRPC](http://www.grpc.io/).
100 changes: 100 additions & 0 deletions speech/grpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Cloud Speech API gRPC samples for Java

This is a sample repo for accessing the [Google Cloud Speech API](http://cloud.google.com/speech) with
[gRPC](http://www.grpc.io/) client library.


## Prerequisites

### Enable the Speech API

If you have not already done so, [enable the Google Cloud Speech API for your project](https://console.developers.google.com/apis/api/speech.googleapis.com/overview).
You must be whitelisted to do this.


### Download and install Java and Maven

Install [Java7 or
higher](http://www.oracle.com/technetwork/java/javase/downloads/jre7-downloads-1880261.html).

This sample uses the [Apache Maven][maven] build system. Before getting started, be
sure to [download][maven-download] and [install][maven-install] it. When you use
Maven as described here, it will automatically download the needed client
libraries.

[maven]: https://maven.apache.org
[maven-download]: https://maven.apache.org/download.cgi
[maven-install]: https://maven.apache.org/install.html


### Set Up to Authenticate With Your Project's Credentials

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](https://console.developers.google.com), 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, you will see an error that looks something like this when
you run the example scripts:
`WARNING: RPC failed: Status{code=PERMISSION_DENIED, description=Request had insufficient authentication scopes., cause=null}`.
See the
[Cloud Platform Auth Guide](https://cloud.google.com/docs/authentication#developer_workflow)
for more information.

## Build the application

Then, build the program:

```sh
$ mvn package
```

or

```sh
$ mvn compile
$ mvn assembly:single
```

## Run the clients

These programs return the transcription of the audio file you provided. Please
note that the audio file must be in RAW format. You can use `sox`
(available, e.g. via [http://sox.sourceforge.net/](http://sox.sourceforge.net/)
or [homebrew](http://brew.sh/)) to convert audio files to raw format.

### Run the non-streaming client

You can run the batch client like this:

```sh
$ bin/speech-sample-nonstreaming.sh --host=speech.googleapis.com --port=443 \
--file=<audio file path> --sampling=<sample rate>
```

Try a streaming rate of 16000 and the included sample audio file, as follows:

```sh
$ bin/speech-sample-nonstreaming.sh --host=speech.googleapis.com --port=443 \
--file=resources/audio.raw --sampling=16000
```

### Run the streaming client

You can run the streaming client as follows:

```sh
$ bin/speech-sample-streaming.sh --host=speech.googleapis.com --port=443 \
--file=resources/audio.raw --sampling=16000
```

18 changes: 18 additions & 0 deletions speech/grpc/bin/speech-sample-nonstreaming.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

SRC_DIR=$(cd "$(dirname "$0")/.."; pwd)
java -cp ${SRC_DIR}/target/grpc-sample-1.0-jar-with-dependencies.jar \
com.google.cloud.speech.grpc.demos.NonStreamingRecognizeClient "$@"
18 changes: 18 additions & 0 deletions speech/grpc/bin/speech-sample-streaming.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

SRC_DIR=$(cd "$(dirname "$0")/.."; pwd)
java -cp ${SRC_DIR}/target/grpc-sample-1.0-jar-with-dependencies.jar \
com.google.cloud.speech.grpc.demos.RecognizeClient "$@"
Loading