Search in sources :

Example 1 with StreamingRecognizeRequest

use of com.google.cloud.speech.v1beta1.StreamingRecognizeRequest in project google-cloud-java by GoogleCloudPlatform.

the class SpeechClientTest method streamingRecognizeExceptionTest.

@Test
@SuppressWarnings("all")
public void streamingRecognizeExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockSpeech.addException(exception);
    StreamingRecognizeRequest request = StreamingRecognizeRequest.newBuilder().build();
    MockStreamObserver<StreamingRecognizeResponse> responseObserver = new MockStreamObserver<>();
    StreamingCallable<StreamingRecognizeRequest, StreamingRecognizeResponse> callable = client.streamingRecognizeCallable();
    ApiStreamObserver<StreamingRecognizeRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
    requestObserver.onNext(request);
    try {
        List<StreamingRecognizeResponse> actualResponses = responseObserver.future().get();
        Assert.fail("No exception thrown");
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof StatusRuntimeException);
        StatusRuntimeException statusException = (StatusRuntimeException) e.getCause();
        Assert.assertEquals(Status.INVALID_ARGUMENT, statusException.getStatus());
    }
}
Also used : StreamingRecognizeRequest(com.google.cloud.speech.v1beta1.StreamingRecognizeRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) StreamingRecognizeResponse(com.google.cloud.speech.v1beta1.StreamingRecognizeResponse) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 2 with StreamingRecognizeRequest

use of com.google.cloud.speech.v1beta1.StreamingRecognizeRequest in project google-cloud-java by GoogleCloudPlatform.

the class SpeechClientTest method streamingRecognizeTest.

@Test
@SuppressWarnings("all")
public void streamingRecognizeTest() throws Exception {
    StreamingRecognizeResponse expectedResponse = StreamingRecognizeResponse.newBuilder().build();
    mockSpeech.addResponse(expectedResponse);
    StreamingRecognizeRequest request = StreamingRecognizeRequest.newBuilder().build();
    MockStreamObserver<StreamingRecognizeResponse> responseObserver = new MockStreamObserver<>();
    StreamingCallable<StreamingRecognizeRequest, StreamingRecognizeResponse> callable = client.streamingRecognizeCallable();
    ApiStreamObserver<StreamingRecognizeRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
    requestObserver.onNext(request);
    requestObserver.onCompleted();
    List<StreamingRecognizeResponse> actualResponses = responseObserver.future().get();
    Assert.assertEquals(1, actualResponses.size());
    Assert.assertEquals(expectedResponse, actualResponses.get(0));
}
Also used : StreamingRecognizeRequest(com.google.cloud.speech.v1.StreamingRecognizeRequest) StreamingRecognizeResponse(com.google.cloud.speech.v1.StreamingRecognizeResponse) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) Test(org.junit.Test)

Example 3 with StreamingRecognizeRequest

use of com.google.cloud.speech.v1beta1.StreamingRecognizeRequest in project google-cloud-java by GoogleCloudPlatform.

the class SpeechClientTest method streamingRecognizeExceptionTest.

@Test
@SuppressWarnings("all")
public void streamingRecognizeExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockSpeech.addException(exception);
    StreamingRecognizeRequest request = StreamingRecognizeRequest.newBuilder().build();
    MockStreamObserver<StreamingRecognizeResponse> responseObserver = new MockStreamObserver<>();
    StreamingCallable<StreamingRecognizeRequest, StreamingRecognizeResponse> callable = client.streamingRecognizeCallable();
    ApiStreamObserver<StreamingRecognizeRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
    requestObserver.onNext(request);
    try {
        List<StreamingRecognizeResponse> actualResponses = responseObserver.future().get();
        Assert.fail("No exception thrown");
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof StatusRuntimeException);
        StatusRuntimeException statusException = (StatusRuntimeException) e.getCause();
        Assert.assertEquals(Status.INVALID_ARGUMENT, statusException.getStatus());
    }
}
Also used : StreamingRecognizeRequest(com.google.cloud.speech.v1.StreamingRecognizeRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) StreamingRecognizeResponse(com.google.cloud.speech.v1.StreamingRecognizeResponse) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 4 with StreamingRecognizeRequest

use of com.google.cloud.speech.v1beta1.StreamingRecognizeRequest in project google-cloud-java by GoogleCloudPlatform.

the class SpeechClientTest method streamingRecognizeTest.

@Test
@SuppressWarnings("all")
public void streamingRecognizeTest() throws Exception {
    int resultIndex = 520358448;
    StreamingRecognizeResponse expectedResponse = StreamingRecognizeResponse.newBuilder().setResultIndex(resultIndex).build();
    mockSpeech.addResponse(expectedResponse);
    StreamingRecognizeRequest request = StreamingRecognizeRequest.newBuilder().build();
    MockStreamObserver<StreamingRecognizeResponse> responseObserver = new MockStreamObserver<>();
    StreamingCallable<StreamingRecognizeRequest, StreamingRecognizeResponse> callable = client.streamingRecognizeCallable();
    ApiStreamObserver<StreamingRecognizeRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
    requestObserver.onNext(request);
    requestObserver.onCompleted();
    List<StreamingRecognizeResponse> actualResponses = responseObserver.future().get();
    Assert.assertEquals(1, actualResponses.size());
    Assert.assertEquals(expectedResponse, actualResponses.get(0));
}
Also used : StreamingRecognizeRequest(com.google.cloud.speech.v1beta1.StreamingRecognizeRequest) StreamingRecognizeResponse(com.google.cloud.speech.v1beta1.StreamingRecognizeResponse) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) Test(org.junit.Test)

