Search code examples
speech-to-textibm-watson

How to use keyword spotting feature for IBM Waston Speech to text API?


I am using IBM Watson Speech to text API to convert audio files into text. Every feature is working fine for me. But I am unable to use the Keyword Spotting feature. The output is not giving any info regarding spotted keywords.

Here is my code:

SpeechToText service = new SpeechToText();
    service.setUsernameAndPassword("*********", "********");
    //SpeechModel model =service.getModel("en-US_NarrowbandModel");


    service.setEndPoint("https://stream.watsonplatform.net/speech-to-text/api");

    String[] keys= {"abuse","bullying","parents","physical","assaulting"};
    RecognizeOptions options = new RecognizeOptions().contentType("audio/wav").model("en-US_NarrowbandModel").continuous(true).inactivityTimeout(500).keywords(keys).keywordsThreshold(0.7);


    File audio = new File("C:\\Users\\AudioFiles\\me.wav");

    SpeechResults transcript = service.recognize(audio, options);
    //Speech t1 = service.recognize(audio, options);
    System.out.println(transcript);

Is there any special function to get the spotted keywords as output as well with the transcript?


Solution

  • This was fixed in the Java SDK v3.2.0. Make sure you download the latest version (4.2.1) jar: java-sdk-4.2.1-jar-with-dependencies.jar or update your Gradle/Maven to pull the latest version.

    The code below is based on the code in your question.

    SpeechToText service = new SpeechToText();
    service.setUsernameAndPassword("USERNAME", "PASSWORD");
    
    File audio = new File("C:\\Users\\AudioFiles\\me.wav");    
    
    RecognizeOptions options = new RecognizeOptions().Builder()
      .contentType("audio/wav)
      .inactivityTimeout(500)
      .keywords({"abuse", "bullying", "parents", "physical", "assaulting"})
      .keywordsThreshold(0.5)
      .build();
    
      SpeechResults transcript = service.recognize(audio, options).execute();
      System.out.println(transcript);