三亩地 三亩地SAN MU DI · CODE DIARY
ARTICLE DETAIL

日记详情

真实记录编程学习的某一天,欢迎挑你感兴趣的翻一翻。

为什么选择aspire-biencoder-compsci-spec?5大核心优势解析

为什么选择aspire-biencoder-compsci-spec?5大核心优势解析

TMSpeech外部识别器开发教程:Python脚本对接完整示例

【免费下载链接】TMSpeech腾讯会议摸鱼工具项目地址: https://gitcode.com/gh_mirrors/tm/TMSpeech

TMSpeech是一款功能强大的腾讯会议摸鱼工具,支持通过外部识别器扩展语音识别能力。本文将详细介绍如何开发Python脚本作为TMSpeech的外部识别器,实现自定义语音识别功能,并提供完整的对接示例。

外部识别器开发准备工作 🚀

环境要求

开发TMSpeech外部识别器需要以下环境和依赖:

  • Python 3.6及以上版本
  • 必要的音频处理库:pyaudionumpy
  • 语音识别框架:sherpa-onnx(本文以该框架为例)

项目结构

TMSpeech项目中与外部识别器相关的文件位于external_recognizer目录下,主要包括:

  • common_audio_utils.py:音频处理通用工具
  • simulate-streaming-sense-voice.py:基于SenseVoice模型的流式识别示例
  • streaming-with-endpoint-detection.py:带端点检测的流式识别示例

核心开发步骤 🔧

1. 识别器脚本基础框架

一个标准的TMSpeech外部识别器Python脚本应包含以下核心模块:

#!/usr/bin/env python3 import argparse import sys import multiprocessing import os from common_audio_utils import ( pyaudio, sherpa_onnx, np, sample_rate, assert_file_exists, start_recording, MyPrinter, select_input_device, get_audio_devices, cleanup_recording_process ) # 全局变量定义 killed = False recording_process = None samples_queue = None stop_event = None def get_args(): # 解析命令行参数 parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) # 添加必要的参数定义 return parser.parse_args() def create_recognizer(args): # 创建识别器实例 pass def main(): # 主函数逻辑 pass if __name__ == "__main__": try: main() except KeyboardInterrupt: # 处理程序中断 pass

2. 音频采集与处理

TMSpeech外部识别器通过麦克风或系统音频采集声音,使用common_audio_utils.py中的工具函数实现:

# 获取音频设备列表 p_temp = pyaudio.PyAudio() devices = get_audio_devices(p_temp) # 选择录音设备 selected_device_indices = [] if args.device < 0: selected_device_indices = select_input_device(devices, p_temp) else: selected_device_indices = [args.device] # 创建录音进程 samples_queue = multiprocessing.Queue() stop_event = multiprocessing.Event() recording_process = multiprocessing.Process( target=start_recording, args=(selected_device_indices, samples_queue, stop_event, args.mix_mode, args.debug_save_audio) ) recording_process.start()

3. 识别器初始化与配置

以Sherpa-ONNX为例,创建识别器实例的代码如下:

def create_recognizer(args): assert_file_exists(args.encoder) assert_file_exists(args.decoder) assert_file_exists(args.joiner) assert_file_exists(args.tokens) recognizer = sherpa_onnx.OnlineRecognizer.from_transducer( tokens=args.tokens, encoder=args.encoder, decoder=args.decoder, joiner=args.joiner, num_threads=args.num_threads, sample_rate=16000, feature_dim=80, enable_endpoint_detection=True, rule1_min_trailing_silence=2.4, rule2_min_trailing_silence=1.2, decoding_method=args.decoding_method, provider=args.provider ) return recognizer

4. 音频流处理与识别

实现实时音频流识别的核心逻辑:

