use of com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult in project java-docs-samples by GoogleCloudPlatform.
the class Recognize method transcribeModelSelection.
// [START speech_transcribe_model_selection]
/**
* Performs transcription of the given audio file synchronously with
* the selected model.
* @param fileName the path to a audio file to transcribe
*/
public static void transcribeModelSelection(String fileName) throws Exception {
Path path = Paths.get(fileName);
byte[] content = Files.readAllBytes(path);
try (SpeechClient speech = SpeechClient.create()) {
// Configure request with video media type
RecognitionConfig recConfig = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.LINEAR16).setLanguageCode("en-US").setSampleRateHertz(16000).setModel("video").build();
RecognitionAudio recognitionAudio = RecognitionAudio.newBuilder().setContent(ByteString.copyFrom(content)).build();
RecognizeResponse recognizeResponse = speech.recognize(recConfig, recognitionAudio);
// Just print the first result here.
SpeechRecognitionResult result = recognizeResponse.getResultsList().get(0);
// There can be several alternative transcripts for a given chunk of speech. Just use the
// first (most likely) one here.
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Transcript : %s\n", alternative.getTranscript());
}
// [END speech_transcribe_model_selection]
}
use of com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult in project java-docs-samples by GoogleCloudPlatform.
the class Recognize method syncRecognizeWords.
/**
* Performs sync recognize and prints word time offsets.
*
* @param fileName the path to a PCM audio file to transcribe get offsets on.
*/
public static void syncRecognizeWords(String fileName) throws Exception {
try (SpeechClient speech = SpeechClient.create()) {
Path path = Paths.get(fileName);
byte[] data = Files.readAllBytes(path);
ByteString audioBytes = ByteString.copyFrom(data);
// Configure request with local raw PCM audio
RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.LINEAR16).setLanguageCode("en-US").setSampleRateHertz(16000).setEnableWordTimeOffsets(true).build();
RecognitionAudio audio = RecognitionAudio.newBuilder().setContent(audioBytes).build();
// Use blocking call to get audio transcript
RecognizeResponse response = speech.recognize(config, audio);
List<SpeechRecognitionResult> results = response.getResultsList();
for (SpeechRecognitionResult result : results) {
// There can be several alternative transcripts for a given chunk of speech. Just use the
// first (most likely) one here.
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Transcription: %s%n", alternative.getTranscript());
for (WordInfo wordInfo : alternative.getWordsList()) {
System.out.println(wordInfo.getWord());
System.out.printf("\t%s.%s sec - %s.%s sec\n", wordInfo.getStartTime().getSeconds(), wordInfo.getStartTime().getNanos() / 100000000, wordInfo.getEndTime().getSeconds(), wordInfo.getEndTime().getNanos() / 100000000);
}
}
}
}
use of com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult in project java-docs-samples by GoogleCloudPlatform.
the class Recognize method asyncRecognizeFile.
/**
* Performs non-blocking speech recognition on raw PCM audio and prints
* the transcription. Note that transcription is limited to 60 seconds audio.
*
* @param fileName the path to a PCM audio file to transcribe.
*/
public static void asyncRecognizeFile(String fileName) throws Exception {
// Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
try (SpeechClient speech = SpeechClient.create()) {
Path path = Paths.get(fileName);
byte[] data = Files.readAllBytes(path);
ByteString audioBytes = ByteString.copyFrom(data);
// Configure request with local raw PCM audio
RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.LINEAR16).setLanguageCode("en-US").setSampleRateHertz(16000).build();
RecognitionAudio audio = RecognitionAudio.newBuilder().setContent(audioBytes).build();
// Use non-blocking call for getting file transcription
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response = speech.longRunningRecognizeAsync(config, audio);
while (!response.isDone()) {
System.out.println("Waiting for response...");
Thread.sleep(10000);
}
List<SpeechRecognitionResult> results = response.get().getResultsList();
for (SpeechRecognitionResult result : results) {
// There can be several alternative transcripts for a given chunk of speech. Just use the
// first (most likely) one here.
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Transcription: %s%n", alternative.getTranscript());
}
}
}
use of com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult in project java-docs-samples by GoogleCloudPlatform.
the class Recognize method syncRecognizeGcs.
/**
* Performs speech recognition on remote FLAC file and prints the transcription.
*
* @param gcsUri the path to the remote FLAC audio file to transcribe.
*/
public static void syncRecognizeGcs(String gcsUri) throws Exception {
// Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
try (SpeechClient speech = SpeechClient.create()) {
// Builds the request for remote FLAC file
RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.FLAC).setLanguageCode("en-US").setSampleRateHertz(16000).build();
RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).build();
// Use blocking call for getting audio transcript
RecognizeResponse response = speech.recognize(config, audio);
List<SpeechRecognitionResult> results = response.getResultsList();
for (SpeechRecognitionResult result : results) {
// There can be several alternative transcripts for a given chunk of speech. Just use the
// first (most likely) one here.
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Transcription: %s%n", alternative.getTranscript());
}
}
}
use of com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult in project sample-googleassistant by androidthings.
the class AssistantActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "starting assistant demo");
setContentView(R.layout.activity_main);
ListView assistantRequestsListView = findViewById(R.id.assistantRequestsListView);
mAssistantRequestsAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mAssistantRequests);
mMainHandler = new Handler(getMainLooper());
assistantRequestsListView.setAdapter(mAssistantRequestsAdapter);
mButtonWidget = findViewById(R.id.assistantQueryButton);
mButtonWidget.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mEmbeddedAssistant.startConversation();
}
});
// Audio routing configuration: use default routing.
AudioDeviceInfo audioInputDevice = null;
AudioDeviceInfo audioOutputDevice = null;
if (USE_VOICEHAT_I2S_DAC) {
audioInputDevice = findAudioDevice(AudioManager.GET_DEVICES_INPUTS, AudioDeviceInfo.TYPE_BUS);
if (audioInputDevice == null) {
Log.e(TAG, "failed to find I2S audio input device, using default");
}
audioOutputDevice = findAudioDevice(AudioManager.GET_DEVICES_OUTPUTS, AudioDeviceInfo.TYPE_BUS);
if (audioOutputDevice == null) {
Log.e(TAG, "failed to found I2S audio output device, using default");
}
}
try {
if (USE_VOICEHAT_I2S_DAC) {
Log.i(TAG, "initializing DAC trigger");
mDac = VoiceHat.openDac();
mDac.setSdMode(Max98357A.SD_MODE_SHUTDOWN);
mButton = VoiceHat.openButton();
mLed = VoiceHat.openLed();
} else {
PeripheralManager pioManager = PeripheralManager.getInstance();
mButton = new Button(BoardDefaults.getGPIOForButton(), Button.LogicState.PRESSED_WHEN_LOW);
mLed = pioManager.openGpio(BoardDefaults.getGPIOForLED());
}
mButton.setDebounceDelay(BUTTON_DEBOUNCE_DELAY_MS);
mButton.setOnButtonEventListener(this);
mLed.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
mLed.setActiveType(Gpio.ACTIVE_HIGH);
} catch (IOException e) {
Log.e(TAG, "error configuring peripherals:", e);
return;
}
// Set volume from preferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
int initVolume = preferences.getInt(PREF_CURRENT_VOLUME, DEFAULT_VOLUME);
Log.i(TAG, "setting audio track volume to: " + initVolume);
UserCredentials userCredentials = null;
try {
userCredentials = EmbeddedAssistant.generateCredentials(this, R.raw.credentials);
} catch (IOException | JSONException e) {
Log.e(TAG, "error getting user credentials", e);
}
mEmbeddedAssistant = new EmbeddedAssistant.Builder().setCredentials(userCredentials).setDeviceInstanceId(DEVICE_INSTANCE_ID).setDeviceModelId(DEVICE_MODEL_ID).setLanguageCode(LANGUAGE_CODE).setAudioInputDevice(audioInputDevice).setAudioOutputDevice(audioOutputDevice).setAudioSampleRate(SAMPLE_RATE).setAudioVolume(initVolume).setDeviceModelId(DEVICE_MODEL_ID).setDeviceInstanceId(DEVICE_INSTANCE_ID).setLanguageCode(LANGUAGE_CODE).setRequestCallback(new RequestCallback() {
@Override
public void onRequestStart() {
Log.i(TAG, "starting assistant request, enable microphones");
mButtonWidget.setText(R.string.button_listening);
mButtonWidget.setEnabled(false);
}
@Override
public void onSpeechRecognition(List<SpeechRecognitionResult> results) {
for (final SpeechRecognitionResult result : results) {
Log.i(TAG, "assistant request text: " + result.getTranscript() + " stability: " + Float.toString(result.getStability()));
mAssistantRequestsAdapter.add(result.getTranscript());
}
}
}).setConversationCallback(new ConversationCallback() {
@Override
public void onResponseStarted() {
super.onResponseStarted();
// When bus type is switched, the AudioManager needs to reset the stream volume
if (mDac != null) {
try {
mDac.setSdMode(Max98357A.SD_MODE_LEFT);
} catch (IOException e) {
Log.e(TAG, "error enabling DAC", e);
}
}
}
@Override
public void onResponseFinished() {
super.onResponseFinished();
if (mDac != null) {
try {
mDac.setSdMode(Max98357A.SD_MODE_SHUTDOWN);
} catch (IOException e) {
Log.e(TAG, "error disabling DAC", e);
}
}
if (mLed != null) {
try {
mLed.setValue(false);
} catch (IOException e) {
Log.e(TAG, "cannot turn off LED", e);
}
}
}
@Override
public void onError(Throwable throwable) {
Log.e(TAG, "assist error: " + throwable.getMessage(), throwable);
}
@Override
public void onVolumeChanged(int percentage) {
Log.i(TAG, "assistant volume changed: " + percentage);
// Update our shared preferences
Editor editor = PreferenceManager.getDefaultSharedPreferences(AssistantActivity.this).edit();
editor.putInt(PREF_CURRENT_VOLUME, percentage);
editor.apply();
}
@Override
public void onConversationFinished() {
Log.i(TAG, "assistant conversation finished");
mButtonWidget.setText(R.string.button_new_request);
mButtonWidget.setEnabled(true);
}
@Override
public void onAssistantResponse(final String response) {
if (!response.isEmpty()) {
mMainHandler.post(new Runnable() {
@Override
public void run() {
mAssistantRequestsAdapter.add("Google Assistant: " + response);
}
});
}
}
public void onDeviceAction(String intentName, JSONObject parameters) {
if (parameters != null) {
Log.d(TAG, "Get device action " + intentName + " with parameters: " + parameters.toString());
} else {
Log.d(TAG, "Get device action " + intentName + " with no paramete" + "rs");
}
if (intentName.equals("action.devices.commands.OnOff")) {
try {
boolean turnOn = parameters.getBoolean("on");
mLed.setValue(turnOn);
} catch (JSONException e) {
Log.e(TAG, "Cannot get value of command", e);
} catch (IOException e) {
Log.e(TAG, "Cannot set value of LED", e);
}
}
}
}).build();
mEmbeddedAssistant.connect();
}
Aggregations