Search in sources :

Example 1 with RecognizeRequest

use of com.google.cloud.speech.v1p1beta1.RecognizeRequest in project google-cloud-java by GoogleCloudPlatform.

the class SpeechClientTest method recognizeTest.

@Test
@SuppressWarnings("all")
public void recognizeTest() {
    RecognizeResponse expectedResponse = RecognizeResponse.newBuilder().build();
    mockSpeech.addResponse(expectedResponse);
    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();
    RecognizeResponse actualResponse = client.recognize(config, audio);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockSpeech.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    RecognizeRequest actualRequest = (RecognizeRequest) actualRequests.get(0);
    Assert.assertEquals(config, actualRequest.getConfig());
    Assert.assertEquals(audio, actualRequest.getAudio());
}
Also used : RecognitionAudio(com.google.cloud.speech.v1.RecognitionAudio) AudioEncoding(com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding) LongRunningRecognizeRequest(com.google.cloud.speech.v1.LongRunningRecognizeRequest) RecognizeRequest(com.google.cloud.speech.v1.RecognizeRequest) StreamingRecognizeRequest(com.google.cloud.speech.v1.StreamingRecognizeRequest) RecognitionConfig(com.google.cloud.speech.v1.RecognitionConfig) LongRunningRecognizeResponse(com.google.cloud.speech.v1.LongRunningRecognizeResponse) StreamingRecognizeResponse(com.google.cloud.speech.v1.StreamingRecognizeResponse) RecognizeResponse(com.google.cloud.speech.v1.RecognizeResponse) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Example 2 with RecognizeRequest

use of com.google.cloud.speech.v1p1beta1.RecognizeRequest in project java-speech by googleapis.

the class SpeechAdaptation method speechAdaptation.

public static void speechAdaptation(String uriPath) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SpeechClient speechClient = SpeechClient.create()) {
        // Provides "hints" to the speech recognizer to favor specific words and phrases in the
        // results.
        // https://cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v1p1beta1#google.cloud.speech.v1p1beta1.SpeechContext
        SpeechContext speechContext = SpeechContext.newBuilder().addPhrases("Brooklyn Bridge").setBoost(20.0F).build();
        // Configure recognition config to match your audio file.
        RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(RecognitionConfig.AudioEncoding.MP3).setSampleRateHertz(44100).setLanguageCode("en-US").addSpeechContexts(speechContext).build();
        // Set the path to your audio file
        RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(uriPath).build();
        // Make the request
        RecognizeRequest request = RecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build();
        // Display the results
        RecognizeResponse response = speechClient.recognize(request);
        for (SpeechRecognitionResult result : response.getResultsList()) {
            // First alternative is the most probable result
            SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
            System.out.printf("Transcript: %s\n", alternative.getTranscript());
        }
    }
}
Also used : SpeechRecognitionAlternative(com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative) RecognitionAudio(com.google.cloud.speech.v1p1beta1.RecognitionAudio) SpeechContext(com.google.cloud.speech.v1p1beta1.SpeechContext) RecognizeRequest(com.google.cloud.speech.v1p1beta1.RecognizeRequest) RecognitionConfig(com.google.cloud.speech.v1p1beta1.RecognitionConfig) SpeechClient(com.google.cloud.speech.v1p1beta1.SpeechClient) RecognizeResponse(com.google.cloud.speech.v1p1beta1.RecognizeResponse) SpeechRecognitionResult(com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult)

Example 3 with RecognizeRequest

use of com.google.cloud.speech.v1p1beta1.RecognizeRequest in project java-speech by googleapis.

the class TranscribeContextClasses method transcribeContextClasses.

// Provides "hints" to the speech recognizer to favor specific classes of words in the results.
static void transcribeContextClasses(String storageUri) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SpeechClient speechClient = SpeechClient.create()) {
        // SpeechContext: to configure your speech_context see:
        // https://cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v1#speechcontext
        // Full list of supported phrases (class tokens) here:
        // https://cloud.google.com/speech-to-text/docs/class-tokens
        SpeechContext speechContext = SpeechContext.newBuilder().addPhrases("$TIME").build();
        // RecognitionConfig: to configure your encoding and sample_rate_hertz, see:
        // https://cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v1#recognitionconfig
        RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(RecognitionConfig.AudioEncoding.LINEAR16).setSampleRateHertz(8000).setLanguageCode("en-US").addSpeechContexts(speechContext).build();
        // Set the path to your audio file
        RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(storageUri).build();
        // Build the request
        RecognizeRequest request = RecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build();
        // Perform the request
        RecognizeResponse response = speechClient.recognize(request);
        for (SpeechRecognitionResult result : response.getResultsList()) {
            // First alternative is the most probable result
            SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
            System.out.printf("Transcript: %s\n", alternative.getTranscript());
        }
    }
}
Also used : SpeechRecognitionAlternative(com.google.cloud.speech.v1.SpeechRecognitionAlternative) RecognitionAudio(com.google.cloud.speech.v1.RecognitionAudio) SpeechContext(com.google.cloud.speech.v1.SpeechContext) RecognizeRequest(com.google.cloud.speech.v1.RecognizeRequest) RecognitionConfig(com.google.cloud.speech.v1.RecognitionConfig) SpeechClient(com.google.cloud.speech.v1.SpeechClient) RecognizeResponse(com.google.cloud.speech.v1.RecognizeResponse) SpeechRecognitionResult(com.google.cloud.speech.v1.SpeechRecognitionResult)

Aggregations

RecognitionAudio (com.google.cloud.speech.v1.RecognitionAudio)2 RecognitionConfig (com.google.cloud.speech.v1.RecognitionConfig)2 RecognizeRequest (com.google.cloud.speech.v1.RecognizeRequest)2 RecognizeResponse (com.google.cloud.speech.v1.RecognizeResponse)2 LongRunningRecognizeRequest (com.google.cloud.speech.v1.LongRunningRecognizeRequest)1 LongRunningRecognizeResponse (com.google.cloud.speech.v1.LongRunningRecognizeResponse)1 AudioEncoding (com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding)1 SpeechClient (com.google.cloud.speech.v1.SpeechClient)1 SpeechContext (com.google.cloud.speech.v1.SpeechContext)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 StreamingRecognizeResponse (com.google.cloud.speech.v1.StreamingRecognizeResponse)1 RecognitionAudio (com.google.cloud.speech.v1p1beta1.RecognitionAudio)1 RecognitionConfig (com.google.cloud.speech.v1p1beta1.RecognitionConfig)1 RecognizeRequest (com.google.cloud.speech.v1p1beta1.RecognizeRequest)1 RecognizeResponse (com.google.cloud.speech.v1p1beta1.RecognizeResponse)1 SpeechClient (com.google.cloud.speech.v1p1beta1.SpeechClient)1 SpeechContext (com.google.cloud.speech.v1p1beta1.SpeechContext)1 SpeechRecognitionAlternative (com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative)1