Search in sources :

Example 11 with WordInfo

use of com.google.cloud.speech.v1.WordInfo in project java-video-intelligence by googleapis.

the class Detect method speechTranscription.

/**
 * Transcribe speech from a video stored on GCS.
 *
 * @param gcsUri the path to the video file to analyze.
 */
public static void speechTranscription(String gcsUri) throws Exception {
    // Instantiate a com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient
    try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) {
        // Set the language code
        SpeechTranscriptionConfig config = SpeechTranscriptionConfig.newBuilder().setLanguageCode("en-US").setEnableAutomaticPunctuation(true).build();
        // Set the video context with the above configuration
        VideoContext context = VideoContext.newBuilder().setSpeechTranscriptionConfig(config).build();
        // Create the request
        AnnotateVideoRequest request = AnnotateVideoRequest.newBuilder().setInputUri(gcsUri).addFeatures(Feature.SPEECH_TRANSCRIPTION).setVideoContext(context).build();
        // asynchronously perform speech transcription on videos
        OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> response = client.annotateVideoAsync(request);
        System.out.println("Waiting for operation to complete...");
        // Display the results
        for (VideoAnnotationResults results : response.get(600, TimeUnit.SECONDS).getAnnotationResultsList()) {
            for (SpeechTranscription speechTranscription : results.getSpeechTranscriptionsList()) {
                try {
                    // Print the transcription
                    if (speechTranscription.getAlternativesCount() > 0) {
                        SpeechRecognitionAlternative alternative = speechTranscription.getAlternatives(0);
                        System.out.printf("Transcript: %s\n", alternative.getTranscript());
                        System.out.printf("Confidence: %.2f\n", alternative.getConfidence());
                        System.out.println("Word level information:");
                        for (WordInfo wordInfo : alternative.getWordsList()) {
                            double startTime = wordInfo.getStartTime().getSeconds() + wordInfo.getStartTime().getNanos() / 1e9;
                            double endTime = wordInfo.getEndTime().getSeconds() + wordInfo.getEndTime().getNanos() / 1e9;
                            System.out.printf("\t%4.2fs - %4.2fs: %s\n", startTime, endTime, wordInfo.getWord());
                        }
                    } else {
                        System.out.println("No transcription found");
                    }
                } catch (IndexOutOfBoundsException ioe) {
                    System.out.println("Could not retrieve frame: " + ioe.getMessage());
                }
            }
        }
    }
// [END video_speech_transcription_gcs]
}
Also used : AnnotateVideoRequest(com.google.cloud.videointelligence.v1.AnnotateVideoRequest) VideoContext(com.google.cloud.videointelligence.v1.VideoContext) VideoIntelligenceServiceClient(com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient) AnnotateVideoProgress(com.google.cloud.videointelligence.v1.AnnotateVideoProgress) SpeechRecognitionAlternative(com.google.cloud.videointelligence.v1.SpeechRecognitionAlternative) SpeechTranscriptionConfig(com.google.cloud.videointelligence.v1.SpeechTranscriptionConfig) VideoAnnotationResults(com.google.cloud.videointelligence.v1.VideoAnnotationResults) SpeechTranscription(com.google.cloud.videointelligence.v1.SpeechTranscription) WordInfo(com.google.cloud.videointelligence.v1.WordInfo) AnnotateVideoResponse(com.google.cloud.videointelligence.v1.AnnotateVideoResponse)

Example 12 with WordInfo

use of com.google.cloud.speech.v1.WordInfo in project java-video-intelligence by googleapis.

the class Detect method speechTranscription.

/**
 * Transcribe speech from a video stored on GCS.
 *
 * @param gcsUri the path to the video file to analyze.
 */
