Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. <dependency>
  2. <groupId>com.google.cloud</groupId>
  3. <artifactId>google-cloud-speech</artifactId>
  4. <version>0.80.0-beta</version>
  5. </dependency>
  6.  
  7. String jsonFilePath = System.getProperty("user.dir") + "/serviceaccount.json";
  8. FileInputStream credentialsStream = new FileInputStream(jsonFilePath);
  9. GoogleCredentials credentials = GoogleCredentials.fromStream(credentialsStream);
  10. FixedCredentialsProvider credentialsProvider = FixedCredentialsProvider.create(credentials);
  11.  
  12. SpeechSettings speechSettings =
  13. SpeechSettings.newBuilder()
  14. .setCredentialsProvider(credentialsProvider)
  15. .build();
  16.  
  17. SpeechClient speechClient = SpeechClient.create(speechSettings);
  18.  
  19. //SpeechClient speechClient = SpeechClient.create();
  20.  
  21. // The path to the audio file to transcribe
  22. String fileName = System.getProperty("user.dir") + "/call-recording-790.opus";
  23.  
  24. // Reads the audio file into memory
  25. Path path = Paths.get(fileName);
  26. byte[] data = Files.readAllBytes(path);
  27. ByteString audioBytes = ByteString.copyFrom(data);
  28.  
  29. System.out.println(path.toAbsolutePath());
  30.  
  31. // Builds the sync recognize request
  32. RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.LINEAR16)
  33. .setSampleRateHertz(8000).setLanguageCode("en-US").build();
  34.  
  35. RecognitionAudio audio = RecognitionAudio.newBuilder().setContent(audioBytes).build();
  36.  
  37. System.out.println("recognize builder");
  38.  
  39. // Performs speech recognition on the audio file
  40. RecognizeResponse response = speechClient.recognize(config, audio);
  41. List<SpeechRecognitionResult> results = response.getResultsList();
  42.  
  43. System.out.println(results.size()); // ***** HERE 0
  44.  
  45. for (SpeechRecognitionResult result : results) {
  46.  
  47. System.out.println(result.toString());
  48.  
  49. // There can be several alternative transcripts for a given chunk of speech.
  50. // Just use the
  51. // first (most likely) one here.
  52. SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
  53. System.out.printf("Transcription: %s%n", alternative.getTranscript());
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement