Search in sources :

Example 1 with SpeechRecognitionAlternative

use of com.google.cloud.speech.v1beta1.SpeechRecognitionAlternative in project Saiy-PS by brandall76.

the class RecognitionGoogleCloud method onNext.

/**
 * Receives a value from the stream.
 * <p>
 * <p>Can be called many times but is never called after {@link #onError(Throwable)} or {@link
 * #onCompleted()} are called.
 * <p>
 * <p>Unary calls must invoke onNext at most once.  Clients may invoke onNext at most once for
 * server streaming calls, but may receive many onNext callbacks.  Servers may invoke onNext at
 * most once for client streaming calls, but may receive many onNext callbacks.
 * <p>
 * <p>If an exception is thrown by an implementation the caller is expected to terminate the
 * stream by calling {@link #onError(Throwable)} with the caught exception prior to
 * propagating it.
 *
 * @param value the value passed to the stream
 */
@Override
public void onNext(final StreamingRecognizeResponse value) {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "onNext: " + TextFormat.printToString(value));
    }
    final StreamingRecognizeResponse.EndpointerType endpointerType = value.getEndpointerType();
    switch(endpointerType) {
        case START_OF_SPEECH:
            if (DEBUG) {
                MyLog.i(CLS_NAME, "onNext: START_OF_SPEECH");
            }
            if (doBeginning.get()) {
                doBeginning.set(false);
                listener.onBeginningOfSpeech();
            }
            break;
        case END_OF_SPEECH:
            if (DEBUG) {
                MyLog.i(CLS_NAME, "onNext: END_OF_SPEECH");
            }
            if (doEnd.get()) {
                doEnd.set(false);
                stopListening();
            }
            break;
        case END_OF_AUDIO:
            if (DEBUG) {
                MyLog.i(CLS_NAME, "onNext: END_OF_AUDIO");
            }
            if (doEnd.get()) {
                doEnd.set(false);
                stopListening();
            }
            break;
        case END_OF_UTTERANCE:
            if (DEBUG) {
                MyLog.i(CLS_NAME, "onNext: END_OF_UTTERANCE");
            }
            if (doEnd.get()) {
                doEnd.set(false);
                stopListening();
            }
            break;
        case UNRECOGNIZED:
            if (DEBUG) {
                MyLog.i(CLS_NAME, "onNext: UNRECOGNIZED");
            }
            break;
        case ENDPOINTER_EVENT_UNSPECIFIED:
        default:
            if (DEBUG) {
                MyLog.i(CLS_NAME, "onNext: ENDPOINTER_EVENT_UNSPECIFIED");
            }
            break;
    }
    if (doResults.get()) {
        if (UtilsList.notNaked(value.getResultsList())) {
            partialArray.clear();
            resultsArray.clear();
            confidenceArray.clear();
            bundle.clear();
            boolean isFinal = false;
            for (final StreamingRecognitionResult recognitionResult : value.getResultsList()) {
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "recognitionResult stability: " + recognitionResult.getStability());
                }
                isFinal = recognitionResult.getIsFinal();
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "isFinal: " + isFinal);
                }
                for (final SpeechRecognitionAlternative alternative : recognitionResult.getAlternativesList()) {
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "alternative: " + alternative.getTranscript());
                    }
                    if (isFinal) {
                        resultsArray.add(alternative.getTranscript());
                        confidenceArray.add(alternative.getConfidence());
                    } else {
                        if (partialArray.isEmpty()) {
                            partialArray.add(alternative.getTranscript());
                        } else {
                            partialArray.add(partialArray.get(0) + " " + alternative.getTranscript());
                        }
                    }
                }
            }
            doResults.set(!isFinal);
            if (isFinal) {
                bundle.putStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION, resultsArray);
                bundle.putFloatArray(SpeechRecognizer.CONFIDENCE_SCORES, ArrayUtils.toPrimitive(confidenceArray.toArray(new Float[0]), 0.0F));
                listener.onResults(bundle);
                stopListening();
            } else {
                bundle.putStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION, partialArray);
                listener.onPartialResults(bundle);
            }
        } else {
            if (DEBUG) {
                MyLog.i(CLS_NAME, "onNext: results list naked");
            }
        }
    } else {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "onNext: doResults false");
        }
    }
}
Also used : SpeechRecognitionAlternative(com.google.cloud.speech.v1beta1.SpeechRecognitionAlternative) StreamingRecognitionResult(com.google.cloud.speech.v1beta1.StreamingRecognitionResult) StreamingRecognizeResponse(com.google.cloud.speech.v1beta1.StreamingRecognizeResponse)

Example 2 with SpeechRecognitionAlternative

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

the class QuickstartSample method main.

/**
 * Demonstrates using the Speech API to transcribe an audio file.
 */
public static void main(String... args) throws Exception {
    // Instantiates a client
    try (SpeechClient speechClient = SpeechClient.create()) {
        // The path to the audio file to transcribe
        String fileName = "./resources/audio.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 = speechClient.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());
        }
    }
}
Also used : Path(java.nio.file.Path) SpeechRecognitionAlternative(com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative) RecognitionAudio(com.google.cloud.speech.v1p1beta1.RecognitionAudio) ByteString(com.google.protobuf.ByteString) RecognitionConfig(com.google.cloud.speech.v1p1beta1.RecognitionConfig) SpeechClient(com.google.cloud.speech.v1p1beta1.SpeechClient) RecognizeResponse(com.google.cloud.speech.v1p1beta1.RecognizeResponse) ByteString(com.google.protobuf.ByteString) SpeechRecognitionResult(com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult)

Example 3 with SpeechRecognitionAlternative

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

the class Recognize method asyncRecognizeGcs.

/**
 * Performs non-blocking speech recognition on remote FLAC file and prints
 * the transcription.
 *
 * @param gcsUri the path to the remote LINEAR16 audio file to transcribe.
 */
public static void asyncRecognizeGcs(String gcsUri) throws Exception {
    // Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
    try (SpeechClient speech = SpeechClient.create()) {
        // Configure remote file request for Linear16
        RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.FLAC).setLanguageCode("en-US").setSampleRateHertz(16000).build();
        RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).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());
        }
    }
}
Also used : LongRunningRecognizeResponse(com.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse) SpeechRecognitionAlternative(com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative) RecognitionAudio(com.google.cloud.speech.v1p1beta1.RecognitionAudio) StreamingRecognitionConfig(com.google.cloud.speech.v1p1beta1.StreamingRecognitionConfig) RecognitionConfig(com.google.cloud.speech.v1p1beta1.RecognitionConfig) SpeechClient(com.google.cloud.speech.v1p1beta1.SpeechClient) SpeechRecognitionResult(com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult) LongRunningRecognizeMetadata(com.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata)

Example 4 with SpeechRecognitionAlternative

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

the class Recognize method transcribeModelSelectionGcs.

// [START speech_transcribe_model_selection_gcs]
/**
 * Performs transcription of the remote audio file asynchronously with
 * the selected model.
 * @param gcsUri the path to the remote audio file to transcribe.
 */
public static void transcribeModelSelectionGcs(String gcsUri) throws Exception {
    try (SpeechClient speech = SpeechClient.create()) {
        // Configure request with video media type
        RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.LINEAR16).setLanguageCode("en-US").setSampleRateHertz(16000).setModel("video").build();
        RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).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();
        // Just print the first result here.
        SpeechRecognitionResult result = results.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_gcs]
}
Also used : LongRunningRecognizeResponse(com.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse) SpeechRecognitionAlternative(com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative) RecognitionAudio(com.google.cloud.speech.v1p1beta1.RecognitionAudio) StreamingRecognitionConfig(com.google.cloud.speech.v1p1beta1.StreamingRecognitionConfig) RecognitionConfig(com.google.cloud.speech.v1p1beta1.RecognitionConfig) SpeechClient(com.google.cloud.speech.v1p1beta1.SpeechClient) SpeechRecognitionResult(com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult) LongRunningRecognizeMetadata(com.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata)

Example 5 with SpeechRecognitionAlternative

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

the class Recognize method asyncRecognizeWords.

/**
 * Performs non-blocking speech recognition on remote FLAC file and prints
 * the transcription as well as word time offsets.
 *
 * @param gcsUri the path to the remote LINEAR16 audio file to transcribe.
 */
public static void asyncRecognizeWords(String gcsUri) throws Exception {
    // Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
    try (SpeechClient speech = SpeechClient.create()) {
        // Configure remote file request for Linear16
        RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.FLAC).setLanguageCode("en-US").setSampleRateHertz(16000).setEnableWordTimeOffsets(true).build();
        RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).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());
            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);
            }
        }
    }
}
Also used : LongRunningRecognizeResponse(com.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse) SpeechRecognitionAlternative(com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative) RecognitionAudio(com.google.cloud.speech.v1p1beta1.RecognitionAudio) StreamingRecognitionConfig(com.google.cloud.speech.v1p1beta1.StreamingRecognitionConfig) RecognitionConfig(com.google.cloud.speech.v1p1beta1.RecognitionConfig) SpeechClient(com.google.cloud.speech.v1p1beta1.SpeechClient) SpeechRecognitionResult(com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult) LongRunningRecognizeMetadata(com.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata) WordInfo(com.google.cloud.speech.v1p1beta1.WordInfo)

Aggregations

RecognitionConfig (com.google.cloud.speech.v1p1beta1.RecognitionConfig)10 SpeechClient (com.google.cloud.speech.v1p1beta1.SpeechClient)10 SpeechRecognitionAlternative (com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative)10 RecognitionAudio (com.google.cloud.speech.v1p1beta1.RecognitionAudio)9 SpeechRecognitionResult (com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult)9 StreamingRecognitionConfig (com.google.cloud.speech.v1p1beta1.StreamingRecognitionConfig)9 LongRunningRecognizeResponse (com.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse)8 Path (java.nio.file.Path)7 RecognizeResponse (com.google.cloud.speech.v1p1beta1.RecognizeResponse)5 StreamingRecognizeResponse (com.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse)5 ByteString (com.google.protobuf.ByteString)5 LongRunningRecognizeMetadata (com.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata)4 WordInfo (com.google.cloud.speech.v1p1beta1.WordInfo)2 ApiStreamObserver (com.google.api.gax.rpc.ApiStreamObserver)1 RecognitionAudio (com.google.cloud.speech.v1.RecognitionAudio)1 RecognitionConfig (com.google.cloud.speech.v1.RecognitionConfig)1 RecognizeResponse (com.google.cloud.speech.v1.RecognizeResponse)1 SpeechClient (com.google.cloud.speech.v1.SpeechClient)1 SpeechRecognitionAlternative (com.google.cloud.speech.v1.SpeechRecognitionAlternative)1 SpeechRecognitionResult (com.google.cloud.speech.v1.SpeechRecognitionResult)1