更新时间:2022-04-26 GMT+08:00
分享

流式一句话

功能介绍

流式一句话模式的语音长度限制为一分钟,适合于对话聊天等识别场景。

该接口支持用户将一整段语音分段,以流式输入,最后得到识别结果。实时语音识别引擎在获得分段的输入语音的同时,就可以同步地对这段数据进行特征提取和解码工作,而不用等到所有数据都获得后再开始工作。因此这样就可以在最后一段语音结束后,仅延迟很短的时间(也即等待处理最后一段语音数据以及获取最终结果的时间)即可返回最终识别结果。这种流式输入方式能缩短整体上获得最终结果的时间,极大地提升用户体验。

wss-URI

  • wss-URI格式

    wss /v1/{project_id}/rasr/short-stream

  • 参数说明
    表1 参数说明

    参数名

    是否必选

    说明

    project_id

    项目编号。获取方法,请参见获取项目ID

  • 请求示例(伪码)
    wss://{endpoint}/v1/{project_id}/rasr/short-stream
    
    Request Header:
    X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...

    “endpoint”即调用API的请求地址,不同服务不同区域的“endpoint”不同,具体请参见终端节点

  • Python3语言请求代码示例
    # -*- coding: utf-8 -*-
    # 此demo仅供测试使用,强烈建议使用sdk。需提前安装websocket-client, 执行pip install websocket-client
    import websocket
    import threading
    import time
    import json
    
    def rasr_demo():
        url = 'wss://{{endpoint}}/v1/{{project_id}}/rasr/short-stream'  # endpoint和project_id需替换
        audio_path = '音频路径'
        token = '用户对应region的token'
        header = {
            'X-Auth-Token': token
        }
        with open(audio_path, 'rb') as f:
            data = f.read()
        body = {
            'command': 'START',
            'config': {
                'audio_format': 'pcm8k16bit',
                'property': 'chinese_8k_common'
            }
        }
        def _on_message(ws, message):
            print(message)
        def _on_error(ws, error):
            print(error)
        ws = websocket.WebSocketApp(url, header, on_message=_on_message, on_error=_on_error)
        _thread = threading.Thread(target=ws.run_forever, args=(None, None, 30, 20))
        _thread.start()
        time.sleep(1)
        ws.send(json.dumps(body), opcode=websocket.ABNF.OPCODE_TEXT)
        now_index = 0
        byte_len = 4000
        while now_index < len(data):
            next_index = now_index + byte_len
            if next_index > len(data):
                next_index = len(data)
            send_array = data[now_index: next_index]
            ws.send(send_array, opcode=websocket.ABNF.OPCODE_BINARY)
            now_index += byte_len
            time.sleep(0.05)
        ws.send("{\"command\": \"END\", \"cancel\": \"false\"}", opcode=websocket.ABNF.OPCODE_TEXT)
        time.sleep(10)
        ws.close()
    if __name__ == '__main__':
        rasr_demo()
  • Java语言请求代码示例
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.Response;
    import okhttp3.WebSocket;
    import okhttp3.WebSocketListener;
    import okio.ByteString;
    import java.net.URL;
    /**
     * 此demo仅供测试使用,强烈建议使用SDK
     * 使用前需已配置okhttp、okio jar包。jar包可通过下载SDK获取。
     */
    public class RasrDemo {
      public void rasrDemo() {
        try {
          // endpoint和projectId需要替换成实际信息。
          String url = "wss://{{endpoint}}/v1/{{project_id}}/rasr/short-stream";
          String token = "对应region的token";
          byte[] data = null;  // 存放将要发送音频的byte数组
          OkHttpClient okHttpClient = new OkHttpClient();
          Request request = new Request.Builder().url(url).header("X-Auth-Token", token).build();
          WebSocket webSocket = okHttpClient.newWebSocket(request, new MyListener());
          webSocket.send("{\"command\": \"START\", \"config\": {\"audio_format\": \"pcm8k16bit\", \"property\": \"chinese_8k_common\"}}");
          webSocket.send(ByteString.of(data));
          webSocket.send("{  \"command\": \"END\",  \"cancel\": false}");
          Thread.sleep(10000);
          webSocket.close(1000, null);
    
        } catch (Exception e) {
          e.printStackTrace();
        }
    
    
      }
    
      class MyListener extends WebSocketListener {
        @Override
    
        public void onOpen(WebSocket webSocket, Response response) {
          System.out.println("conneected");
        }
        @Override
        public void onClosed(WebSocket webSocket, int code, String reason) {
          System.out.println("closed");
        }
        @Override
        public void onFailure(WebSocket webSocket, Throwable t, Response response) {
          t.printStackTrace();
        }
        @Override
        public void onMessage(WebSocket webSocket, String text) {
          System.out.println(text);
        }
      }
      public static void main(String[] args) {
        RasrDemo rasrDemo = new RasrDemo();
        rasrDemo.rasrDemo();
      }
    }
分享:

    相关文档

    相关产品

关闭导读