Search in sources :

Example 11 with LongRunningRecognizeMetadata

use of com.google.cloud.speech.v1.LongRunningRecognizeMetadata in project java-speech by googleapis.

the class Recognize method transcribeGcsWithAutomaticPunctuation.

// [END speech_sync_recognize_punctuation]
// [START speech_transcribe_auto_punctuation]
/**
 * Performs transcription on remote FLAC file and prints the transcription.
 *
 * @param gcsUri the path to the remote FLAC audio file to transcribe.
 */
public static void transcribeGcsWithAutomaticPunctuation(String gcsUri) throws Exception {
    try (SpeechClient speechClient = SpeechClient.create()) {
        // Configure request with raw PCM audio
        RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.FLAC).setLanguageCode("en-US").setSampleRateHertz(16000).setEnableAutomaticPunctuation(true).build();
        // Set the remote path for the audio file
        RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).build();
        // Use non-blocking call for getting file transcription
        OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response = speechClient.longRunningRecognizeAsync(config, audio);
        while (!response.isDone()) {
            System.out.println("Waiting for response...");
            Thread.sleep(10000);
        }
        // Just print the first result here.
        SpeechRecognitionResult result = response.get().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);
        // Print out the result
        System.out.printf("Transcript : %s\n", alternative.getTranscript());
    }
}
Also used : LongRunningRecognizeResponse(com.google.cloud.speech.v1.LongRunningRecognizeResponse) SpeechRecognitionAlternative(com.google.cloud.speech.v1.SpeechRecognitionAlternative) RecognitionAudio(com.google.cloud.speech.v1.RecognitionAudio) RecognitionConfig(com.google.cloud.speech.v1.RecognitionConfig) StreamingRecognitionConfig(com.google.cloud.speech.v1.StreamingRecognitionConfig) SpeechClient(com.google.cloud.speech.v1.SpeechClient) SpeechRecognitionResult(com.google.cloud.speech.v1.SpeechRecognitionResult) LongRunningRecognizeMetadata(com.google.cloud.speech.v1.LongRunningRecognizeMetadata)

Example 12 with LongRunningRecognizeMetadata

use of com.google.cloud.speech.v1.LongRunningRecognizeMetadata in project java-speech by googleapis.

the class Recognize method asyncRecognizeWords.

// [END speech_transcribe_async]
// [START speech_transcribe_async_word_time_offsets_gcs]
/**
 * 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 FLAC
        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.v1.LongRunningRecognizeResponse) SpeechRecognitionAlternative(com.google.cloud.speech.v1.SpeechRecognitionAlternative) RecognitionAudio(com.google.cloud.speech.v1.RecognitionAudio) RecognitionConfig(com.google.cloud.speech.v1.RecognitionConfig) StreamingRecognitionConfig(com.google.cloud.speech.v1.StreamingRecognitionConfig) SpeechClient(com.google.cloud.speech.v1.SpeechClient) SpeechRecognitionResult(com.google.cloud.speech.v1.SpeechRecognitionResult) LongRunningRecognizeMetadata(com.google.cloud.speech.v1.LongRunningRecognizeMetadata) WordInfo(com.google.cloud.speech.v1.WordInfo)

Example 13 with LongRunningRecognizeMetadata

use of com.google.cloud.speech.v1.LongRunningRecognizeMetadata in project java-speech by googleapis.

the class Recognize method asyncRecognizeFile.

// [END speech_transcribe_sync_gcs]
// [START speech_transcribe_async]
/**
 * 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());
        }
    }
}
Also used : Path(java.nio.file.Path) LongRunningRecognizeResponse(com.google.cloud.speech.v1.LongRunningRecognizeResponse) 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) StreamingRecognitionConfig(com.google.cloud.speech.v1.StreamingRecognitionConfig) SpeechClient(com.google.cloud.speech.v1.SpeechClient) SpeechRecognitionResult(com.google.cloud.speech.v1.SpeechRecognitionResult) LongRunningRecognizeMetadata(com.google.cloud.speech.v1.LongRunningRecognizeMetadata)

Example 14 with LongRunningRecognizeMetadata

use of com.google.cloud.speech.v1.LongRunningRecognizeMetadata in project java-speech by googleapis.

the class Recognize method transcribeMultiChannelGcs.

// [END speech_transcribe_multichannel]
// [START speech_transcribe_multichannel_gcs]
/**
 * Transcribe a remote audio file with multi-channel recognition
 *
 * @param gcsUri the path to the audio file
 */
public static void transcribeMultiChannelGcs(String gcsUri) throws Exception {
    try (SpeechClient speechClient = SpeechClient.create()) {
        // Configure request to enable multiple channels
        RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.LINEAR16).setLanguageCode("en-US").setSampleRateHertz(44100).setAudioChannelCount(2).setEnableSeparateRecognitionPerChannel(true).build();
        // Set the remote path for the audio file
        RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).build();
        // Use non-blocking call for getting file transcription
        OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response = speechClient.longRunningRecognizeAsync(config, audio);
        while (!response.isDone()) {
            System.out.println("Waiting for response...");
            Thread.sleep(10000);
        }
        // Just print the first result here.
        for (SpeechRecognitionResult result : response.get().getResultsList()) {
            // 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);
            // Print out the result
            System.out.printf("Transcript : %s\n", alternative.getTranscript());
            System.out.printf("Channel Tag : %s\n", result.getChannelTag());
        }
    }
}
Also used : LongRunningRecognizeResponse(com.google.cloud.speech.v1.LongRunningRecognizeResponse) SpeechRecognitionAlternative(com.google.cloud.speech.v1.SpeechRecognitionAlternative) RecognitionAudio(com.google.cloud.speech.v1.RecognitionAudio) RecognitionConfig(com.google.cloud.speech.v1.RecognitionConfig) StreamingRecognitionConfig(com.google.cloud.speech.v1.StreamingRecognitionConfig) SpeechClient(com.google.cloud.speech.v1.SpeechClient) SpeechRecognitionResult(com.google.cloud.speech.v1.SpeechRecognitionResult) LongRunningRecognizeMetadata(com.google.cloud.speech.v1.LongRunningRecognizeMetadata)

Example 15 with LongRunningRecognizeMetadata

use of com.google.cloud.speech.v1.LongRunningRecognizeMetadata in project java-speech by googleapis.

the class RecognizeBeta method transcribeWordLevelConfidenceGcs.

// [END speech_transcribe_word_level_confidence_beta]
// [START speech_transcribe_word_level_confidence_gcs_beta]
/**
 * Transcribe a remote audio file with word level confidence
 *
 * @param gcsUri path to the remote audio file
 */
public static void transcribeWordLevelConfidenceGcs(String gcsUri) throws Exception {
    try (SpeechClient speechClient = SpeechClient.create()) {
        // Configure request to enable word level confidence
        RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.FLAC).setSampleRateHertz(44100).setLanguageCode("en-US").setEnableWordConfidence(true).build();
        // Set the remote path for the audio file
        RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).build();
        // Use non-blocking call for getting file transcription
        OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response = speechClient.longRunningRecognizeAsync(config, audio);
        while (!response.isDone()) {
            System.out.println("Waiting for response...");
            Thread.sleep(10000);
        }
        // Just print the first result here.
        SpeechRecognitionResult result = response.get().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);
        // Print out the result
        System.out.printf("Transcript : %s\n", alternative.getTranscript());
        System.out.format("First Word and Confidence : %s %s \n", alternative.getWords(0).getWord(), alternative.getWords(0).getConfidence());
    }
}
Also used : LongRunningRecognizeResponse(com.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse) SpeechRecognitionAlternative(com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative) RecognitionAudio(com.google.cloud.speech.v1p1beta1.RecognitionAudio) 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)

Aggregations

LongRunningRecognizeMetadata (com.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata)9 LongRunningRecognizeResponse (com.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse)9 RecognitionAudio (com.google.cloud.speech.v1p1beta1.RecognitionAudio)9 RecognitionConfig (com.google.cloud.speech.v1p1beta1.RecognitionConfig)9 SpeechClient (com.google.cloud.speech.v1p1beta1.SpeechClient)9 SpeechRecognitionAlternative (com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative)8 LongRunningRecognizeMetadata (com.google.cloud.speech.v1.LongRunningRecognizeMetadata)7 LongRunningRecognizeResponse (com.google.cloud.speech.v1.LongRunningRecognizeResponse)7 RecognitionAudio (com.google.cloud.speech.v1.RecognitionAudio)7 RecognitionConfig (com.google.cloud.speech.v1.RecognitionConfig)7 SpeechClient (com.google.cloud.speech.v1.SpeechClient)7 SpeechRecognitionAlternative (com.google.cloud.speech.v1.SpeechRecognitionAlternative)7 SpeechRecognitionResult (com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult)7 SpeechRecognitionResult (com.google.cloud.speech.v1.SpeechRecognitionResult)6 StreamingRecognitionConfig (com.google.cloud.speech.v1.StreamingRecognitionConfig)6 StreamingRecognitionConfig (com.google.cloud.speech.v1p1beta1.StreamingRecognitionConfig)4 ByteString (com.google.protobuf.ByteString)3 WordInfo (com.google.cloud.speech.v1.WordInfo)2 WordInfo (com.google.cloud.speech.v1p1beta1.WordInfo)2 Path (java.nio.file.Path)2