Search in sources :

Example 11 with SpeechRecognitionResults

use of com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults in project java-sdk by watson-developer-cloud.

the class SpeechToTextIT method testRecognizeWithSilence.

/**
 * Test recognize with silence.
 *
 * @throws FileNotFoundException the file not found exception
 */
@Test
public void testRecognizeWithSilence() throws FileNotFoundException {
    File audio = new File(SAMPLE_WAV_WITH_PAUSE);
    // Make call with a long end-of-phrase silence time.
    RecognizeOptions firstOptions = new RecognizeOptions.Builder().audio(audio).contentType(HttpMediaType.AUDIO_WAV).endOfPhraseSilenceTime(100.0).splitTranscriptAtPhraseEnd(true).build();
    SpeechRecognitionResults results = service.recognize(firstOptions).execute().getResult();
    assertEquals(1, results.getResults().size());
    // Make call again with a short end-of-phrase silence time, which should return
    // multiple
    // results.
    RecognizeOptions secondOptions = new RecognizeOptions.Builder().audio(audio).contentType(HttpMediaType.AUDIO_WAV).endOfPhraseSilenceTime(0.1).splitTranscriptAtPhraseEnd(true).build();
    results = service.recognize(secondOptions).execute().getResult();
    assertTrue(results.getResults().size() > 1);
    assertEquals(SpeechRecognitionResult.EndOfUtterance.SILENCE, results.getResults().get(0).getEndOfUtterance());
}
Also used : File(java.io.File) SpeechRecognitionResults(com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults) RecognizeOptions(com.ibm.watson.speech_to_text.v1.model.RecognizeOptions) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 12 with SpeechRecognitionResults

use of com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults in project java-sdk by watson-developer-cloud.

the class SpeechToTextIT method testRecognizeMultipleSpeakers.

/**
 * Test recognize multiple speakers.
 *
 * @throws FileNotFoundException the file not found exception
 */
@Test
public void testRecognizeMultipleSpeakers() throws FileNotFoundException {
    File audio = new File(TWO_SPEAKERS_WAV);
    RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).speakerLabels(true).model(RecognizeOptions.Model.EN_US_NARROWBANDMODEL).contentType(HttpMediaType.AUDIO_WAV).build();
    SpeechRecognitionResults results = service.recognize(options).execute().getResult();
    assertNotNull(results.getSpeakerLabels());
    assertTrue(results.getSpeakerLabels().size() > 0);
}
Also used : File(java.io.File) SpeechRecognitionResults(com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults) RecognizeOptions(com.ibm.watson.speech_to_text.v1.model.RecognizeOptions) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 13 with SpeechRecognitionResults

use of com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults in project java-sdk by watson-developer-cloud.

the class SpeechToTextIT method testRecognizeKeywords.

/**
 * Test keyword recognition.
 *
 * @throws FileNotFoundException the file not found exception
 */
@Test
public void testRecognizeKeywords() throws FileNotFoundException {
    final String keyword1 = "rain";
    final String keyword2 = "tornadoes";
    final File audio = new File(SAMPLE_WAV);
    final RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).contentType(HttpMediaType.AUDIO_WAV).model(RecognizeOptions.Model.EN_US_NARROWBANDMODEL).inactivityTimeout(500).keywords(Arrays.asList(keyword1, keyword2)).keywordsThreshold(0.5f).build();
    final SpeechRecognitionResults results = service.recognize(options).execute().getResult();
    final SpeechRecognitionResult transcript = results.getResults().get(0);
    assertEquals(2, transcript.getKeywordsResult().size());
    assertTrue(transcript.getKeywordsResult().containsKey(keyword1));
    assertTrue(transcript.getKeywordsResult().containsKey(keyword2));
    final KeywordResult result1 = transcript.getKeywordsResult().get(keyword1).get(0);
    assertEquals(keyword1, result1.getNormalizedText());
    assertEquals(0.9, result1.getConfidence(), 0.1);
    assertEquals(5.58, result1.getStartTime(), 1.0);
    assertEquals(6.14, result1.getEndTime(), 1.0);
    final KeywordResult result2 = transcript.getKeywordsResult().get(keyword2).get(0);
    assertEquals(keyword2, result2.getNormalizedText());
    assertEquals(0.9, result2.getConfidence(), 0.1);
    assertEquals(4.42, result2.getStartTime(), 1.0);
    assertEquals(5.04, result2.getEndTime(), 1.0);
}
Also used : File(java.io.File) SpeechRecognitionResult(com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResult) SpeechRecognitionResults(com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults) RecognizeOptions(com.ibm.watson.speech_to_text.v1.model.RecognizeOptions) KeywordResult(com.ibm.watson.speech_to_text.v1.model.KeywordResult) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 14 with SpeechRecognitionResults

use of com.ibm.watson.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);
}
Also used : BaseRecognizeCallback(com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeCallback) FileInputStream(java.io.FileInputStream) SpeechRecognitionResults(com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults) RecognizeOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions)

Example 15 with SpeechRecognitionResults

use of com.ibm.watson.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);
}
Also used : BaseRecognizeCallback(com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeCallback) FileInputStream(java.io.FileInputStream) SpeechRecognitionResults(com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults) RecognizeOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions)

Aggregations

Test (org.junit.Test)18 SpeechRecognitionResults (com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults)17 RecognizeOptions (com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions)16 SpeechRecognitionResults (com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults)14 File (java.io.File)13 FileInputStream (java.io.FileInputStream)11 RecognizeOptions (com.ibm.watson.speech_to_text.v1.model.RecognizeOptions)8 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)7 MockResponse (okhttp3.mockwebserver.MockResponse)7 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)7 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)6 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)5 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)5 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)5 RecognizeWithWebsocketsOptions (com.ibm.watson.speech_to_text.v1.model.RecognizeWithWebsocketsOptions)5 BaseRecognizeCallback (com.ibm.watson.speech_to_text.v1.websocket.BaseRecognizeCallback)5 JsonObject (com.google.gson.JsonObject)4 JsonParser (com.google.gson.JsonParser)4 BaseRecognizeCallback (com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeCallback)4 ByteString (okio.ByteString)4