-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathapp_main.py
More file actions
1319 lines (1135 loc) · 53.1 KB
/
app_main.py
File metadata and controls
1319 lines (1135 loc) · 53.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# app_main.py
# -*- coding: utf-8 -*-
import os, sys, time, json, asyncio, base64, audioop
from typing import Any, Dict, Optional, Tuple, List, Callable, Set, Deque
from collections import deque
from dataclasses import dataclass
import re
# 在其它 import 之后加:
from qwen_extractor import extract_english_label
from navigation_master import NavigationMaster, OrchestratorResult
# 新增:导入盲道导航器
from workflow_blindpath import BlindPathNavigator
# 新增:导入过马路导航器
from workflow_crossstreet import CrossStreetNavigator
import torch
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Request
from fastapi.responses import HTMLResponse, PlainTextResponse
from fastapi.staticfiles import StaticFiles
from starlette.websockets import WebSocketState
import uvicorn
import cv2
import numpy as np
from ultralytics import YOLO
from obstacle_detector_client import ObstacleDetectorClient
import torch # 添加这行
import mediapipe as mp
import bridge_io
import threading
import yolomedia # 确保和 app_main.py 同目录,文件名就是 yolomedia.py
# ---- Windows 事件循环策略 ----
if sys.platform.startswith("win"):
try:
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
except Exception:
pass
# ---- .env ----
try:
from dotenv import load_dotenv
load_dotenv()
except Exception:
pass
# ---- DashScope ASR 基础 ----
from dashscope import audio as dash_audio # 若未安装,会在原项目里抛错提示
API_KEY = os.getenv("DASHSCOPE_API_KEY", "sk-a9440db694924559ae4ebdc2023d2b9a")
if not API_KEY:
raise RuntimeError("未设置 DASHSCOPE_API_KEY")
MODEL = "paraformer-realtime-v2"
SAMPLE_RATE = 16000
AUDIO_FMT = "pcm"
CHUNK_MS = 20
BYTES_CHUNK = SAMPLE_RATE * CHUNK_MS // 1000 * 2
SILENCE_20MS = bytes(BYTES_CHUNK)
# ---- 引入我们的模块 ----
from audio_stream import (
register_stream_route, # 挂 /stream.wav
broadcast_pcm16_realtime, # 实时向连接分发 16k PCM
hard_reset_audio, # 音频+AI 播放总闸
BYTES_PER_20MS_16K,
is_playing_now,
current_ai_task,
)
from omni_client import stream_chat, OmniStreamPiece
from asr_core import (
ASRCallback,
set_current_recognition,
stop_current_recognition,
)
from audio_player import initialize_audio_system, play_voice_text
# ---- 同步录制器 ----
import sync_recorder
import signal
import atexit
# ---- IMU UDP ----
UDP_IP = "0.0.0.0"
UDP_PORT = 12345
app = FastAPI()
# ====== 状态与容器 ======
app.mount("/static", StaticFiles(directory="static"), name="static")
ui_clients: Dict[int, WebSocket] = {}
current_partial: str = ""
recent_finals: List[str] = []
RECENT_MAX = 50
last_frames: Deque[Tuple[float, bytes]] = deque(maxlen=10)
camera_viewers: Set[WebSocket] = set()
esp32_camera_ws: Optional[WebSocket] = None
imu_ws_clients: Set[WebSocket] = set()
esp32_audio_ws: Optional[WebSocket] = None
# 【新增】盲道导航相关全局变量
blind_path_navigator = None
navigation_active = False
yolo_seg_model = None
obstacle_detector = None
# 【新增】过马路导航相关全局变量
cross_street_navigator = None
cross_street_active = False
orchestrator = None # 新增
# 【新增】omni对话状态标志
omni_conversation_active = False # 标记omni对话是否正在进行
omni_previous_nav_state = None # 保存omni激活前的导航状态,用于恢复
# 【新增】模型加载函数
def load_navigation_models():
"""加载盲道导航所需的模型"""
global yolo_seg_model, obstacle_detector
try:
seg_model_path = os.getenv("BLIND_PATH_MODEL", r"C:\Users\Administrator\Desktop\rebuild1002\model\yolo-seg.pt")
#print(f"[NAVIGATION] 尝试加载模型: {seg_model_path}")
if os.path.exists(seg_model_path):
print(f"[NAVIGATION] 模型文件存在,开始加载...")
yolo_seg_model = YOLO(seg_model_path)
# 强制放到 GPU
if torch.cuda.is_available():
yolo_seg_model.to("cuda")
print(f"[NAVIGATION] 盲道分割模型加载成功并放到GPU: {yolo_seg_model.device}")
else:
print("[NAVIGATION] CUDA不可用,模型仍在CPU")
# 测试模型是否能正常运行
try:
test_img = np.zeros((640, 640, 3), dtype=np.uint8)
results = yolo_seg_model.predict(
test_img,
device="cuda" if torch.cuda.is_available() else "cpu",
verbose=False
)
print(f"[NAVIGATION] 模型测试成功,支持的类别数: {len(yolo_seg_model.names) if hasattr(yolo_seg_model, 'names') else '未知'}")
if hasattr(yolo_seg_model, 'names'):
print(f"[NAVIGATION] 模型类别: {yolo_seg_model.names}")
except Exception as e:
print(f"[NAVIGATION] 模型测试失败: {e}")
else:
print(f"[NAVIGATION] 错误:找不到模型文件: {seg_model_path}")
print(f"[NAVIGATION] 当前工作目录: {os.getcwd()}")
print(f"[NAVIGATION] 请检查文件路径是否正确")
# 【修改开始】使用 ObstacleDetectorClient 替代直接的 YOLO
obstacle_model_path = os.getenv("OBSTACLE_MODEL", r"C:\Users\Administrator\Desktop\rebuild1002\model\yoloe-11l-seg.pt")
print(f"[NAVIGATION] 尝试加载障碍物检测模型: {obstacle_model_path}")
if os.path.exists(obstacle_model_path):
print(f"[NAVIGATION] 障碍物检测模型文件存在,开始加载...")
try:
# 使用 ObstacleDetectorClient 封装的 YOLO-E
obstacle_detector = ObstacleDetectorClient(model_path=obstacle_model_path)
print(f"[NAVIGATION] ========== YOLO-E 障碍物检测器加载成功 ==========")
# 检查模型是否成功加载
if hasattr(obstacle_detector, 'model') and obstacle_detector.model is not None:
print(f"[NAVIGATION] YOLO-E 模型已初始化")
print(f"[NAVIGATION] 模型设备: {next(obstacle_detector.model.parameters()).device}")
else:
print(f"[NAVIGATION] 警告:YOLO-E 模型初始化异常")
# 检查白名单是否成功加载
if hasattr(obstacle_detector, 'WHITELIST_CLASSES'):
print(f"[NAVIGATION] 白名单类别数: {len(obstacle_detector.WHITELIST_CLASSES)}")
print(f"[NAVIGATION] 白名单前10个类别: {', '.join(obstacle_detector.WHITELIST_CLASSES[:10])}")
else:
print(f"[NAVIGATION] 警告:白名单类别未定义")
# 检查文本特征是否成功预计算
if hasattr(obstacle_detector, 'whitelist_embeddings') and obstacle_detector.whitelist_embeddings is not None:
print(f"[NAVIGATION] YOLO-E 文本特征已预计算")
print(f"[NAVIGATION] 文本特征张量形状: {obstacle_detector.whitelist_embeddings.shape if hasattr(obstacle_detector.whitelist_embeddings, 'shape') else '未知'}")
else:
print(f"[NAVIGATION] 警告:YOLO-E 文本特征未预计算")
# 测试障碍物检测功能
print(f"[NAVIGATION] 开始测试 YOLO-E 检测功能...")
try:
test_img = np.zeros((640, 640, 3), dtype=np.uint8)
# 在测试图像中画一个白色矩形,模拟一个物体
cv2.rectangle(test_img, (200, 200), (400, 400), (255, 255, 255), -1)
# 测试检测(不提供 path_mask)
test_results = obstacle_detector.detect(test_img)
print(f"[NAVIGATION] YOLO-E 检测测试成功!")
print(f"[NAVIGATION] 测试检测结果数: {len(test_results)}")
if len(test_results) > 0:
print(f"[NAVIGATION] 测试检测到的物体:")
for i, obj in enumerate(test_results):
print(f" - 物体 {i+1}: {obj.get('name', 'unknown')}, "
f"面积比例: {obj.get('area_ratio', 0):.3f}, "
f"位置: ({obj.get('center_x', 0):.0f}, {obj.get('center_y', 0):.0f})")
except Exception as e:
print(f"[NAVIGATION] YOLO-E 检测测试失败: {e}")
import traceback
traceback.print_exc()
print(f"[NAVIGATION] ========== YOLO-E 障碍物检测器加载完成 ==========")
except Exception as e:
print(f"[NAVIGATION] 障碍物检测器加载失败: {e}")
import traceback
traceback.print_exc()
obstacle_detector = None
else:
print(f"[NAVIGATION] 警告:找不到障碍物检测模型文件: {obstacle_model_path}")
except Exception as e:
print(f"[NAVIGATION] 模型加载失败: {e}")
import traceback
traceback.print_exc()
# 在程序启动时加载模型
print("[NAVIGATION] 开始加载导航模型...")
load_navigation_models()
print(f"[NAVIGATION] 模型加载完成 - yolo_seg_model: {yolo_seg_model is not None}")
# 【新增】启动同步录制
print("[RECORDER] 启动同步录制系统...")
sync_recorder.start_recording()
print("[RECORDER] 录制系统已启动,将自动保存视频和音频")
# 【新增】注册退出处理器,确保Ctrl+C时保存录制文件
def cleanup_on_exit():
"""程序退出时的清理工作"""
print("\n[SYSTEM] 正在关闭录制器...")
try:
sync_recorder.stop_recording()
print("[SYSTEM] 录制文件已保存")
except Exception as e:
print(f"[SYSTEM] 关闭录制器时出错: {e}")
def signal_handler(sig, frame):
"""处理Ctrl+C信号"""
print("\n[SYSTEM] 收到中断信号,正在安全退出...")
cleanup_on_exit()
import sys
sys.exit(0)
# 注册信号处理器
signal.signal(signal.SIGINT, signal_handler) # Ctrl+C
signal.signal(signal.SIGTERM, signal_handler) # 终止信号
atexit.register(cleanup_on_exit) # 正常退出时也调用
print("[RECORDER] 已注册退出处理器 - Ctrl+C时会自动保存录制文件")
# 【新增】预加载红绿灯检测模型(避免进入WAIT_TRAFFIC_LIGHT状态时卡顿)
try:
import trafficlight_detection
print("[TRAFFIC_LIGHT] 开始预加载红绿灯检测模型...")
if trafficlight_detection.init_model():
print("[TRAFFIC_LIGHT] 红绿灯检测模型预加载成功")
# 执行一次测试推理,完全预热模型
try:
test_img = np.zeros((640, 640, 3), dtype=np.uint8)
_ = trafficlight_detection.process_single_frame(test_img)
print("[TRAFFIC_LIGHT] 模型预热完成")
except Exception as e:
print(f"[TRAFFIC_LIGHT] 模型预热失败: {e}")
else:
print("[TRAFFIC_LIGHT] 红绿灯检测模型预加载失败")
except Exception as e:
print(f"[TRAFFIC_LIGHT] 红绿灯模型预加载出错: {e}")
# ============== 关键:系统级"硬重置"总闸 =================
interrupt_lock = asyncio.Lock()
# ============== YOLO媒体线程管理 =================
yolomedia_thread: Optional[threading.Thread] = None
yolomedia_stop_event = threading.Event()
yolomedia_running = False
yolomedia_sending_frames = False # 新增:标记YOLO是否已经开始发送处理后的帧
# 物品名称到YOLO类别的映射
ITEM_TO_CLASS_MAP = {
"红牛": "Red_Bull",
"AD钙奶": "AD_milk",
"ad钙奶": "AD_milk",
"钙奶": "AD_milk",
}
async def ui_broadcast_raw(msg: str):
dead = []
for k, ws in list(ui_clients.items()):
try:
await ws.send_text(msg)
except Exception:
dead.append(k)
for k in dead:
ui_clients.pop(k, None)
async def ui_broadcast_partial(text: str):
global current_partial
current_partial = text
await ui_broadcast_raw("PARTIAL:" + text)
async def ui_broadcast_final(text: str):
global current_partial, recent_finals
current_partial = ""
recent_finals.append(text)
if len(recent_finals) > RECENT_MAX:
recent_finals = recent_finals[-RECENT_MAX:]
await ui_broadcast_raw("FINAL:" + text)
print(f"[ASR/AI FINAL] {text}", flush=True)
async def full_system_reset(reason: str = ""):
"""
回到刚启动后的状态:
1) 停播 + 取消AI任务 + 切断所有/stream.wav(hard_reset_audio)
2) 停止 ASR 实时识别流(关键)
3) 清 UI 状态
4) 清最近相机帧(避免把旧帧又拼进下一轮)
5) 告知 ESP32:RESET(可选)
"""
# 1) 音频&AI
await hard_reset_audio(reason or "full_system_reset")
# 2) ASR
await stop_current_recognition()
# 3) UI
global current_partial, recent_finals
current_partial = ""
recent_finals = []
# 4) 相机帧
try:
last_frames.clear()
except Exception:
pass
# 5) 通知 ESP32
try:
if esp32_audio_ws and (esp32_audio_ws.client_state == WebSocketState.CONNECTED):
await esp32_audio_ws.send_text("RESET")
except Exception:
pass
print("[SYSTEM] full reset done.", flush=True)
# ========= 启动/停止 YOLO 媒体处理 =========
def start_yolomedia_with_target(target_name: str):
"""启动yolomedia线程,搜索指定物品"""
global yolomedia_thread, yolomedia_stop_event, yolomedia_running, yolomedia_sending_frames
# 如果已经在运行,先停止
if yolomedia_running:
stop_yolomedia()
# 查找对应的YOLO类别
yolo_class = ITEM_TO_CLASS_MAP.get(target_name, target_name)
print(f"[YOLOMEDIA] Starting with target: {target_name} -> YOLO class: {yolo_class}", flush=True)
print(f"[YOLOMEDIA] Available mappings: {ITEM_TO_CLASS_MAP}", flush=True) # 添加这行调试
yolomedia_stop_event.clear()
yolomedia_running = True
yolomedia_sending_frames = False # 重置发送帧状态
def _run():
try:
# 传递目标类别名和停止事件
yolomedia.main(headless=True, prompt_name=yolo_class, stop_event=yolomedia_stop_event)
except Exception as e:
print(f"[YOLOMEDIA] worker stopped: {e}", flush=True)
finally:
global yolomedia_running, yolomedia_sending_frames
yolomedia_running = False
yolomedia_sending_frames = False
yolomedia_thread = threading.Thread(target=_run, daemon=True)
yolomedia_thread.start()
print(f"[YOLOMEDIA] background worker started for: {yolo_class}(正在初始化,暂时显示原始画面)", flush=True)
def stop_yolomedia():
"""停止yolomedia线程"""
global yolomedia_thread, yolomedia_stop_event, yolomedia_running, yolomedia_sending_frames
if yolomedia_running:
print("[YOLOMEDIA] Stopping worker...", flush=True)
yolomedia_stop_event.set()
# 等待线程结束(最多等5秒)
if yolomedia_thread and yolomedia_thread.is_alive():
yolomedia_thread.join(timeout=5.0)
yolomedia_running = False
yolomedia_sending_frames = False
# 【新增】如果orchestrator在找物品模式,结束时不自动恢复(由命令控制)
# 只清理标志位即可
print("[YOLOMEDIA] Worker stopped, 等待状态切换.", flush=True)
# ========= 自定义的 start_ai_with_text,支持识别特殊命令 =========
async def start_ai_with_text_custom(user_text: str):
"""扩展版的AI启动函数,支持识别特殊命令"""
global navigation_active, blind_path_navigator, cross_street_active, cross_street_navigator, orchestrator
# 【修改】在导航模式和红绿灯检测模式下,只有特定词才进入omni对话
if orchestrator:
current_state = orchestrator.get_state()
# 如果在导航模式或红绿灯检测模式(非CHAT模式)
if current_state not in ["CHAT", "IDLE"]:
# 检查是否是允许的对话触发词
allowed_keywords = ["帮我看", "帮我看下", "帮我找", "找一下", "看看", "识别一下"]
is_allowed_query = any(keyword in user_text for keyword in allowed_keywords)
# 检查是否是导航控制命令
nav_control_keywords = ["开始过马路", "过马路结束", "开始导航", "盲道导航", "停止导航", "结束导航",
"检测红绿灯", "看红绿灯", "停止检测", "停止红绿灯"]
is_nav_control = any(keyword in user_text for keyword in nav_control_keywords)
# 如果既不是允许的查询,也不是导航控制命令,则丢弃
if not is_allowed_query and not is_nav_control:
mode_name = "红绿灯检测" if current_state == "TRAFFIC_LIGHT_DETECTION" else "导航"
print(f"[{mode_name}模式] 丢弃非对话语音: {user_text}")
return # 直接丢弃,不进入omni
# 【修改】检查是否是过马路相关命令 - 使用orchestrator控制
if "开始过马路" in user_text or "帮我过马路" in user_text:
# 【新增】如果正在找物品,先停止
if yolomedia_running:
stop_yolomedia()
print("[ITEM_SEARCH] 从找物品模式切换到过马路")
if orchestrator:
orchestrator.start_crossing()
print(f"[CROSS_STREET] 过马路模式已启动,状态: {orchestrator.get_state()}")
# 播放启动语音并广播到UI
play_voice_text("过马路模式已启动。")
await ui_broadcast_final("[系统] 过马路模式已启动")
else:
print("[CROSS_STREET] 警告:导航统领器未初始化!")
play_voice_text("启动过马路模式失败,请稍后重试。")
await ui_broadcast_final("[系统] 导航系统未就绪")
return
if "过马路结束" in user_text or "结束过马路" in user_text:
if orchestrator:
orchestrator.stop_navigation()
print(f"[CROSS_STREET] 导航已停止,状态: {orchestrator.get_state()}")
# 播放停止语音并广播到UI
play_voice_text("已停止导航。")
await ui_broadcast_final("[系统] 过马路模式已停止")
else:
await ui_broadcast_final("[系统] 导航系统未运行")
return
# 【修改】检查是否是红绿灯检测命令 - 实现与盲道导航互斥
if "检测红绿灯" in user_text or "看红绿灯" in user_text:
try:
import trafficlight_detection
# 切换orchestrator到红绿灯检测模式(暂停盲道导航)
if orchestrator:
orchestrator.start_traffic_light_detection()
print(f"[TRAFFIC] 切换到红绿灯检测模式,状态: {orchestrator.get_state()}")
# 【改进】使用主线程模式而不是独立线程,避免掉帧
success = trafficlight_detection.init_model() # 只初始化模型,不启动线程
trafficlight_detection.reset_detection_state() # 重置状态
if success:
await ui_broadcast_final("[系统] 红绿灯检测已启动")
else:
await ui_broadcast_final("[系统] 红绿灯模型加载失败")
except Exception as e:
print(f"[TRAFFIC] 启动红绿灯检测失败: {e}")
await ui_broadcast_final(f"[系统] 启动失败: {e}")
return
if "停止检测" in user_text or "停止红绿灯" in user_text:
try:
# 恢复到对话模式
if orchestrator:
orchestrator.stop_navigation() # 回到CHAT模式
print(f"[TRAFFIC] 红绿灯检测停止,恢复到{orchestrator.get_state()}模式")
await ui_broadcast_final("[系统] 红绿灯检测已停止")
except Exception as e:
print(f"[TRAFFIC] 停止红绿灯检测失败: {e}")
await ui_broadcast_final(f"[系统] 停止失败: {e}")
return
# 【修改】检查是否是导航相关命令 - 使用orchestrator控制
if "开始导航" in user_text or "盲道导航" in user_text or "帮我导航" in user_text:
# 【新增】如果正在找物品,先停止
if yolomedia_running:
stop_yolomedia()
print("[ITEM_SEARCH] 从找物品模式切换到盲道导航")
if orchestrator:
orchestrator.start_blind_path_navigation()
print(f"[NAVIGATION] 盲道导航已启动,状态: {orchestrator.get_state()}")
await ui_broadcast_final("[系统] 盲道导航已启动")
else:
print("[NAVIGATION] 警告:导航统领器未初始化!")
await ui_broadcast_final("[系统] 导航系统未就绪")
return
if "停止导航" in user_text or "结束导航" in user_text:
if orchestrator:
orchestrator.stop_navigation()
print(f"[NAVIGATION] 导航已停止,状态: {orchestrator.get_state()}")
await ui_broadcast_final("[系统] 盲道导航已停止")
else:
await ui_broadcast_final("[系统] 导航系统未运行")
return
nav_cmd_keywords = ["开始过马路", "过马路结束", "开始导航", "盲道导航", "停止导航", "结束导航", "立即通过", "现在通过", "继续"]
if any(k in user_text for k in nav_cmd_keywords):
if orchestrator:
orchestrator.on_voice_command(user_text)
await ui_broadcast_final("[系统] 导航模式已更新")
else:
await ui_broadcast_final("[系统] 导航统领器未初始化")
return
# 检查是否是"帮我找/识别一下xxx"的命令
# 扩展正则表达式,支持更多关键词
find_pattern = r"(?:^\s*帮我)?\s*找一下\s*(.+?)(?:。|!|?|$)"
match = re.search(find_pattern, user_text)
if match:
# 提取中文物品名称
item_cn = match.group(1).strip()
if item_cn:
# 【新增】用本地映射 + Qwen 提取英文类名
label_en, src = extract_english_label(item_cn)
print(f"[COMMAND] Finder request: '{item_cn}' -> '{label_en}' (src={src})", flush=True)
# 【新增】切换到找物品模式(暂停导航)
if orchestrator:
orchestrator.start_item_search()
print(f"[ITEM_SEARCH] 已切换到找物品模式,状态: {orchestrator.get_state()}")
# 【关键】把英文类名传给 yolomedia(它会在找不到类时自动切 YOLOE)
start_yolomedia_with_target(label_en)
# 给前端/语音来个确认反馈
try:
await ui_broadcast_final(f"[找物品] 正在寻找 {item_cn}...")
except Exception:
pass
return
# 检查是否是"找到了"的命令
if "找到了" in user_text or "拿到了" in user_text:
print("[COMMAND] Found command detected", flush=True)
# 停止yolomedia
stop_yolomedia()
# 【新增】停止找物品模式,恢复之前的导航状态
if orchestrator:
orchestrator.stop_item_search(restore_nav=True)
current_state = orchestrator.get_state()
print(f"[ITEM_SEARCH] 找物品结束,当前状态: {current_state}")
# 根据恢复的状态给出反馈
if current_state in ["BLINDPATH_NAV", "SEEKING_CROSSWALK", "WAIT_TRAFFIC_LIGHT", "CROSSING", "SEEKING_NEXT_BLINDPATH"]:
await ui_broadcast_final("[找物品] 已找到物品,继续导航。")
else:
await ui_broadcast_final("[找物品] 已找到物品。")
else:
await ui_broadcast_final("[找物品] 已找到物品。")
return
# 【修改】omni对话开始时,切换到CHAT模式
global omni_conversation_active, omni_previous_nav_state
omni_conversation_active = True
# 保存当前导航状态并切换到CHAT模式
if orchestrator:
current_state = orchestrator.get_state()
# 只有在导航模式下才需要保存和切换
if current_state not in ["CHAT", "IDLE"]:
omni_previous_nav_state = current_state
orchestrator.force_state("CHAT")
print(f"[OMNI] 对话开始,从{current_state}切换到CHAT模式")
else:
omni_previous_nav_state = None
print(f"[OMNI] 对话开始(当前已在{current_state}模式)")
# 如果不是特殊命令,执行原有的AI对话逻辑
# 但如果yolomedia正在运行,暂时不处理普通对话
if yolomedia_running:
print("[AI] YOLO media is running, skipping normal AI response", flush=True)
return
# 原有的AI对话逻辑
await start_ai_with_text(user_text)
# ========= Omni 播放启动 =========
async def start_ai_with_text(user_text: str):
"""硬重置后,开启新的 AI 语音输出。"""
async def _runner():
txt_buf: List[str] = []
rate_state = None
# 组装(图像+文本)
content_list = []
if last_frames:
try:
_, jpeg_bytes = last_frames[-1]
img_b64 = base64.b64encode(jpeg_bytes).decode("ascii")
content_list.append({
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{img_b64}"}
})
except Exception:
pass
content_list.append({"type": "text", "text": user_text})
try:
async for piece in stream_chat(content_list, voice="Cherry", audio_format="wav"):
# 文本增量(仅 UI)
if piece.text_delta:
txt_buf.append(piece.text_delta)
try:
await ui_broadcast_partial("[AI] " + "".join(txt_buf))
except Exception:
pass
# 音频分片:Omni 返回 24k (PCM16) 的 wav audio.data(Base64);下行需要 8k PCM16
if piece.audio_b64:
try:
pcm24 = base64.b64decode(piece.audio_b64)
except Exception:
pcm24 = b""
if pcm24:
# 24k → 8k (使用ratecv保证音调和速度不变)
pcm8k, rate_state = audioop.ratecv(pcm24, 2, 1, 24000, 8000, rate_state)
pcm8k = audioop.mul(pcm8k, 2, 0.60)
if pcm8k:
await broadcast_pcm16_realtime(pcm8k)
except asyncio.CancelledError:
# 被新一轮打断
raise
except Exception as e:
try:
await ui_broadcast_final(f"[AI] 发生错误:{e}")
except Exception:
pass
finally:
# 【修改】标记omni对话结束,恢复之前的导航模式
global omni_conversation_active, omni_previous_nav_state
omni_conversation_active = False
# 恢复之前的导航状态
if orchestrator and omni_previous_nav_state:
orchestrator.force_state(omni_previous_nav_state)
print(f"[OMNI] 对话结束,恢复到{omni_previous_nav_state}模式")
omni_previous_nav_state = None
else:
print(f"[OMNI] 对话结束(无需恢复导航状态)")
# 自然结束时,给当前连接一个 "完结" 信号
from audio_stream import stream_clients # 局部导入,避免环依赖
for sc in list(stream_clients):
if not sc.abort_event.is_set():
try: sc.q.put_nowait(b"\x00"*BYTES_PER_20MS_16K) # 一帧静音
except Exception: pass
try: sc.q.put_nowait(None)
except Exception: pass
final_text = ("".join(txt_buf)).strip() or "(空响应)"
try:
await ui_broadcast_final("[AI] " + final_text)
except Exception:
pass
# 真正启动前先硬重置,保证**绝无**旧音频残留
await hard_reset_audio("start_ai_with_text")
loop = asyncio.get_running_loop()
from audio_stream import current_ai_task as _task_holder # 读写模块内全局
from audio_stream import __dict__ as _as_dict
# 设置模块内的 current_ai_task
task = loop.create_task(_runner())
_as_dict["current_ai_task"] = task
# ---------- 页面 / 健康 ----------
@app.get("/", response_class=HTMLResponse)
def root():
with open(os.path.join("templates", "index.html"), "r", encoding="utf-8") as f:
return HTMLResponse(f.read())
@app.get("/api/health", response_class=PlainTextResponse)
def health():
return "OK"
# 注册 /stream.wav
register_stream_route(app)
# ---------- WebSocket:WebUI 文本(ASR/AI 状态推送) ----------
@app.websocket("/ws_ui")
async def ws_ui(ws: WebSocket):
await ws.accept()
ui_clients[id(ws)] = ws
try:
init = {"partial": current_partial, "finals": recent_finals[-10:]}
await ws.send_text("INIT:" + json.dumps(init, ensure_ascii=False))
while True:
await asyncio.sleep(60)
except WebSocketDisconnect:
pass
finally:
ui_clients.pop(id(ws), None)
# ---------- WebSocket:ESP32 音频入口(ASR 上行) ----------
@app.websocket("/ws_audio")
async def ws_audio(ws: WebSocket):
global esp32_audio_ws
esp32_audio_ws = ws
await ws.accept()
print("\n[AUDIO] client connected")
recognition = None
streaming = False
last_ts = time.monotonic()
keepalive_task: Optional[asyncio.Task] = None
async def stop_rec(send_notice: Optional[str] = None):
nonlocal recognition, streaming, keepalive_task
if keepalive_task and not keepalive_task.done():
keepalive_task.cancel()
try: await keepalive_task
except Exception: pass
keepalive_task = None
if recognition:
try: recognition.stop()
except Exception: pass
recognition = None
await set_current_recognition(None)
streaming = False
if send_notice:
try: await ws.send_text(send_notice)
except Exception: pass
async def on_sdk_error(_msg: str):
await stop_rec(send_notice="RESTART")
async def keepalive_loop():
nonlocal last_ts, recognition, streaming
try:
while streaming and recognition is not None:
idle = time.monotonic() - last_ts
if idle > 0.35:
try:
for _ in range(30): # ~600ms 静音
recognition.send_audio_frame(SILENCE_20MS)
last_ts = time.monotonic()
except Exception:
await on_sdk_error("keepalive send failed")
return
await asyncio.sleep(0.10)
except asyncio.CancelledError:
return
try:
while True:
if WebSocketState and ws.client_state != WebSocketState.CONNECTED:
break
try:
msg = await ws.receive()
except WebSocketDisconnect:
break
except RuntimeError as e:
if "Cannot call \"receive\"" in str(e):
break
raise
if "text" in msg and msg["text"] is not None:
raw = (msg["text"] or "").strip()
cmd = raw.upper()
if cmd == "START":
print("[AUDIO] START received")
await stop_rec()
loop = asyncio.get_running_loop()
def post(coro):
asyncio.run_coroutine_threadsafe(coro, loop)
# 组装 ASR 回调(把依赖都注入)
cb = ASRCallback(
on_sdk_error=lambda s: post(on_sdk_error(s)),
post=post,
ui_broadcast_partial=ui_broadcast_partial,
ui_broadcast_final=ui_broadcast_final,
is_playing_now_fn=is_playing_now,
start_ai_with_text_fn=start_ai_with_text_custom, # 使用自定义版本
full_system_reset_fn=full_system_reset,
interrupt_lock=interrupt_lock,
)
recognition = dash_audio.asr.Recognition(
api_key=API_KEY, model=MODEL, format=AUDIO_FMT,
sample_rate=SAMPLE_RATE, callback=cb
)
recognition.start()
await set_current_recognition(recognition)
streaming = True
last_ts = time.monotonic()
keepalive_task = asyncio.create_task(keepalive_loop())
await ui_broadcast_partial("(已开始接收音频…)")
await ws.send_text("OK:STARTED")
elif cmd == "STOP":
if recognition:
for _ in range(15): # ~300ms 静音
try: recognition.send_audio_frame(SILENCE_20MS)
except Exception: break
await stop_rec(send_notice="OK:STOPPED")
elif raw.startswith("PROMPT:"):
# 设备端主动发起一轮:同样使用“先硬重置后播放”的强语义
text = raw[len("PROMPT:"):].strip()
if text:
async with interrupt_lock:
await start_ai_with_text_custom(text) # 使用自定义的启动函数
await ws.send_text("OK:PROMPT_ACCEPTED")
else:
await ws.send_text("ERR:EMPTY_PROMPT")
elif "bytes" in msg and msg["bytes"] is not None:
if streaming and recognition:
try:
recognition.send_audio_frame(msg["bytes"])
last_ts = time.monotonic()
except Exception:
await on_sdk_error("send_audio_frame failed")
except Exception as e:
print(f"\n[WS ERROR] {e}")
finally:
await stop_rec()
try:
if WebSocketState is None or ws.client_state == WebSocketState.CONNECTED:
await ws.close(code=1000)
except Exception:
pass
if esp32_audio_ws is ws:
esp32_audio_ws = None
print("[WS] connection closed")
# ---------- WebSocket:ESP32 相机入口(JPEG 二进制) ----------
@app.websocket("/ws/camera")
async def ws_camera_esp(ws: WebSocket):
global esp32_camera_ws, blind_path_navigator, cross_street_navigator, cross_street_active, navigation_active, orchestrator
if esp32_camera_ws is not None:
await ws.close(code=1013)
return
esp32_camera_ws = ws
await ws.accept()
print("[CAMERA] ESP32 connected")
# 【新增】初始化盲道导航器
if blind_path_navigator is None and yolo_seg_model is not None:
blind_path_navigator = BlindPathNavigator(yolo_seg_model, obstacle_detector)
print("[NAVIGATION] 盲道导航器已初始化")
else:
if blind_path_navigator is not None:
print("[NAVIGATION] 导航器已存在,无需重新初始化")
elif yolo_seg_model is None:
print("[NAVIGATION] 警告:YOLO模型未加载,无法初始化导航器")
# 【新增】初始化过马路导航器
if cross_street_navigator is None:
if yolo_seg_model:
cross_street_navigator = CrossStreetNavigator(
seg_model=yolo_seg_model,
coco_model=None, # 不使用交通灯检测
obs_model=None # 暂时也不用障碍物检测,让它更快
)
print("[CROSS_STREET] 过马路导航器已初始化(简化版 - 仅斑马线检测)")
else:
print("[CROSS_STREET] 错误:缺少分割模型,无法初始化过马路导航器")
if not yolo_seg_model:
print("[CROSS_STREET] - 缺少分割模型 (yolo_seg_model)")
if not obstacle_detector:
print("[CROSS_STREET] - 缺少障碍物检测器 (obstacle_detector)")
if orchestrator is None and blind_path_navigator is not None and cross_street_navigator is not None:
orchestrator = NavigationMaster(blind_path_navigator, cross_street_navigator)
print("[NAV MASTER] 统领状态机已初始化(托管模式)")
frame_counter = 0 # 添加帧计数器
try:
while True:
msg = await ws.receive()
if "bytes" in msg and msg["bytes"] is not None:
data = msg["bytes"]
frame_counter += 1
# 【新增】录制原始帧
try:
sync_recorder.record_frame(data)
except Exception as e:
if frame_counter % 100 == 0: # 避免日志刷屏
print(f"[RECORDER] 录制帧失败: {e}")
try:
last_frames.append((time.time(), data))
except Exception:
pass
# 推送到bridge_io(供yolomedia使用)
bridge_io.push_raw_jpeg(data)
# 【调试】检查导航条件
if frame_counter % 30 == 0: # 每30帧输出一次
state_dbg = orchestrator.get_state() if orchestrator else "N/A"
print(f"[NAVIGATION DEBUG] 帧:{frame_counter}, state={state_dbg}, yolomedia_running={yolomedia_running}")
# 统一解码(添加更严格的异常处理)
try:
arr = np.frombuffer(data, dtype=np.uint8)
bgr = cv2.imdecode(arr, cv2.IMREAD_COLOR)
# 验证解码结果
if bgr is None or bgr.size == 0:
if frame_counter % 30 == 0:
print(f"[JPEG] 解码失败:数据长度={len(data)}")
bgr = None
except Exception as e:
if frame_counter % 30 == 0:
print(f"[JPEG] 解码异常: {e}")
bgr = None
# 【托管】优先交给统领状态机(寻物未占用画面时)
# 【修改】找物品模式时不执行导航处理,让yolomedia接管画面
if orchestrator and not yolomedia_running and bgr is not None:
current_state = orchestrator.get_state()
# 【新增】找物品模式:不处理画面,等待yolomedia发送处理后的帧
if current_state == "ITEM_SEARCH":
# 找物品模式下,如果yolomedia还没开始发送帧,先显示原始画面
if not yolomedia_sending_frames and camera_viewers:
ok, enc = cv2.imencode(".jpg", bgr, [int(cv2.IMWRITE_JPEG_QUALITY), 80])
if ok:
jpeg_data = enc.tobytes()
dead = []
for viewer_ws in list(camera_viewers):
try:
await viewer_ws.send_bytes(jpeg_data)
except Exception:
dead.append(viewer_ws)
for d in dead:
camera_viewers.discard(d)
continue # 跳过后续的导航处理
out_img = bgr
try:
# 【新增】检查是否在红绿灯检测模式
if current_state == "TRAFFIC_LIGHT_DETECTION":
# 红绿灯检测模式:在主线程中直接处理,避免掉帧
import trafficlight_detection
result = trafficlight_detection.process_single_frame(bgr, ui_broadcast_callback=ui_broadcast_final)
out_img = result['vis_image'] if result['vis_image'] is not None else bgr
else:
# 其他模式:正常的导航处理
res = orchestrator.process_frame(bgr)
# 语音引导(内部已节流)
# 注:omni对话时已切换到CHAT模式,不会生成导航语音
if res.guidance_text:
try:
# 先播放语音,再广播到UI
play_voice_text(res.guidance_text)
await ui_broadcast_final(f"[导航] {res.guidance_text}")
except Exception:
pass
# 输出图像
out_img = res.annotated_image if res.annotated_image is not None else bgr
except Exception as e:
if frame_counter % 100 == 0:
print(f"[NAV MASTER] 处理帧时出错: {e}")
# 广播图像
if camera_viewers and out_img is not None:
ok, enc = cv2.imencode(".jpg", out_img, [int(cv2.IMWRITE_JPEG_QUALITY), 80])
if ok:
jpeg_data = enc.tobytes()