首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IBM :如何使用多块Websocket接口?

IBM :如何使用多块Websocket接口?
EN

Stack Overflow用户
提问于 2019-05-27 06:44:51
回答 2查看 270关注 0票数 0

我已经开发了一个在c++中使用另一个API和IBM语音到文本服务API的流式语音识别应用程序。

在这两个程序中,我使用的是包含此音频的同一个文件。

周日,当一列雷暴横扫科罗拉多州时,几个龙卷风落了下来。

该文件的大小为641,680字节,我一次向文本服务器发送100,000字节(最大)块到语音。

现在,有了另一个API,我就可以将所有东西作为一个整体来识别。对于IBM Watson API,我做不到。下面是我所做的:

  • 连接到IBM服务器(语音到文本API)
  • 发送启动帧{"action":"start","content-type":"audio/mulaw;rate=8000"}
  • 发送二进制100,000字节
  • 发送停止帧{"action":"stop"}
  • ...Repeat二进制文件并停止到最后一个字节。

IBM语音API只能单独识别这些块。

例如:

几次龙卷风登陆 一连串的雷声 横扫科罗拉多州 星期天

这似乎是单个块的输出,块分割之间的单词(例如在这里,“雷雨”部分出现在块的末尾,部分出现在下一个块的开头)因此被错误地识别或丢弃。

我做错了什么?

编辑(我使用c++和boost库进行websocket接口)

代码语言:javascript
复制
//Do the websocket handshake 
void IbmWebsocketSession::on_ssl_handshake(beast::error_code ec) {

    auto mToken = mSttServiceObject->GetToken(); // Get the authentication token

    //Complete the websocket handshake and call back the "send_start" function
    mWebSocket.async_handshake_ex(mHost, mUrlEndpoint, [mToken](request_type& reqHead) {reqHead.insert(http::field::authorization,mToken);},
            bind(&IbmWebsocketSession::send_start, shared_from_this(), placeholders::_1));
}

//Sent the start frame
void IbmWebsocketSession::send_start(beast::error_code ec) {

    //Send the START_FRAME and call back the "read_resp" function to receive the "state: listening" message
    mWebSocket.async_write(net::buffer(START_FRAME),
            bind(&IbmWebsocketSession::read_resp, shared_from_this(), placeholders::_1, placeholders::_2));
}

//Sent the binary data
void IbmWebsocketSession::send_binary(beast::error_code ec) {

    streamsize bytes_read = mFilestream.rdbuf()->sgetn(&chunk[0], chunk.size()); //gets the binary data chunks from a file (which is being written at run time

    // Send binary data
    if (bytes_read > mcMinsize) {  //Minimum size defined by IBM  is 100 bytes.
                                   // If chunk size is greater than 100 bytes, then send the data and then callback "send_stop" function
        mWebSocket.binary(true);

        /**********************************************************************
         *  Wait a second before writing the next chunk.
         **********************************************************************/
        this_thread::sleep_for(chrono::seconds(1));

        mWebSocket.async_write(net::buffer(&chunk[0], bytes_read),
                bind(&IbmWebsocketSession::send_stop, shared_from_this(), placeholders::_1));
    } else {                     //If chunk size is less than 100 bytes, then DO NOT send the data only call "send_stop" function
        shared_from_this()->send_stop(ec);
    }

}

void IbmWebsocketSession::send_stop(beast::error_code ec) {

    mWebSocket.binary(false);
    /*****************************************************************
     * Send the Stop message
     *****************************************************************/
    mWebSocket.async_write(net::buffer(mTextStop),
            bind(&IbmWebsocketSession::read_resp, shared_from_this(), placeholders::_1, placeholders::_2));
}

void IbmWebsocketSession::read_resp(beast::error_code ec, size_t bytes_transferred) {
    boost::ignore_unused(bytes_transferred);
        if(mWebSocket.is_open())
        {
            // Read the websocket response and call back the "display_buffer" function
            mWebSocket.async_read(mBuffer, bind(&IbmWebsocketSession::display_buffer, shared_from_this(),placeholders::_1));
        }
        else
            cerr << "Error: " << e->what() << endl;

}

void IbmWebsocketSession::display_buffer(beast::error_code ec) {

    /*****************************************************************
     * Get the buffer into stringstream
     *****************************************************************/
    msWebsocketResponse << beast::buffers(mBuffer.data());

    mResponseTranscriptIBM = ParseTranscript(); //Parse the response transcript

    mBuffer.consume(mBuffer.size()); //Clear the websocket buffer

    if ("Listening" == mResponseTranscriptIBM && true != mSttServiceObject->IsGstFileWriteDone()) { // IsGstFileWriteDone -> checks if the user has stopped speaking
        shared_from_this()->send_binary(ec);
    } else {
        shared_from_this()->close_websocket(ec, 0);
    }
}
EN

回答 2

Stack Overflow用户

发布于 2019-05-27 07:01:22

IBM的文字演讲有几个APIs传输音频和接收转录的文本。根据您的描述,您似乎使用了WebSocket接口

对于WebSocket接口,您将打开连接(启动),然后发送单个数据块,并且--一旦所有内容都已发送--停止识别请求。

您没有共享代码,但似乎正在启动和停止对每个块的请求。只在最后一大块之后停下来。

我建议看一看API文档,它包含不同语言的示例。Node.js示例演示如何注册事件。。在GitHub上也有类似于这个带有Python的WebSocket的例子。这是另一个显示块状的

票数 0
EN

Stack Overflow用户

发布于 2019-05-28 01:52:41

@data_henrik是正确的,流程是错误的,应该是:...START帧>>二进制数据>> . >>停止帧

只有当没有更多的音频块要发送时,才需要发送{"action":"stop"}消息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56320932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档