use of com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults in project java-sdk by watson-developer-cloud.
the class MicrophoneWithWebSocketsExample method main.
/**
* The main method.
*
* @param args the arguments
* @throws Exception the exception
*/
public static void main(final String[] args) throws Exception {
SpeechToText service = new SpeechToText();
service.setUsernameAndPassword("<username>", "<password>");
// Signed PCM AudioFormat with 16kHz, 16 bit sample size, mono
int sampleRate = 16000;
AudioFormat format = new AudioFormat(sampleRate, 16, 1, true, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line not supported");
System.exit(0);
}
TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();
AudioInputStream audio = new AudioInputStream(line);
RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).interimResults(true).timestamps(true).wordConfidence(true).contentType(HttpMediaType.AUDIO_RAW + ";rate=" + sampleRate).build();
service.recognizeUsingWebSocket(options, new BaseRecognizeCallback() {
@Override
public void onTranscription(SpeechRecognitionResults speechResults) {
System.out.println(speechResults);
}
});
System.out.println("Listening to your voice for the next 30s...");
Thread.sleep(30 * 1000);
// closing the WebSockets underlying InputStream will close the WebSocket itself.
line.stop();
line.close();
System.out.println("Fin.");
}
use of com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults in project java-sdk by watson-developer-cloud.
the class RecognizeUsingWebSocketsExample method main.
public static void main(String[] args) throws FileNotFoundException, InterruptedException {
SpeechToText service = new SpeechToText();
service.setUsernameAndPassword("<username>", "<password>");
FileInputStream audio = new FileInputStream("src/test/resources/speech_to_text/sample1.wav");
RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).interimResults(true).contentType(HttpMediaType.AUDIO_WAV).build();
service.recognizeUsingWebSocket(options, new BaseRecognizeCallback() {
@Override
public void onTranscription(SpeechRecognitionResults speechResults) {
System.out.println(speechResults);
}
@Override
public void onDisconnected() {
lock.countDown();
}
});
lock.await(1, TimeUnit.MINUTES);
}
use of com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults in project java-sdk by watson-developer-cloud.
the class RecognizeUsingWebSocketsWithSpeakerLabelsExample method main.
/**
* The main method.
*
* @param args the arguments
* @throws FileNotFoundException the file not found exception
* @throws InterruptedException the interrupted exception
*/
public static void main(String[] args) throws FileNotFoundException, InterruptedException {
FileInputStream audio = new FileInputStream("src/test/resources/speech_to_text/twospeakers.wav");
SpeechToText service = new SpeechToText();
service.setUsernameAndPassword("<username>", "<password>");
RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).interimResults(true).speakerLabels(true).model(RecognizeOptions.EN_US_NARROWBANDMODEL).contentType(HttpMediaType.AUDIO_WAV).build();
RecoTokens recoTokens = new RecoTokens();
service.recognizeUsingWebSocket(options, new BaseRecognizeCallback() {
@Override
public void onTranscription(SpeechRecognitionResults speechResults) {
recoTokens.add(speechResults);
}
@Override
public void onDisconnected() {
lock.countDown();
}
});
lock.await(1, TimeUnit.MINUTES);
}
use of com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults in project java-sdk by watson-developer-cloud.
the class SpeechToTextExample method main.
public static void main(String[] args) {
SpeechToText service = new SpeechToText();
service.setUsernameAndPassword("<username>", "<password>");
File audio = new File("src/test/resources/speech_to_text/sample1.wav");
RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).contentType(RecognizeOptions.ContentType.AUDIO_WAV).build();
SpeechRecognitionResults transcript = service.recognize(options).execute();
System.out.println(transcript);
}
use of com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults in project java-sdk by watson-developer-cloud.
the class SpeechToTextIT method testRecognizeFileString.
/**
* Test recognize audio file.
*/
@Test
public void testRecognizeFileString() throws FileNotFoundException {
Long maxAlternatives = 3L;
Float wordAlternativesThreshold = 0.8f;
File audio = new File(SAMPLE_WAV);
RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).contentType(RecognizeOptions.ContentType.AUDIO_WAV).maxAlternatives(maxAlternatives).wordAlternativesThreshold(wordAlternativesThreshold).smartFormatting(true).build();
SpeechRecognitionResults results = service.recognize(options).execute();
assertNotNull(results.getResults().get(0).getAlternatives().get(0).getTranscript());
assertTrue(results.getResults().get(0).getAlternatives().size() <= maxAlternatives);
List<WordAlternativeResults> wordAlternatives = results.getResults().get(0).getWordAlternatives();
for (WordAlternativeResults alternativeResults : wordAlternatives) {
assertTrue(alternativeResults.getAlternatives().get(0).getConfidence() >= wordAlternativesThreshold);
}
}
Aggregations