129 lines
3.6 KiB
Python
129 lines
3.6 KiB
Python
"""
|
|
FunASR 使用示例
|
|
展示常见的语音识别应用场景
|
|
"""
|
|
|
|
from asr_service import ASRService, recognize_audio
|
|
|
|
|
|
def example_1_basic_recognition():
|
|
"""示例1: 基础识别"""
|
|
print("=" * 60)
|
|
print("示例1: 基础语音识别")
|
|
print("=" * 60)
|
|
|
|
# 方式1: 使用便捷函数
|
|
# results = recognize_audio("meeting.wav")
|
|
|
|
# 方式2: 使用服务类(推荐,可复用)
|
|
service = ASRService(model_name="paraformer-zh")
|
|
# results = service.recognize("meeting.wav")
|
|
|
|
print("代码:")
|
|
print(" from asr_service import recognize_audio")
|
|
print(" results = recognize_audio('meeting.wav')")
|
|
print(" for sent in results:")
|
|
print(" print(sent)")
|
|
print()
|
|
print("输出格式:")
|
|
print(" [SPEAKER_00] 大家好,今天的会议现在开始。 (0.50s - 3.20s)")
|
|
print(" [SPEAKER_01] 好的,我先汇报一下进度。 (3.50s - 6.10s)")
|
|
|
|
|
|
def example_2_batch_processing():
|
|
"""示例2: 批量处理"""
|
|
print("\n" + "=" * 60)
|
|
print("示例2: 批量处理多个音频")
|
|
print("=" * 60)
|
|
|
|
print("代码:")
|
|
print(" from pathlib import Path")
|
|
print(" from asr_service import ASRService")
|
|
print()
|
|
print(" service = ASRService()")
|
|
print(" audio_files = list(Path('./audio').glob('*.wav'))")
|
|
print(" results = service.recognize_batch(audio_files)")
|
|
print()
|
|
print(" for audio_path, sentences in zip(audio_files, results):")
|
|
print(" print(f'{audio_path}: {len(sentences)} 句话')")
|
|
|
|
|
|
def example_3_export_results():
|
|
"""示例3: 导出结果"""
|
|
print("\n" + "=" * 60)
|
|
print("示例3: 导出识别结果")
|
|
print("=" * 60)
|
|
|
|
print("代码:")
|
|
print(" service = ASRService()")
|
|
print(" sentences = service.recognize('meeting.wav')")
|
|
print()
|
|
print(" # 导出为 JSON")
|
|
print(" service.export_to_json(sentences, 'meeting.json')")
|
|
print()
|
|
print(" # 导出为 SRT 字幕")
|
|
print(" service.export_to_srt(sentences, 'meeting.srt')")
|
|
print()
|
|
print("JSON 输出示例:")
|
|
print(""" {
|
|
"total_sentences": 2,
|
|
"sentences": [
|
|
{
|
|
"speaker": "SPEAKER_00",
|
|
"text": "大家好",
|
|
"begin_time": 0.50,
|
|
"end_time": 3.20,
|
|
"duration": 2.70
|
|
}
|
|
]
|
|
}""")
|
|
|
|
|
|
def example_4_different_models():
|
|
"""示例4: 选择不同模型"""
|
|
print("\n" + "=" * 60)
|
|
print("示例4: 选择不同模型")
|
|
print("=" * 60)
|
|
|
|
print("模型选择:")
|
|
print()
|
|
print("1. paraformer-zh (默认)")
|
|
print(" - 达摩院出品,中文识别精度高")
|
|
print(" - 支持说话人分离")
|
|
print(" - 代码: ASRService(model_name='paraformer-zh')")
|
|
print()
|
|
print("2. SenseVoice")
|
|
print(" - 多语言支持(中、英、日、韩等)")
|
|
print(" - 支持情感识别")
|
|
print(" - 代码: ASRService(model_name='SenseVoice')")
|
|
|
|
|
|
def example_5_hardware_options():
|
|
"""示例5: 硬件选择"""
|
|
print("\n" + "=" * 60)
|
|
print("示例5: 选择运行设备")
|
|
print("=" * 60)
|
|
|
|
print("设备选项:")
|
|
print()
|
|
print(" # 自动选择 (推荐)")
|
|
print(" service = ASRService(device='auto')")
|
|
print()
|
|
print(" # 使用 GPU")
|
|
print(" service = ASRService(device='cuda')")
|
|
print()
|
|
print(" # 使用 CPU")
|
|
print(" service = ASRService(device='cpu')")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
example_1_basic_recognition()
|
|
example_2_batch_processing()
|
|
example_3_export_results()
|
|
example_4_different_models()
|
|
example_5_hardware_options()
|
|
|
|
print("\n" + "=" * 60)
|
|
print("提示: 运行测试请使用: python test_asr.py -f your_audio.wav")
|
|
print("=" * 60)
|