test_streaming_demo_real.py 4.07 KB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
AIfeng/2025-01-07 17:15:00
流式语音识别演示应用测试脚本
测试真实ASR服务集成功能
"""

import os
import sys
import time
import subprocess
import threading

# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

def check_funasr_service():
    """检查FunASR服务是否运行"""
    try:
        import websockets
        import asyncio
        
        async def test_connection():
            try:
                uri = "ws://127.0.0.1:10197"
                async with websockets.connect(uri, timeout=3) as websocket:
                    return True
            except Exception:
                return False
        
        return asyncio.run(test_connection())
    except ImportError:
        print("⚠️ websockets模块未安装,无法检查FunASR服务")
        return False
    except Exception as e:
        print(f"⚠️ 检查FunASR服务时出错: {e}")
        return False

def check_audio_devices():
    """检查音频设备"""
    try:
        import pyaudio
        audio = pyaudio.PyAudio()
        device_count = 0
        
        for i in range(audio.get_device_count()):
            device_info = audio.get_device_info_by_index(i)
            if device_info['maxInputChannels'] > 0:
                device_count += 1
        
        audio.terminate()
        return device_count > 0
    except ImportError:
        print("⚠️ pyaudio模块未安装,无法检查音频设备")
        return False
    except Exception as e:
        print(f"⚠️ 检查音频设备时出错: {e}")
        return False

def main():
    """主测试函数"""
    print("=" * 60)
    print("流式语音识别演示应用 - 真实ASR服务测试")
    print("作者: AIfeng")
    print("时间: 2025-01-07 17:15:00")
    print("=" * 60)
    
    # 检查依赖
    print("\n🔍 检查系统依赖...")
    
    # 检查FunASR服务
    print("1. 检查FunASR服务...")
    if check_funasr_service():
        print("   ✅ FunASR服务运行正常")
    else:
        print("   ❌ FunASR服务未运行")
        print("   💡 请先启动FunASR服务:")
        print("      python -u web/asr/funasr/ASR_server.py --host \"127.0.0.1\" --port 10197 --ngpu 1")
        
        choice = input("\n是否继续测试? (y/N): ").strip().lower()
        if choice != 'y':
            return 1
    
    # 检查音频设备
    print("\n2. 检查音频设备...")
    if check_audio_devices():
        print("   ✅ 找到可用的音频输入设备")
    else:
        print("   ❌ 未找到可用的音频输入设备")
        print("   💡 请确保麦克风已连接并正常工作")
        
        choice = input("\n是否继续测试? (y/N): ").strip().lower()
        if choice != 'y':
            return 1
    
    # 检查配置文件
    print("\n3. 检查配置文件...")
    config_path = "streaming/streaming_config.json"
    if os.path.exists(config_path):
        print(f"   ✅ 配置文件存在: {config_path}")
    else:
        print(f"   ⚠️ 配置文件不存在: {config_path}")
        print("   💡 将使用默认配置")
    
    print("\n" + "=" * 60)
    print("🚀 启动流式语音识别演示应用")
    print("=" * 60)
    
    try:
        # 导入并启动演示应用
        from streaming_demo import StreamingRecognitionDemo
        
        demo = StreamingRecognitionDemo(config_path if os.path.exists(config_path) else None)
        return demo.start()
        
    except ImportError as e:
        print(f"❌ 导入演示应用失败: {e}")
        print("💡 请确保所有依赖模块都已正确安装")
        return 1
    except Exception as e:
        print(f"❌ 启动演示应用失败: {e}")
        return 1

if __name__ == "__main__":
    try:
        exit_code = main()
        print("\n👋 感谢使用流式语音识别演示应用!")
        sys.exit(exit_code)
    except KeyboardInterrupt:
        print("\n\n⚠️ 程序被用户中断")
        sys.exit(0)
    except Exception as e:
        print(f"\n❌ 程序异常退出: {e}")
        sys.exit(1)