Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <dependency>
- <groupId>com.google.cloud</groupId>
- <artifactId>google-cloud-speech</artifactId>
- <version>0.80.0-beta</version>
- </dependency>
- String jsonFilePath = System.getProperty("user.dir") + "/serviceaccount.json";
- FileInputStream credentialsStream = new FileInputStream(jsonFilePath);
- GoogleCredentials credentials = GoogleCredentials.fromStream(credentialsStream);
- FixedCredentialsProvider credentialsProvider = FixedCredentialsProvider.create(credentials);
- SpeechSettings speechSettings =
- SpeechSettings.newBuilder()
- .setCredentialsProvider(credentialsProvider)
- .build();
- SpeechClient speechClient = SpeechClient.create(speechSettings);
- //SpeechClient speechClient = SpeechClient.create();
- // The path to the audio file to transcribe
- String fileName = System.getProperty("user.dir") + "/call-recording-790.opus";
- // Reads the audio file into memory
- Path path = Paths.get(fileName);
- byte[] data = Files.readAllBytes(path);
- ByteString audioBytes = ByteString.copyFrom(data);
- System.out.println(path.toAbsolutePath());
- // Builds the sync recognize request
- RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.LINEAR16)
- .setSampleRateHertz(8000).setLanguageCode("en-US").build();
- RecognitionAudio audio = RecognitionAudio.newBuilder().setContent(audioBytes).build();
- System.out.println("recognize builder");
- // Performs speech recognition on the audio file
- RecognizeResponse response = speechClient.recognize(config, audio);
- List<SpeechRecognitionResult> results = response.getResultsList();
- System.out.println(results.size()); // ***** HERE 0
- for (SpeechRecognitionResult result : results) {
- System.out.println(result.toString());
- // There can be several alternative transcripts for a given chunk of speech.
- // Just use the
- // first (most likely) one here.
- SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
- System.out.printf("Transcription: %s%n", alternative.getTranscript());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement