Parcourir la source

讯飞实时听写接口,debug

Davidliu il y a 1 mois
Parent
commit
6ddc5300ca
1 fichiers modifiés avec 20 ajouts et 55 suppressions
  1. 20 55
      src/core/voip/asr.py

+ 20 - 55
src/core/voip/asr.py

@@ -326,6 +326,7 @@ class XunfeiAsr:
 
         self.connected = False
         self.status = XunfeiAsr.STATUS_FIRST_FRAME  # 音频的状态信息,标识音频是第一帧,还是中间帧、最后一帧
+        self.pre_result = ""
 
         self.APPID = '1ec1097b'
         self.APIKey = '7237c29d862daa6fd805f788ed70c409'
@@ -380,10 +381,7 @@ class XunfeiAsr:
         import ssl
         import websocket
 
-        count = 0
-        # self.__event.clear()
         websocket.enableTrace(False)
-        # while not self.__event.is_set():
         try:
             # 测试时候在此处正确填写相关信息即可运行
             time1 = datetime.now()
@@ -394,13 +392,10 @@ class XunfeiAsr:
             self.connected = False
             self.ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
             time_cost = (datetime.now() - time1)
-            self.logger.info(f"xunfei.Asr.started. call_id:{self.__id}, timeCost:{time_cost}, count:{count}")
-            # self.__event.wait(timeout=1)
-            # self.logger.info(f"xunfei.Asr.started. call_id:{self.__id}, {count}")
+            self.logger.info(f"xunfei.Asr.started. call_id:{self.__id}, timeCost:{time_cost}")
         except Exception as e:
             traceback.print_exc()
             self.logger.info(f"[{self.__id}]ASR session start exception. {e}")
-            # count += 1
 
     def start(self):
         self.__th.start()
@@ -458,22 +453,10 @@ class XunfeiAsr:
     def on_message(self, ws, message):
         try:
             self.logger.info("xunfei.Asr.recv: call_id: %s, message :%s", self.__id, message)
-            code = json.loads(message)["code"]
-            sid = json.loads(message)["sid"]
-            if code != 0:
-                errMsg = json.loads(message)["message"]
-                self.logger.info("sid:%s call error:%s code is:%s" % (sid, errMsg, code))
-
-            # else:
-            #     data = json.loads(message)["data"]["result"]["ws"]
-            #     # print(json.loads(message))
-            #     result = ""
-            #     for i in data:
-            #         for w in i["cw"]:
-            #             result += w["w"]
-            #     self.logger.info("sid:%s call success!,data is:%s" % (sid, json.dumps(data, ensure_ascii=False)))
+            if self.message_receiver:
+                self.message_receiver(self.convert_message(message))
         except Exception as e:
-            self.logger.error("receive msg,but parse exception:%s", e)
+            self.logger.error("receive msg, but parse exception call_id:%s, message:%s, error:%s", self.__id, message, e)
 
     # 收到websocket错误的处理
     def on_error(self, ws, error):
@@ -494,8 +477,22 @@ class XunfeiAsr:
 
         if message["code"] == 0:
             data = message["data"]["result"]["ws"]
-            status = message["data"]["status"]
+            result = ""
+            for i in data:
+                for w in i["cw"]:
+                    result += w["w"]
 
+            status = message["data"]["status"]
+            if status == 0:
+                final_result['name'] = 'SentenceBegin'
+            elif status == 1:
+                final_result['name'] = 'TranscriptionResultChanged'
+                final_result['result'] = result
+                self.pre_result = result
+            elif status == 2:
+                final_result['status'] = 'SentenceEnd'
+                final_result['result'] = self.pre_result + result
+                self.pre_result = ""
         else:
             final_result['name'] = 'TranscriptionResultError'
             final_result['status'] = message['code']
@@ -503,37 +500,5 @@ class XunfeiAsr:
             self.logger.info("call_id:%s, sid:%s call error:%s code is:%s" % (self.__id, message["sid"], message["message"], message["code"]))
             registry.ASR_ERRORS.labels(message['code']).inc()
 
-        # code = json.loads(message)["code"]
-        # sid = json.loads(message)["sid"]
-        # if code != 0:
-        #     errMsg = json.loads(message)["message"]
-        #     self.logger.info("sid:%s call error:%s code is:%s" % (sid, errMsg, code))
-        #
-        # else:
-        #     data = json.loads(message)["data"]["result"]["ws"]
-        #     # print(json.loads(message))
-        #     result = ""
-        #     for i in data:
-        #         for w in i["cw"]:
-        #             result += w["w"]
-        #     self.logger.info("sid:%s call success!,data is:%s" % (sid, json.dumps(data, ensure_ascii=False)))
-        #
-        # if message["header"]["status"] == 20000000:
-        #     if message["header"]["name"] == "SentenceBegin":
-        #         final_result['name'] = 'SentenceBegin'
-        #     if message["header"]["name"] == "SentenceEnd":
-        #         result = message["payload"]["result"]
-        #         # self.logger.info("asr返回内容Result:%s", result)
-        #         final_result['name'] = 'SentenceEnd'
-        #         final_result['result'] = result
-        #     elif message["header"]["name"] == "TranscriptionResultChanged":
-        #         final_result['name'] = 'TranscriptionResultChanged'
-        # else:
-        #     final_result['name'] = 'TranscriptionResultError'
-        #     final_result['status'] = message['header']['status']
-        #     final_result['result'] = ''
-        #     self.logger.info(f"Status is not {message['header']['status']}")
-        #     registry.ASR_ERRORS.labels(message['header']['status']).inc()
-
         self.logger.error("xunfei.Asr.recv: call_id:%s, final_result: %s", self.__id, final_result)
         return final_result