Java IBM Watson使用WebSocket将语音转换为文本

Java IBM Watson使用WebSocket将语音转换为文本,java,ibm-cloud,speech-to-text,ibm-watson,Java,Ibm Cloud,Speech To Text,Ibm Watson,我正在尝试使用Watson Developer Cloud java SDK转录大型音频文件。我尝试了无会话方法,效果很好,但是当我尝试WebSockets方法时,事情变得不可靠 大多数情况下,该方法返回时不会向学员传递SpeechResult;它很少工作,但它只转录前几秒钟 这就是我的代码的样子: static SpeechResults transcript = null; private static String SpeechToText(String audioFile) throws

我正在尝试使用Watson Developer Cloud java SDK转录大型音频文件。我尝试了无会话方法,效果很好,但是当我尝试WebSockets方法时,事情变得不可靠

大多数情况下,该方法返回时不会向学员传递
SpeechResult
;它很少工作,但它只转录前几秒钟

这就是我的代码的样子:

static SpeechResults transcript = null;
private static String SpeechToText(String audioFile) throws FileNotFoundException {
        SpeechToText service = new SpeechToText();
        service.setUsernameAndPassword("<!!USERNAME!!>", "<!!PASSWORD!!>");
        service.setEndPoint("https://stream.watsonplatform.net/speech-to-text/api");

        RecognizeOptions options = new RecognizeOptions();
        options.contentType("audio/ogg;codecs=opus");
        options.continuous(Boolean.TRUE);
        options.inactivityTimeout(-1);
        options.model(Models.GetModelName(Models.SpeechModelEnums.ArabicBroadband));
        options.timestamps(Boolean.TRUE);
        options.wordAlternativesThreshold(0.5);
        options.wordConfidence(Boolean.TRUE);

        options.interimResults(Boolean.FALSE);

        File audio = new File(audioFile);

        //This is my sessionless call
        //SpeechResults transcript = service.recognize(audio, options);


        service.recognizeUsingWebSockets(new FileInputStream(audio),  options, new BaseRecognizeDelegate()
        {
                @Override
                public void onMessage(SpeechResults speechResults){
                System.out.println(speechResults);                
                }
            }
        );

        return "";//transcript.toString();
    } 
static SpeechResults transcript=null;
私有静态字符串SpeechToText(字符串音频文件)引发FileNotFoundException{
SpeechToText服务=新建SpeechToText();
service.setUserName和密码(“,”);
service.setEndPoint(“https://stream.watsonplatform.net/speech-to-text/api");
RecognizeOptions=newrecognizeoptions();
options.contentType(“音频/ogg;编解码器=作品”);
options.continuous(Boolean.TRUE);
选项。不活动超时(-1);
options.model(Models.GetModelName(Models.speechmodelenum.ArabicBroadband));
options.timestamps(Boolean.TRUE);
options.wordAlternativesThreshold(0.5);
options.wordConfidence(Boolean.TRUE);
options.interimResults(Boolean.FALSE);
文件音频=新文件(音频文件);
//这是我的无会话通话
//SpeechResults转录本=服务。识别(音频、选项);
服务。使用WebSocket(新文件输入流(音频)、选项、新BaseRecognitizeDelegate()进行识别
{
@凌驾
消息(SpeechResults SpeechResults)上的公共空白{
System.out.println(speechResults);
}
}
);
返回“”;//transcript.toString();
} 
我已经连续启用了。我试着摆弄一下交互结果,但没用


我做错了什么?

您提到的问题已在
3.0.0-RC1
版本中修复。
我有一个类似的问题,并添加了一个代码片段,可以使用WebSocket识别音频文件


3.0.0-RC1
开始,在中有一个WebSocket示例。

您正在使用哪个版本的SDK?我想他们最近已经取代了websockets库。(我自己也有同样的问题)这很有效,谢谢。但是,我注意到在示例中,您调用Thread.sleep()来等待异步调用。是否有更好的方法等待转录操作完成?那么我如何等待转录过程完成呢?是否有我应该处理的事件或回调?
SpeechResults
有一个方法。嗯,如果我使用continuous,我会在
isFinal()
设置为true时得到许多结果。我相信无论何时在音频中发现暂停,都会将
isFinal()
设置为true,但这并不一定意味着它已经到达了文件的结尾。对不起,我的错,我正在查看
转录本
对象的
isFinal()
而不是
SpeechResult