Browse Source

讯飞asr测试,debug

Davidliu 1 tháng trước cách đây
mục cha
commit
5507cd455d
1 tập tin đã thay đổi với 12 bổ sung6 xóa
  1. 12 6
      src/core/voip/asr.py

+ 12 - 6
src/core/voip/asr.py

@@ -234,13 +234,16 @@ class XfAsr:
         signa = str(signa, 'utf-8')
 
         count = 10
+        _ws = None
         while count > 0:
             try:
-                return create_connection(base_url + "?appid=" + app_id + "&ts=" + ts + "&signa=" + quote(signa))
+                _ws = create_connection(base_url + "?appid=" + app_id + "&ts=" + ts + "&signa=" + quote(signa))
+                break
             except Exception as e:
                 count -= 1
                 self.logger.info("new_connection:exception, count=%s, message=%s", count, e)
                 time.sleep(.010)
+        return _ws
 
     def start(self):
         self.trecv.start()
@@ -264,11 +267,13 @@ class XfAsr:
     #     print("send end tag success")
 
     def send_audio(self, chunk):
-        self.ws.send(chunk)
+        if self.ws:
+            self.ws.send(chunk)
 
     def close(self):
-        self.ws.send(bytes(self.end_tag.encode('utf-8')))
-        self.ws.close()
+        if self.ws:
+            self.ws.send(bytes(self.end_tag.encode('utf-8')))
+            self.ws.close()
         print("connection closed")
 
     def recv(self):
@@ -277,7 +282,7 @@ class XfAsr:
             # {"seg_id": 1, "cn": {"st": {"rt": [{"ws": [{"cw": [{"sc": 0.00, "w": "停水", "wp": "n", "rl": "0", "wb": 26, "wc": 0.00, "we": 52}], "wb": 0,"we": 0}]}], "bg": "9770", "type": "1", "ed": "0"}}, "ls": false}
             # {"seg_id": 2, "cn": {"st": {"rt": [{"ws": [{"cw": [{"sc": 0.00, "w": "停水", "wp": "n", "rl": "0", "wb": 9, "wc": 0.00, "we": 64}], "wb": 9,"we": 64},{"cw": [{"sc": 0.00, "w": "咨询", "wp": "n", "rl": "0", "wb": 65, "wc": 0.00, "we": 132}], "wb": 65,"we": 132}]}], "bg": "9510", "type": "0", "ed": "10950"}}, "ls": false}
             self.logger.info(f"xunfei.Asr.recv: ws.connected:{self.ws.connected}")
-            while self.ws.connected:
+            while self.ws and self.ws.connected:
                 message = str(self.ws.recv())
                 if len(message) == 0:
                     self.logger.info("xunfei.Asr.recv: receive result end")
@@ -327,6 +332,7 @@ class XfAsr:
             final_result['name'] = 'TranscriptionResultError'
             final_result['status'] = ''
             final_result['result'] = message
-            self.ws.close()
+            if self.ws:
+                self.ws.close()
 
         return final_result