我一直在尝试使用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时这样做,我需要创建一个会话,然后将我的搜索引擎绑定到所述会话,以便我可以查询在会话上运行的作业的状态。
我尝试创建会话但会话永远不可用。我已经验证它似乎适用于提供的webapp(https://stream.watsonplatform.net/speech-to-text/api/v1/sessions?Method=GET)。
此外,我尝试编写自己的客户端,我试图设置从会话创建中检索到的cookie,但也没有用。
我还尝试通过安全的websockets进行连接,但无法实现成功的连接。
以下是我一直使用的一些示例代码。
有什么想法吗?
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");
}
}
答案 0 :(得分:3)
在https://stream.watsonplatform.net/speech-to-text/api/v1/sessions上进行GET不会列出您的会话,即使这些会话已创建。
检查您是否有会话的方法是在https://stream.watsonplatform.net/speech-to-text/api/v1/sessions/yourSessionId
上进行GET如果会话在那里,您将获得200响应,否则为404.请记住为此启用cookie。
答案 1 :(得分:0)
如果你想要的是转录音频文件,你可以这样做:
<dependency>
<groupId>com.ibm.watson.developer_cloud</groupId>
<artifactId>java-sdk</artifactId>
<version>3.8.0</version>
</dependency>
确保使用最新版本的Java SDK。
的Maven
compile 'com.ibm.watson.developer_cloud:java-sdk:3.8.0'
摇篮
#ifndef TREE_H
#define TREE_H
#include <algorithm>
class TreeNode {
private:
int key;
TreeNode *left;
TreeNode *right;
public:
TreeNode(int key);
void insert(int key);
void display(TreeNode *p, int lvl);
void preOrder();
void inOrder();
void postOrder();
int size();
int height();
};
class Tree {
private:
TreeNode *root;
public:
Tree();
Tree(int *a, int n);
void insert(int key);
void preOrder();
void inOrder();
void postOrder();
void display(TreeNode *p);
int size();
int height();
};
void TreeNode::display(TreeNode *p, int lvl){
if (p!=NULL){
if (p->left) display(p->left, lvl + 4);
if (p->right) display(p->right, lvl + 4);
if (lvl) {
std::cout << std::setw(lvl) << ' ';
}
cout << p->key << "\n";
}
}
void Tree::display(TreeNode *p){
if (p!=NULL){
return display(p,0);
}