Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOException {
- // Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
- SpeechClient speech = SpeechClient.create();
- long seconds = 0;
- long minutes = 0;
- // Configure remote file request for Linear16
- RecognitionConfig config = RecognitionConfig.newBuilder()
- .setEncoding(AudioEncoding.LINEAR16)
- .setLanguageCode("en-US") /*en-US*/ /*fr-CA*/
- .setSampleRateHertz(44100)
- .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.
- for (int i=0; i<result.getAlternativesCount();i++){
- seconds = result.getAlternativesList().get(i).getWordsList().get(0).getStartTimeOrBuilder().getSeconds();
- minutes = (int) (seconds / 60.0);
- HelloAppEngine.textTransalated = HelloAppEngine.textTransalated + "rn[" + minutes + "m et " + (seconds % 60) + "s][" + result.getAlternativesList().get(i).getConfidence() + "] " + result.getAlternativesList().get(i).getTranscript();
- }
- /*SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
- System.out.printf("Transcription: %sn",alternative.getTranscript());
- HelloAppEngine.textTransalated = alternative.getTranscript();*/
- }
- speech.close();
- }
Add Comment
Please, Sign In to add comment