Search in sources :

Example 6 with RecognitionConfig

use of com.google.cloud.speech.v1.RecognitionConfig in project google-cloud-java by GoogleCloudPlatform.

the class RecognizeSpeech method main.

public static void main(String... args) throws Exception {
    // Instantiates a client
    SpeechClient speech = SpeechClient.create();
    // The path to the audio file to transcribe
    // for example "./resources/audio.raw";
    String fileName = "your/speech/audio/file.raw";
    // Reads the audio file into memory
    Path path = Paths.get(fileName);
    byte[] data = Files.readAllBytes(path);
    ByteString audioBytes = ByteString.copyFrom(data);
    // Builds the sync recognize request
    RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.LINEAR16).setSampleRateHertz(16000).setLanguageCode("en-US").build();
    RecognitionAudio audio = RecognitionAudio.newBuilder().setContent(audioBytes).build();
    // Performs speech recognition on the audio file
    RecognizeResponse response = speech.recognize(config, audio);
    List<SpeechRecognitionResult> results = response.getResultsList();
    for (SpeechRecognitionResult result : results) {
        List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList();
        for (SpeechRecognitionAlternative alternative : alternatives) {
            System.out.printf("Transcription: %s%n", alternative.getTranscript());
        }
    }
    speech.close();
}
Also used : Path(java.nio.file.Path) SpeechRecognitionAlternative(com.google.cloud.speech.v1.SpeechRecognitionAlternative) RecognitionAudio(com.google.cloud.speech.v1.RecognitionAudio) ByteString(com.google.protobuf.ByteString) RecognitionConfig(com.google.cloud.speech.v1.RecognitionConfig) SpeechClient(com.google.cloud.speech.spi.v1.SpeechClient) RecognizeResponse(com.google.cloud.speech.v1.RecognizeResponse) ByteString(com.google.protobuf.ByteString) SpeechRecognitionResult(com.google.cloud.speech.v1.SpeechRecognitionResult)

Example 7 with RecognitionConfig

use of com.google.cloud.speech.v1.RecognitionConfig in project google-cloud-java by GoogleCloudPlatform.

the class SpeechClientTest method syncRecognizeTest.

@Test
@SuppressWarnings("all")
public void syncRecognizeTest() {
    SyncRecognizeResponse expectedResponse = SyncRecognizeResponse.newBuilder().build();
    mockSpeech.addResponse(expectedResponse);
    RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
    int sampleRate = 44100;
    RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(encoding).setSampleRate(sampleRate).build();
    String uri = "gs://bucket_name/file_name.flac";
    RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(uri).build();
    SyncRecognizeResponse actualResponse = client.syncRecognize(config, audio);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockSpeech.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    SyncRecognizeRequest actualRequest = (SyncRecognizeRequest) actualRequests.get(0);
    Assert.assertEquals(config, actualRequest.getConfig());
    Assert.assertEquals(audio, actualRequest.getAudio());
}
Also used : RecognitionAudio(com.google.cloud.speech.v1beta1.RecognitionAudio) AudioEncoding(com.google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding) SyncRecognizeRequest(com.google.cloud.speech.v1beta1.SyncRecognizeRequest) RecognitionConfig(com.google.cloud.speech.v1beta1.RecognitionConfig) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) SyncRecognizeResponse(com.google.cloud.speech.v1beta1.SyncRecognizeResponse) Test(org.junit.Test)

Example 8 with RecognitionConfig

use of com.google.cloud.speech.v1.RecognitionConfig in project google-cloud-java by GoogleCloudPlatform.

the class SpeechClientTest method syncRecognizeExceptionTest.

@Test
@SuppressWarnings("all")
public void syncRecognizeExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockSpeech.addException(exception);
    try {
        RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
        int sampleRate = 44100;
        RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(encoding).setSampleRate(sampleRate).build();
        String uri = "gs://bucket_name/file_name.flac";
        RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(uri).build();
        client.syncRecognize(config, audio);
        Assert.fail("No exception raised");
    } catch (ApiException e) {
        Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
    }
}
Also used : RecognitionAudio(com.google.cloud.speech.v1beta1.RecognitionAudio) AudioEncoding(com.google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding) StatusRuntimeException(io.grpc.StatusRuntimeException) RecognitionConfig(com.google.cloud.speech.v1beta1.RecognitionConfig) ApiException(com.google.api.gax.grpc.ApiException) Test(org.junit.Test)

Example 9 with RecognitionConfig

use of com.google.cloud.speech.v1.RecognitionConfig in project google-cloud-java by GoogleCloudPlatform.

the class SpeechClientTest method longRunningRecognizeTest.

@Test
@SuppressWarnings("all")
public void longRunningRecognizeTest() throws Exception {
    LongRunningRecognizeResponse expectedResponse = LongRunningRecognizeResponse.newBuilder().build();
    Operation resultOperation = Operation.newBuilder().setName("longRunningRecognizeTest").setDone(true).setResponse(Any.pack(expectedResponse)).build();
    mockSpeech.addResponse(resultOperation);
    RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
    int sampleRateHertz = 44100;
    String languageCode = "en-US";
    RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(encoding).setSampleRateHertz(sampleRateHertz).setLanguageCode(languageCode).build();
    String uri = "gs://bucket_name/file_name.flac";
    RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(uri).build();
    LongRunningRecognizeResponse actualResponse = client.longRunningRecognizeAsync(config, audio).get();
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockSpeech.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    LongRunningRecognizeRequest actualRequest = (LongRunningRecognizeRequest) actualRequests.get(0);
    Assert.assertEquals(config, actualRequest.getConfig());
    Assert.assertEquals(audio, actualRequest.getAudio());
}
Also used : LongRunningRecognizeResponse(com.google.cloud.speech.v1.LongRunningRecognizeResponse) RecognitionAudio(com.google.cloud.speech.v1.RecognitionAudio) AudioEncoding(com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding) LongRunningRecognizeRequest(com.google.cloud.speech.v1.LongRunningRecognizeRequest) RecognitionConfig(com.google.cloud.speech.v1.RecognitionConfig) Operation(com.google.longrunning.Operation) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)8 RecognitionAudio (com.google.cloud.speech.v1.RecognitionAudio)5 RecognitionConfig (com.google.cloud.speech.v1.RecognitionConfig)5 ApiException (com.google.api.gax.grpc.ApiException)4 AudioEncoding (com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding)4 RecognitionAudio (com.google.cloud.speech.v1beta1.RecognitionAudio)4 RecognitionConfig (com.google.cloud.speech.v1beta1.RecognitionConfig)4 AudioEncoding (com.google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding)4 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)4 StatusRuntimeException (io.grpc.StatusRuntimeException)4 LongRunningRecognizeRequest (com.google.cloud.speech.v1.LongRunningRecognizeRequest)2 LongRunningRecognizeResponse (com.google.cloud.speech.v1.LongRunningRecognizeResponse)2 RecognizeResponse (com.google.cloud.speech.v1.RecognizeResponse)2 Operation (com.google.longrunning.Operation)2 ExecutionException (java.util.concurrent.ExecutionException)2 SpeechClient (com.google.cloud.speech.spi.v1.SpeechClient)1 RecognizeRequest (com.google.cloud.speech.v1.RecognizeRequest)1 SpeechRecognitionAlternative (com.google.cloud.speech.v1.SpeechRecognitionAlternative)1 SpeechRecognitionResult (com.google.cloud.speech.v1.SpeechRecognitionResult)1 StreamingRecognizeRequest (com.google.cloud.speech.v1.StreamingRecognizeRequest)1