我一直在尝试使用 BlueMix SpeechToText Java 库,特别是 com.ibm.watson.developer_cloud.speech_to_text.v1 中的 SpeechToText 类。
我有很长的 wav 文件,我想将其转换为文本。这些文件约为 70MB。目标是使用 java API ( http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/speech-to-text/api/v1/?java#recognize ) 来识别文本。我意识到我需要每 30 秒检查一次通话状态,因为翻译结束后,我只有 30 秒的时间来检索最终结果。
为了在使用 RESTful API 时这样做,我需要创建一个会话,然后将我的搜索引擎与所述会话绑定,以便我可以查询在会话上运行的作业的状态。
我试图创建一个会话,但该会话永远不可用。我已经验证它似乎可以在提供的 web 应用程序(https://stream.watsonplatform.net/speech-to-text/api/v1/sessions?Method=GET)上工作。
此外,我尝试编写自己的客户端,尝试设置从会话创建中检索到的 cookie,但这也不起作用。
我也尝试通过安全的 websocket 进行连接,但无法成功连接。
下面是我一直在使用的一些示例代码。
有任何想法吗?
public class Speech2Text extends WatsonService {
private static final Logger logger = LoggerFactory .getLogger(Speech2Text.class);
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, InterruptedException {
Speech2Text s2t = new Speech2Text();
s2t.httpClient();
// try {
// s2t.webSocketClient();
// } catch (URISyntaxException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
// }
}
public void httpClient() throws FileNotFoundException,UnsupportedEncodingException {
logger.info("Running http client");
final Stopwatch stopwatch = Stopwatch.createStarted();
SpeechToText service = new SpeechToText();
service.setUsernameAndPassword("XXXXXX","XXXXX");
List<SpeechModel> models = service.getModels();
for (SpeechModel model : models) {
logger.info(model.getName());
}
SpeechSession session = service.createSession("en-US_NarrowbandModel");
System.out.println(session.toString());
SessionStatus status = service.getRecognizeStatus(session);
logger.info(status.getModel());
logger.info(service.getEndPoint());
File audio = new File("/home/baaron/watson-bluemix/answer_06.wav");
Map params = new HashMap();
params.put("audio", audio);
params.put("content_type", "audio/wav");
params.put("continuous", "true");
params.put("session_id", session.getSessionId());
logger.info(service.getEndPoint());
SpeechResults transcript = service.recognize(params);
PrintWriter writer = new PrintWriter("/home/baaron/watson-bluemix/PCCJPApart1test.transcript", "UTF-8");
writer.println(transcript.toString());
SessionStatus status1 = service.getRecognizeStatus(session.getSessionId());
System.out.println(status1);
service.deleteSession(session.getSessionId());
writer.close();
stopwatch.stop();
logger.info("Processing took: " + stopwatch + ".");
}
public void webSocketClient() throws URISyntaxException, IOException,
InterruptedException {
logger.info("Running web socket client");
String encoding = new String(Base64.encodeBase64String("XXXXXXXXXX".getBytes()));
HttpPost httppost = new HttpPost( "https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api");
httppost.setHeader("Authorization", "Basic " + encoding);
System.out.println("executing request " + httppost.getRequestLine());
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
logger.info(response.getStatusLine().getReasonPhrase());
WebSocketImpl.DEBUG = true;
BufferedReader reader = new BufferedReader(new InputStreamReader( entity.getContent()));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
}
String token = out.toString();
final WebSocketClient client = new WebSocketClient(
new URI("wss://stream.watsonplatform.net/speech-to-text-beta/api/v1/recognize?watson-token=" + token)) {
@Override
public void onMessage(String message) {
JSONObject obj = new JSONObject(message);
// String channel = obj.getString("channel");
}
@Override
public void onOpen(ServerHandshake handshake) {
System.out.println("opened connection");
}
@Override
public void onClose(int code, String reason, boolean remote) {
System.out.println("closed connection");
}
@Override
public void onError(Exception ex) {
ex.printStackTrace();
}
};
// open websocket
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, null);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
client.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(
sslContext));
logger.info("CONNECTED: " + client.connectBlocking());
JSONObject obj = new JSONObject();
obj.put("action", "start");
obj.put("content-type", "audio/wav");
client.send(obj.toString());
logger.info("Done");
}
}