@@ -47,7 +47,7 @@ public static void main(String... args) throws Exception {
4747 System .out .printf (
4848 "\t java %s \" <command>\" \" <path-to-image>\" \n "
4949 + "Commands:\n "
50- + "\t model-selection | metadata | diarization | multi-channel |\n "
50+ + "\t metadata | diarization | multi-channel |\n "
5151 + "\t multi-language | word-level-conf\n "
5252 + "Path:\n \t A file path (ex: ./resources/audio.raw) or a URI "
5353 + "for a Cloud Storage resource (gs://...)\n " ,
@@ -58,13 +58,7 @@ public static void main(String... args) throws Exception {
5858 String path = args .length > 1 ? args [1 ] : "" ;
5959
6060 // Use command and GCS path pattern to invoke transcription.
61- if (command .equals ("model-selection" )) {
62- if (path .startsWith ("gs://" )) {
63- transcribeModelSelectionGcs (path );
64- } else {
65- transcribeModelSelection (path );
66- }
67- } else if (command .equals ("metadata" )) {
61+ if (command .equals ("metadata" )) {
6862 transcribeFileWithMetadata (path );
6963 } else if (command .equals ("diarization" )) {
7064 if (path .startsWith ("gs://" )) {
@@ -93,86 +87,7 @@ public static void main(String... args) throws Exception {
9387 }
9488 }
9589
96- // [START speech_transcribe_model_selection_beta]
97- /**
98- * Performs transcription of the given audio file synchronously with the selected model.
99- *
100- * @param fileName the path to a audio file to transcribe
101- */
102- public static void transcribeModelSelection (String fileName ) throws Exception {
103- Path path = Paths .get (fileName );
104- byte [] content = Files .readAllBytes (path );
105-
106- try (SpeechClient speech = SpeechClient .create ()) {
107- // Configure request with video media type
108- RecognitionConfig recConfig =
109- RecognitionConfig .newBuilder ()
110- // encoding may either be omitted or must match the value in the file header
111- .setEncoding (AudioEncoding .LINEAR16 )
112- .setLanguageCode ("en-US" )
113- // sample rate hertz may be either be omitted or must match the value in the file
114- // header
115- .setSampleRateHertz (16000 )
116- .setModel ("video" )
117- .build ();
118-
119- RecognitionAudio recognitionAudio =
120- RecognitionAudio .newBuilder ().setContent (ByteString .copyFrom (content )).build ();
121-
122- RecognizeResponse recognizeResponse = speech .recognize (recConfig , recognitionAudio );
123- // Just print the first result here.
124- SpeechRecognitionResult result = recognizeResponse .getResultsList ().get (0 );
125- // There can be several alternative transcripts for a given chunk of speech. Just use the
126- // first (most likely) one here.
127- SpeechRecognitionAlternative alternative = result .getAlternativesList ().get (0 );
128- System .out .printf ("Transcript : %s\n " , alternative .getTranscript ());
129- }
130- // [END speech_transcribe_model_selection_beta]
131- }
132-
133- // [START speech_transcribe_model_selection_gcs_beta]
134- /**
135- * Performs transcription of the remote audio file asynchronously with the selected model.
136- *
137- * @param gcsUri the path to the remote audio file to transcribe.
138- */
139- public static void transcribeModelSelectionGcs (String gcsUri ) throws Exception {
140- try (SpeechClient speech = SpeechClient .create ()) {
141-
142- // Configure request with video media type
143- RecognitionConfig config =
144- RecognitionConfig .newBuilder ()
145- // encoding may either be omitted or must match the value in the file header
146- .setEncoding (AudioEncoding .LINEAR16 )
147- .setLanguageCode ("en-US" )
148- // sample rate hertz may be either be omitted or must match the value in the file
149- // header
150- .setSampleRateHertz (16000 )
151- .setModel ("video" )
152- .build ();
153-
154- RecognitionAudio audio = RecognitionAudio .newBuilder ().setUri (gcsUri ).build ();
155-
156- // Use non-blocking call for getting file transcription
157- OperationFuture <LongRunningRecognizeResponse , LongRunningRecognizeMetadata > response =
158- speech .longRunningRecognizeAsync (config , audio );
15990
160- while (!response .isDone ()) {
161- System .out .println ("Waiting for response..." );
162- Thread .sleep (10000 );
163- }
164-
165- List <SpeechRecognitionResult > results = response .get ().getResultsList ();
166-
167- // Just print the first result here.
168- SpeechRecognitionResult result = results .get (0 );
169- // There can be several alternative transcripts for a given chunk of speech. Just use the
170- // first (most likely) one here.
171- SpeechRecognitionAlternative alternative = result .getAlternativesList ().get (0 );
172- System .out .printf ("Transcript : %s\n " , alternative .getTranscript ());
173- }
174- }
175- // [END speech_transcribe_model_selection_gcs_beta]
17691
17792 // [START speech_transcribe_recognition_metadata_beta]
17893 /**
0 commit comments