Guest User

Untitled

a guest
Feb 25th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOException {
  2. // Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
  3. SpeechClient speech = SpeechClient.create();
  4. long seconds = 0;
  5. long minutes = 0;
  6.  
  7. // Configure remote file request for Linear16
  8. RecognitionConfig config = RecognitionConfig.newBuilder()
  9. .setEncoding(AudioEncoding.LINEAR16)
  10. .setLanguageCode("en-US") /*en-US*/ /*fr-CA*/
  11. .setSampleRateHertz(44100)
  12. .setEnableWordTimeOffsets(true)
  13. .build();
  14. RecognitionAudio audio = RecognitionAudio.newBuilder()
  15. .setUri(gcsUri)
  16. .build();
  17.  
  18. // Use non-blocking call for getting file transcription
  19. OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response =
  20. speech.longRunningRecognizeAsync(config, audio);
  21. while (!response.isDone()) {
  22. System.out.println("Waiting for response...");
  23. /*Thread.sleep(10000);*/
  24. }
  25.  
  26. List<SpeechRecognitionResult> results = response.get().getResultsList();
  27.  
  28. for (SpeechRecognitionResult result: results) {
  29. // There can be several alternative transcripts for a given chunk of speech. Just use the
  30. // first (most likely) one here.
  31. for (int i=0; i<result.getAlternativesCount();i++){
  32. seconds = result.getAlternativesList().get(i).getWordsList().get(0).getStartTimeOrBuilder().getSeconds();
  33. minutes = (int) (seconds / 60.0);
  34. HelloAppEngine.textTransalated = HelloAppEngine.textTransalated + "rn[" + minutes + "m et " + (seconds % 60) + "s][" + result.getAlternativesList().get(i).getConfidence() + "] " + result.getAlternativesList().get(i).getTranscript();
  35. }
  36.  
  37. /*SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
  38. System.out.printf("Transcription: %sn",alternative.getTranscript());
  39. HelloAppEngine.textTransalated = alternative.getTranscript();*/
  40. }
  41. speech.close();
  42. }
Add Comment
Please, Sign In to add comment