IBM Watson演讲文本仅使用Java SDK返回第一个单词

时间:2016-03-07 21:56:25

标签: java speech-to-text ibm-watson

我正在使用IBM Watson语音来编写Java SDK文本,当我上传.wav文件时,响应JSON只包含第一个转录的单词。当我将相同的文件上传到网络演示时,我得到完整的回复。

使用SDK实现非常简单:

SpeechToText service = new SpeechToText();
service.setUsernameAndPassword("<username>", "<password>");

File audio = new File("src/test/resources/sample1.wav");

SpeechResults transcript = service.recognize(audio, HttpMediaType.AUDIO_WAV);
System.out.println(transcript);

2 个答案:

答案 0 :(得分:2)

您使用的recognize()签名将在第一次暂停后返回。要查看所有结果,请执行以下操作:

RecognizeOptions options = new RecognizeOptions();
options = options.continuous(true)
          .contentType(HttpMediaType.AUDIO_WAV)
          .interimResults(false)
          .inactivityTimeout(10)
          .maxAlternatives(1)
          .wordConfidence(false)
          .timestamps(true)
          .model("en-US_BroadbandModel");
SpeechResults transcript = service.recognize(audio, options);

这适用于我使用以下maven依赖项:

<dependency>
  <groupId>com.ibm.watson.developer_cloud</groupId>
  <artifactId>java-sdk</artifactId>
  <version>2.8.0</version>
</dependency>

答案 1 :(得分:0)

我正在尝试完全相同的事情,使用Chris K.描述的相同参数(“interim results = true”除外)

    FileInputStream audio = new FileInputStream("/home/leoks/BM/ws/mp32wav/out.wav");

    RecognizeOptions options = new RecognizeOptions();
    options = options.continuous(true)
              .contentType(HttpMediaType.AUDIO_WAV)
              .interimResults(true)
              .inactivityTimeout(10)
              .maxAlternatives(1)
              .wordConfidence(false)
              .timestamps(true)
              .model("en-US_BroadbandModel");

    api.stt.recognizeUsingWebSockets(audio, options, new BaseRecognizeDelegate() {
      @Override
      public void onMessage(SpeechResults speechResults) {
        System.out.println(speechResults);
        try{
            if (speechResults != null && speechResults.isFinal()){
              lock.countDown();
            }
        }catch(java.lang.IndexOutOfBoundsException ignored){
        }
      }
    });

    lock.await(5, TimeUnit.SECONDS);

但是,它仍然会抛出异常

java.lang.NullPointerException
    at java.io.StringReader.<init>(StringReader.java:50)
    at com.google.gson.JsonParser.parse(JsonParser.java:45)
    at com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.WebSocketSpeechToTextClient$WebSocketListener.onTextMessage(WebSocketSpeechToTextClient.java:66)
    at com.neovisionaries.ws.client.ListenerManager.callOnTextMessage(ListenerManager.java:352)
    at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:233)
    at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:211)
    at com.neovisionaries.ws.client.ReadingThread.handleTextFrame(ReadingThread.java:910)
    at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:693)
    at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:102)
    at com.neovisionaries.ws.client.ReadingThread.run(ReadingThread.java:61)
java.lang.NullPointerException
    at java.io.StringReader.<init>(StringReader.java:50)
    at com.google.gson.JsonParser.parse(JsonParser.java:45)
    at com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.WebSocketSpeechToTextClient$WebSocketListener.onTextMessage(WebSocketSpeechToTextClient.java:66)
    at com.neovisionaries.ws.client.ListenerManager.callOnTextMessage(ListenerManager.java:352)
    at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:233)
    at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:211)
    at com.neovisionaries.ws.client.ReadingThread.handleTextFrame(ReadingThread.java:910)
    at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:693)
    at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:102)
    at com.neovisionaries.ws.client.ReadingThread.run(ReadingThread.java:61)
java.lang.NullPointerException
    at java.io.StringReader.<init>(StringReader.java:50)
    at com.google.gson.JsonParser.parse(JsonParser.java:45)
    at com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.WebSocketSpeechToTextClient$WebSocketListener.onTextMessage(WebSocketSpeechToTextClient.java:66)
    at com.neovisionaries.ws.client.ListenerManager.callOnTextMessage(ListenerManager.java:352)
    at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:233)
    at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:211)
    at com.neovisionaries.ws.client.ReadingThread.handleTextFrame(ReadingThread.java:910)
    at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:693)
    at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:102)
    at com.neovisionaries.ws.client.ReadingThread.run(ReadingThread.java:61)

似乎与此问题有关

https://github.com/watson-developer-cloud/java-sdk/issues/205

设置为在里程碑2.9.0(当前为2.8.0)

中达到