Google Cloud Speech API enables easy integration of Google speech recognition technologies into developer applications.
These sample Java applications demonstrate how to access the Cloud Speech API using the Google Cloud Client Library for Java.
Before you begin, complete the following steps as listed on the Speech-to-Text Docs pages for Getting Started
- Create or select a project.
- Enable the Google Speech-to-Text API for that project.
- Create a service account.
- Download a private key as JSON.
- Set
GOOGLE_APPLICATION_CREDENTIALS
export GOOGLE_APPLICATION_CREDENTIALS=path_to_your_downloaded_json_file
Install Maven.
Build your project with:
mvn clean package
Transcribe a local audio file
mvn exec:java -DQuickstart
Transcribe a local audio file
mvn exec:java -DRecognize -Dexec.args="syncrecognize ./resources/audio.raw"
Asynchronously transcribe a local audio file
mvn exec:java -DRecognize -Dexec.args="asyncrecognize ./resources/audio.raw"
Transcribe a remote audio file
mvn exec:java -DRecognize -Dexec.args="syncrecognize gs://cloud-samples-tests/speech/brooklyn.flac"
Asynchronously transcribe a remote audio file
mvn exec:java -DRecognize -Dexec.args="asyncrecognize gs://cloud-samples-tests/speech/vr.flac"
Synchronously transcribe an audio file and print word offsets
mvn exec:java -DRecognize -Dexec.args="wordoffsets ./resources/audio.raw"
Asynchronously transcribe a remote audio file and print word offsets
mvn exec:java -DRecognize -Dexec.args="wordoffsets gs://cloud-samples-tests/speech/vr.flac"
Synchronously transcribe an audio file
mvn exec:java -DRecognize -Dexec.args="model-selection ./resources/Google_Gnome.wav"
Asynchronously transcribe an audio file hosted on GCS
mvn exec:java -DRecognize -Dexec.args="model-selection gs://cloud-samples-tests/speech/Google_Gnome.wav"
Perform streaming speech transcription on an audio file
mvn exec:java -DRecognize -Dexec.args="streamrecognize ./resources/audio.raw"
Synchronously transcribe and punctuate an audio file
mvn exec:java -DRecognize -Dexec.args="auto-punctuation ./resources/audio.raw"
Asynchronously transcribe and punctuate an audio file hosted on GCS
mvn exec:java -DRecognize -Dexec.args="auto-punctuation gs://cloud-samples-tests/speech/brooklyn.flac"
Performing streaming speech transcription and punctuation on an audio file
mvn exec:java -DRecognize -Dexec.args="stream-punctuation ./resources/audio.raw"
Perform microphone streaming speech recognition
mvn exec:java -DRecognize -Dexec.args="micstreamrecognize"
Transcribe an audio file using an enhanced model
mvn exec:java -DRecognize -Dexec.args="enhanced-model ./resources/commercial_mono.wav"
Transcribe an audio file with recognition metadata
mvn exec:java -DRecognize -Dexec.args="metadata ./resources/commercial_mono.wav"
Transcribe an audio file using diarization on an audio file
mvn exec:java -DRecognize -Dexec.args="diarization ./resources/commercial_mono.wav"
Transcribe an audio file using diarization on an audio file hosted on GCS
mvn exec:java -DRecognize -Dexec.args="diarization gs://cloud-samples-tests/speech/commercial_mono.wav"
Transcribe an audio file with multiple channels
mvn exec:java -DRecognize -Dexec.args="multi-channel ./resources/commercial_stereo.wav"
Transcribe an audio file hosted on GCS with multiple channels
mvn exec:java -DRecognize -Dexec.args="multi-channel gs://cloud-samples-tests/speech/commercial_stereo.wav"
Transcribe an audio file with multiple languages
mvn exec:java -DRecognize -Dexec.args="multi-language ./resources/Google_Gnome.wav"
Transcribe an audio file hosted on GCS with multiple languages
mvn exec:java -DRecognize -Dexec.args="multi-language gs://cloud-samples-tests/speech/Google_Gnome.wav"
Transcribe an audio file with word level confidence
mvn exec:java -DRecognize -Dexec.args="word-level-conf ./resources/audio.raw"
Transcribe an audio file hosted on GCS with word level confidence
mvn exec:java -DRecognize -Dexec.args="word-level-conf gs://cloud-samples-tests/speech/brooklyn.flac"
Continuously stream audio to the speech API over multiple requests (by default en-US).
mvn exec:java -DInfiniteStreamRecognize
If stream audio is in different language, you could also pass language code as a command line argument (for example, en-GB for english (Great Britian), en-US for english U.S., and more available in this link).
mvn exec:java -Dexec.args="-lang_code=en-US" -DInfiniteStreamRecognize