优化AI语言界面
authordxy15521469561 <1731678236@qq.com>
Fri, 14 Jun 2024 10:43:30 +0000 (18:43 +0800)
committerdxy15521469561 <1731678236@qq.com>
Fri, 14 Jun 2024 10:43:30 +0000 (18:43 +0800)
2.sdk/project/W8/sdk/services/microPython/ports/quectel/core/ai.py
2.sdk/project/W8/sdk/services/microPython/ports/quectel/core/ui_setting.py

index 5df06fd..9bca2a7 100644 (file)
@@ -580,6 +580,7 @@ class AIScreen(Screen):#AI_GUI
         self.timer_out_count = 0
         self.img_show_count = 0
         self.img_show_count_tmp = 0
+        self.img_show_count_flag = 0
         self.tts_data_tmp = bytes()
         self.press_state = 0
         self.AI_label_str = ""
@@ -660,6 +661,7 @@ class AIScreen(Screen):#AI_GUI
         self.timer_out_count = 0
         self.img_show_count = 0
         self.img_show_count_tmp = 0
+        self.img_show_count_flag = 0
         self.AI_listen_end = 0 #语音识别完成
         self.refresh_count = 0
         self.AI_listen_text = ""
@@ -742,6 +744,7 @@ class AIScreen(Screen):#AI_GUI
                 self.timer_out_count = 0
                 self.img_show_count = 0
                 self.img_show_count_tmp = 0
+                self.img_show_count_flag = 0
                 self.refresh_texttimer.start(500,1,self.text_show_think)
             else:
                 self.not_talk_cb()
@@ -787,13 +790,23 @@ class AIScreen(Screen):#AI_GUI
 
     def text_show_think(self,*argc):
         EventMesh.publish("publish_message_event_ui",{"func":self.text_show_think_cb,"data":0})
-
+    def think_end_result(self):
+        if self.timer_out_count > 4:
+            if self.img_show_count_flag == 1:
+                return
+            self.timer_out_count = 0
+            self.img_show_count_flag = 1
+            self.refresh_texttimer.stop()
+            self.ai_talk_end()
     def text_show_think_cb(self,data): #文本刷新
-        if len(self.AI_label_str) != 0:
+        if self.timer_out_count > 4:
+            if self.img_show_count_flag == 1:
+                return
+            self.timer_out_count = 0
+            self.img_show_count_flag = 1
             self.refresh_texttimer.stop()
             self.ai_talk_end()
             return
-        
         EventMesh.publish("system_sleep_rest")#防止黑屏
         self.timer_out_count += 1
         if self.timer_out_count > 2*30: #说话了服务器没有返回结果数据
@@ -922,29 +935,16 @@ class AIScreen(Screen):#AI_GUI
     def text_show_refresh(self,*argc):
         EventMesh.publish("publish_message_event_ui",{"func":self.text_show_refresh_cb,"data":0})
     def talk_ars_end(self):
-        self.AI_client_chat_send(self.AI_listen_text)
-        self.AI_listen_end = 0
-        self.refresh_count = 0
-        self.refresh_texttimer.stop()
         self.record.stop()#关闭录音
         self.record_timer.stop()#关闭定时器
         self.timer_out_count = 0
-        print("self.AI_listen_text:{}".format(self.AI_listen_text))
-        #正在思考
-        AI_talk_end_screen.add_flag(lv.obj.FLAG.HIDDEN) #先隐藏
-        AI_listen_screen.add_flag(lv.obj.FLAG.HIDDEN)
-        AI_talk_screen.add_flag(lv.obj.FLAG.HIDDEN) #先隐藏
-        AI_no_net_screen.add_flag(lv.obj.FLAG.HIDDEN) #
-        AI_connect_timerout_screen.add_flag(lv.obj.FLAG.HIDDEN) #先隐藏
-        AI_not_listen_text_screen.add_flag(lv.obj.FLAG.HIDDEN) #
-        AI_think_screen.clear_flag(lv.obj.FLAG.HIDDEN) #
-        al_think_screen_icon.set_src("E:/static/AI/AI_THINK001.png")
-        self.refresh_count = 0
-        self.timer_out_count = 0
-        self.img_show_count = 0
-        self.img_show_count_tmp = 0
-        self.refresh_texttimer.start(500,1,self.text_show_think)
-    
+        print("talk_ars_end!!!!!!!!!!!\r\n")
+        self.AI_client_chat_send(self.AI_listen_text) 
+        try:
+            self.websocket_deconnect_asr()
+        except Exception as e:
+            usys.print_exception(e)
+       
     
     def text_show_refresh_cb(self,data):
         if self.AI_listen_end == 1 or self.timer_out_count >= 120:#语音识别结束
@@ -971,6 +971,7 @@ class AIScreen(Screen):#AI_GUI
                     self.timer_out_count = 0
                     self.img_show_count = 0
                     self.img_show_count_tmp = 0
+                    self.img_show_count_flag = 0
                     self.refresh_texttimer.start(500,1,self.text_show_think)
             else:
 
@@ -1020,7 +1021,7 @@ class AIScreen(Screen):#AI_GUI
                 al_listen_screen_title1.set_text("识别中...")  
                 self.record.stream_start(audio.Record.AMRNB, 8000, 60)
                 self.record_timer.start(1*1000,1,self.record_timer_cb)
-                self.refresh_texttimer.start(400,1,self.text_show_refresh)
+                self.refresh_texttimer.start(500,1,self.text_show_refresh)
                 #self.AI_list.add_flag(lv.obj.FLAG.HIDDEN)#隐藏列表选项方框
 
                 # AI_label.clear_flag(lv.obj.FLAG.HIDDEN)