public static void speechTranscription(String gcsUri) throws Exception {
    // Instantiate a com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient
    try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) {
        // Set the language code
        SpeechTranscriptionConfig config = SpeechTranscriptionConfig.newBuilder().setLanguageCode("en-US").setEnableAutomaticPunctuation(true).build();
        // Set the video context with the above configuration
        VideoContext context = VideoContext.newBuilder().setSpeechTranscriptionConfig(config).build();
        // Create the request
        AnnotateVideoRequest request = AnnotateVideoRequest.newBuilder().setInputUri(gcsUri).addFeatures(Feature.SPEECH_TRANSCRIPTION).setVideoContext(context).build();
        // asynchronously perform speech transcription on videos
        OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> response = client.annotateVideoAsync(request);
        System.out.println("Waiting for operation to complete...");
        // Display the results
        for (VideoAnnotationResults results : response.get(600, TimeUnit.SECONDS).getAnnotationResultsList()) {
            for (SpeechTranscription speechTranscription : results.getSpeechTranscriptionsList()) {
                try {
                    // Print the transcription
                    if (speechTranscription.getAlternativesCount() > 0) {
                        SpeechRecognitionAlternative alternative = speechTranscription.getAlternatives(0);
                        System.out.printf("Transcript: %s\n", alternative.getTranscript());
                        System.out.printf("Confidence: %.2f\n", alternative.getConfidence());
                        System.out.println("Word level information:");
                        for (WordInfo wordInfo : alternative.getWordsList()) {
                            double startTime = wordInfo.getStartTime().getSeconds() + wordInfo.getStartTime().getNanos() / 1e9;
                            double endTime = wordInfo.getEndTime().getSeconds() + wordInfo.getEndTime().getNanos() / 1e9;
                            System.out.printf("\t%4.2fs - %4.2fs: %s\n", startTime, endTime, wordInfo.getWord());
                        }
                    } else {
                        System.out.println("No transcription found");
                    }
                } catch (IndexOutOfBoundsException ioe) {
                    System.out.println("Could not retrieve frame: " + ioe.getMessage());
                }
            }
        }
    }
// [END video_speech_transcription_gcs]
}
Also used : AnnotateVideoRequest(com.google.cloud.videointelligence.v1.AnnotateVideoRequest) VideoContext(com.google.cloud.videointelligence.v1.VideoContext) VideoIntelligenceServiceClient(com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient) AnnotateVideoProgress(com.google.cloud.videointelligence.v1.AnnotateVideoProgress) SpeechRecognitionAlternative(com.google.cloud.videointelligence.v1.SpeechRecognitionAlternative) SpeechTranscriptionConfig(com.google.cloud.videointelligence.v1.SpeechTranscriptionConfig) VideoAnnotationResults(com.google.cloud.videointelligence.v1.VideoAnnotationResults) SpeechTranscription(com.google.cloud.videointelligence.v1.SpeechTranscription) WordInfo(com.google.cloud.videointelligence.v1.WordInfo) AnnotateVideoResponse(com.google.cloud.videointelligence.v1.AnnotateVideoResponse)

Aggregations

RecognitionAudio (com.google.cloud.speech.v1.RecognitionAudio)4 RecognitionConfig (com.google.cloud.speech.v1.RecognitionConfig)4 SpeechClient (com.google.cloud.speech.v1.SpeechClient)4 SpeechRecognitionAlternative (com.google.cloud.speech.v1.SpeechRecognitionAlternative)4 WordInfo (com.google.cloud.speech.v1.WordInfo)4 LongRunningRecognizeResponse (com.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse)4 RecognitionAudio (com.google.cloud.speech.v1p1beta1.RecognitionAudio)4 RecognitionConfig (com.google.cloud.speech.v1p1beta1.RecognitionConfig)4 SpeechClient (com.google.cloud.speech.v1p1beta1.SpeechClient)4 SpeechRecognitionAlternative (com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative)4 WordInfo (com.google.cloud.speech.v1p1beta1.WordInfo)4 Path (java.nio.file.Path)4 LongRunningRecognizeResponse (com.google.cloud.speech.v1.LongRunningRecognizeResponse)3 LongRunningRecognizeMetadata (com.google.cloud.speech.v1.LongRunningRecognizeMetadata)2 RecognizeResponse (com.google.cloud.speech.v1.RecognizeResponse)2 SpeakerDiarizationConfig (com.google.cloud.speech.v1.SpeakerDiarizationConfig)2 SpeechRecognitionResult (com.google.cloud.speech.v1.SpeechRecognitionResult)2 StreamingRecognitionConfig (com.google.cloud.speech.v1.StreamingRecognitionConfig)2 LongRunningRecognizeMetadata (com.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata)2 RecognizeResponse (com.google.cloud.speech.v1p1beta1.RecognizeResponse)2