stream = recognizer.create_stream() while not killed: try: samples = samples_queue.get(timeout=0.5) # 从队列获取音频数据 except: continue # 将音频数据送入识别流 stream.accept_waveform(sample_rate, samples) # 处理所有准备好的音频 while recognizer.is_ready(stream): recognizer.decode_stream(stream) # 检查是否到达端点 is_endpoint = recognizer.is_endpoint(stream) # 获取识别结果 text = recognizer.get_result(stream).strip() # 输出识别结果(TMSpeech会捕获此输出) print(text) # 如果到达端点,重置流 if is_endpoint: recognizer.reset(stream)

完整示例:SenseVoice识别器对接

simulate-streaming-sense-voice.py是一个完整的外部识别器示例,使用SenseVoice模型实现语音识别:

模型下载与配置

该脚本需要以下模型文件:

  • silero_vad.onnx(语音活动检测模型)
  • SenseVoice模型文件(model.onnx)
  • tokens.txt(词汇表文件)

脚本会自动检查模型文件是否存在,如果不存在会提示下载链接:

vad_model_url = 'https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/silero_vad.onnx' onnx_model_url = 'https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-int8-2025-09-09.tar.bz2' def try_download_model(vad_model_path, onnx_model_path): if not os.path.exists(vad_model_path): print(f"请下载文件到:{vad_model_path}", file=sys.stderr) print(f"下载链接:{vad_model_url}", file=sys.stderr) if not os.path.exists(onnx_model_path): print(f"请下载并解压文件夹到:{onnx_model_path}", file=sys.stderr) print(f"下载链接:{onnx_model_url}", file=sys.stderr)

VAD语音活动检测

该示例集成了VAD(语音活动检测)功能,能够自动检测语音的开始和结束:

config = sherpa_onnx.VadModelConfig() config.silero_vad.model = args.silero_vad_model config.silero_vad.threshold = 0.5 config.silero_vad.min_silence_duration = 0.1 # 静音时长阈值 config.silero_vad.min_speech_duration = 0.25 # 最小语音时长 config.silero_vad.max_speech_duration = 8 # 最大语音时长 config.sample_rate = sample_rate vad = sherpa_onnx.VoiceActivityDetector(config, buffer_size_in_seconds=100)

TMSpeech配置外部识别器

开发完成后,需要在TMSpeech中配置使用外部识别器:

  1. 打开TMSpeech配置界面,切换到"语音识别"选项卡
  2. 在"语音识别器"下拉菜单中选择"命令行识别器"

  1. 在资源配置界面确保已安装所需的模型文件

  1. 配置命令行参数,指定外部识别器脚本路径和参数:
python external_recognizer/simulate-streaming-sense-voice.py --silero-vad-model=silero_vad.onnx --sense-voice=model.onnx --tokens=tokens.txt

调试与优化技巧 💡

日志输出

在开发过程中,可以通过stderr输出调试信息,TMSpeech会将这些信息保存到日志文件中:

print("识别已启动,请说话", file=sys.stderr)

多设备支持

TMSpeech外部识别器支持多设备录音,可通过--mix-mode参数设置混音模式:

  • average:平均混音(默认)
  • add:加法混音

性能优化

  • 调整线程数:通过--num-threads参数设置识别线程数
  • 选择合适的计算设备:通过--provider参数选择cpucuda
  • 模型优化:使用int8量化模型减小内存占用和提高速度

总结

通过本文介绍的方法,你可以轻松开发TMSpeech的外部识别器,扩展其语音识别能力。TMSpeech提供了灵活的插件架构和完善的工具支持,使外部识别器的开发变得简单高效。

完整的示例代码可参考项目中的external_recognizer目录,包括:

  • simulate-streaming-sense-voice.py
  • streaming-with-endpoint-detection.py
  • common_audio_utils.py

开始开发你自己的TMSpeech外部识别器,体验更强大的语音识别功能吧!

【免费下载链接】TMSpeech腾讯会议摸鱼工具项目地址: https://gitcode.com/gh_mirrors/tm/TMSpeech

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

← 返回列表