@@ -1125,6 +1126,7 @@ class AIScreen(Screen):#AI_GUI
                         print("AI_client_chat_recv chat_data = {}".format(chat_data))
                         self.AI_label_str+=str(chat_data)
                         self.AI_client_chat_play_str+=str(chat_data)
+                        
                         if self.TTS_PRO_TYPE == 1:
                             AI_label.set_text(self.AI_label_str)
                         # EventMesh.publish("system_sleep_rest")#防止黑屏
@@ -1179,7 +1181,7 @@ class AIScreen(Screen):#AI_GUI
         if self.TTS_PRO_TYPE == 1:
             #tts数据入队
             self.AI_client_tts_put_data()
-        print("AI_audio_queue.size:{},AI_send_data_queue.size:{} self.AI_client_chat_play_str:{}\r\n".format(self.AI_audio_queue.size(),self.AI_send_data_queue.size(),self.AI_client_chat_play_str) )
+        print("AI_audio_queue.size:{},AI_send_data_queue.size:{} self.AI_client_chat_play_str:{} self.AI_client_tts_tra_flag:{}\r\n".format(self.AI_audio_queue.size(),self.AI_send_data_queue.size(),self.AI_client_chat_play_str,self.AI_client_tts_tra_flag) )
         #字符串分段发送,等待播放缓存变小
         if len(self.AI_client_chat_play_str) >0  and self.AI_client_tts_data_len < 150*1024 :#and (self.AI_client_tts_str_total_len <100 or self.AI_client_tts_str_total_len - self.AI_client_tts_data_total_len//672 < 60)缓存200k以下,且tts发送文字字节数比返回音频数据少60字节
             # play_str = split_string(self.AI_client_chat_play_str,40)#
@@ -1190,12 +1192,13 @@ class AIScreen(Screen):#AI_GUI
             elif self.TTS_PRO_TYPE == 0:
                 if self.AI_audio_queue.size() >= 5 or self.AI_client_tts_tra_flag == 1:#存了超过4句,就等小于3次在转化
                     return
-                play_str = split_string(self.AI_client_chat_play_str,60,130)#按逗号,句号切割
-                if play_str =="" or len(play_str)>130:#没找到句号或者逗号,切割字符串
-                    play_str = sub_utf8_String(self.AI_client_chat_play_str,80)#按中文切割字符串
+                play_str = split_string(self.AI_client_chat_play_str,50,100)#按逗号,句号切割
+                if play_str =="" or len(play_str)>100:#没找到句号或者逗号,切割字符串
+                    play_str = sub_utf8_String(self.AI_client_chat_play_str,100)#按中文切割字符串
                 self.AI_client_tts_tra_flag = 1
                 self.AI_client_tts_str_sen = play_str
-
+            if len(play_str) == 0:
+                return
             self.AI_client_tts_send(play_str)#TTS语音播放AI结果
             self.AI_client_chat_play_str = self.AI_client_chat_play_str[len(play_str):]
             print("TTS play:len{},data{}".format(len(play_str),play_str))
@@ -1296,6 +1299,8 @@ class AIScreen(Screen):#AI_GUI
                             
                             self.AI_client_tts_full_sentence_flag = 1
                             break
+                        if recv_data.find('\"msg\":\"success\"') == -1:
+                            break
                     else:
                         #判断是否是一个完整的句子
                         recv_data_len_tmp = len(recv_data)
@@ -1380,7 +1385,7 @@ class AIScreen(Screen):#AI_GUI
                             if jsload_data["result"]["is_end"] == True:  #{"type":"chat","content":"你好"}
                                 self.AI_listen_end = 1
                                 self.audio_play_state = 0
-                                self.AI_client_chat_send(str(jsload_data["result"]["result"]))
+                                self.talk_ars_end()
                                 break
                                 #chat_data = bytearray(chat_data_str)
                                 # AI_label.set_text("思考中...")
index 7c50849..86e9cb7 100644 (file)
@@ -969,7 +969,7 @@ class RestoreScreen(Screen):
                 pass
     def done_return(self): 
         EventMesh.publish("load_screen", {"screen": "setting"})
-        
+
     def btn8_press(self):
         EventMesh.publish("watch_top_show")
         EventMesh.publish("load_screen", {"screen": "main_menu"})
@@ -1150,6 +1150,7 @@ class PowerOnScreen(Screen):
         self.meta_type = "popup"
         self.timer = osTimer()
         self.timer1 = osTimer()
+        self.timer2 = osTimer()
         self.aud = audio.Audio(0)
         self.aud.set_pa(Pin.GPIO29)
         self.volum_size = 8
@@ -1168,6 +1169,7 @@ class PowerOnScreen(Screen):
         # EventMesh.publish("set_motor_time",[1,2,8,8,4])#设置马达参数
         self.timer.start(1000,1,self.fun)
         self.timer1.start(3000,0,self.fun_motor)
+        self.timer2.start(13000,0,self.to_main_page_fun)
         pass
 
     def fun(self,*argc):
@@ -1182,10 +1184,13 @@ class PowerOnScreen(Screen):
                 self.count = 0
                 self.timer.stop()
                 self.aud.stopAll()
-                EventMesh.publish("set_UI_high_priority",0)
-                EventMesh.publish("load_screen", {"screen": "main_menu"})
+                
                 return
         self.count+=1
+    def to_main_page_fun(self,*argc):
+        EventMesh.publish("set_UI_high_priority",0)
+        EventMesh.publish("load_screen", {"screen": "main_menu"})
+        
     def fun_motor(self,*argc):
         EventMesh.publish("set_motor_time",[1,2,8*1,8,4])#设置马达参数