Example 5 with StreamingRecognizeRequest

use of com.google.cloud.speech.v1beta1.StreamingRecognizeRequest in project java-docs-samples by GoogleCloudPlatform.

the class Recognize method streamingRecognizeFile.

/**
 * Performs streaming speech recognition on raw PCM audio data.
 *
 * @param fileName the path to a PCM audio file to transcribe.
 */
public static void streamingRecognizeFile(String fileName) throws Exception, IOException {
    Path path = Paths.get(fileName);
    byte[] data = Files.readAllBytes(path);
    // Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
    try (SpeechClient speech = SpeechClient.create()) {
        // Configure request with local raw PCM audio
        RecognitionConfig recConfig = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.LINEAR16).setLanguageCode("en-US").setSampleRateHertz(16000).setModel("default").build();
        StreamingRecognitionConfig config = StreamingRecognitionConfig.newBuilder().setConfig(recConfig).build();
        class ResponseApiStreamingObserver<T> implements ApiStreamObserver<T> {

            private final SettableFuture<List<T>> future = SettableFuture.create();

            private final List<T> messages = new java.util.ArrayList<T>();

            @Override
            public void onNext(T message) {
                messages.add(message);
            }

            @Override
            public void onError(Throwable t) {
                future.setException(t);
            }

            @Override
            public void onCompleted() {
                future.set(messages);
            }

            // Returns the SettableFuture object to get received messages / exceptions.
            public SettableFuture<List<T>> future() {
                return future;
            }
        }
        ResponseApiStreamingObserver<StreamingRecognizeResponse> responseObserver = new ResponseApiStreamingObserver<>();
        BidiStreamingCallable<StreamingRecognizeRequest, StreamingRecognizeResponse> callable = speech.streamingRecognizeCallable();
        ApiStreamObserver<StreamingRecognizeRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
        // The first request must **only** contain the audio configuration:
        requestObserver.onNext(StreamingRecognizeRequest.newBuilder().setStreamingConfig(config).build());
        // Subsequent requests must **only** contain the audio data.
        requestObserver.onNext(StreamingRecognizeRequest.newBuilder().setAudioContent(ByteString.copyFrom(data)).build());
        // Mark transmission as completed after sending the data.
        requestObserver.onCompleted();
        List<StreamingRecognizeResponse> responses = responseObserver.future().get();
        for (StreamingRecognizeResponse response : responses) {
            // For streaming recognize, the results list has one is_final result (if available) followed
            // by a number of in-progress results (if iterim_results is true) for subsequent utterances.
            // Just print the first result here.
            StreamingRecognitionResult result = response.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());
        }
    }
}
Also used : Path(java.nio.file.Path) SettableFuture(com.google.common.util.concurrent.SettableFuture) StreamingRecognitionConfig(com.google.cloud.speech.v1p1beta1.StreamingRecognitionConfig) SpeechRecognitionAlternative(com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative) StreamingRecognitionResult(com.google.cloud.speech.v1p1beta1.StreamingRecognitionResult) StreamingRecognizeRequest(com.google.cloud.speech.v1p1beta1.StreamingRecognizeRequest) ApiStreamObserver(com.google.api.gax.rpc.ApiStreamObserver) StreamingRecognitionConfig(com.google.cloud.speech.v1p1beta1.StreamingRecognitionConfig) RecognitionConfig(com.google.cloud.speech.v1p1beta1.RecognitionConfig) SpeechClient(com.google.cloud.speech.v1p1beta1.SpeechClient) List(java.util.List) StreamingRecognizeResponse(com.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse)

Aggregations

MockStreamObserver (com.google.api.gax.grpc.testing.MockStreamObserver)4 Test (org.junit.Test)4 StreamingRecognizeRequest (com.google.cloud.speech.v1.StreamingRecognizeRequest)2 StreamingRecognizeResponse (com.google.cloud.speech.v1.StreamingRecognizeResponse)2 StreamingRecognizeRequest (com.google.cloud.speech.v1beta1.StreamingRecognizeRequest)2 StreamingRecognizeResponse (com.google.cloud.speech.v1beta1.StreamingRecognizeResponse)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 ExecutionException (java.util.concurrent.ExecutionException)2 ApiStreamObserver (com.google.api.gax.rpc.ApiStreamObserver)1 RecognitionConfig (com.google.cloud.speech.v1p1beta1.RecognitionConfig)1 SpeechClient (com.google.cloud.speech.v1p1beta1.SpeechClient)1 SpeechRecognitionAlternative (com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative)1 StreamingRecognitionConfig (com.google.cloud.speech.v1p1beta1.StreamingRecognitionConfig)1 StreamingRecognitionResult (com.google.cloud.speech.v1p1beta1.StreamingRecognitionResult)1 StreamingRecognizeRequest (com.google.cloud.speech.v1p1beta1.StreamingRecognizeRequest)1 StreamingRecognizeResponse (com.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse)1 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 Path (java.nio.file.Path)1 List (java.util.List)1