唤醒词的离线语音唤醒实现方案二 ubuntu 22.04

自定义修改唤醒词的离线语音唤醒实现方案二(ubuntu 22.04)

实现功能(离线唤醒):

  • 指定唤醒词可随时唤醒目标
  • 唤醒词可自定义修改
  • 本地修改唤醒词且不需要繁琐生成模型和代码
  • 源码后续上传 github 并更新

实验方案二:使用讯飞开放平台的 AIkit语音唤醒 Linux SDK

1. 讯飞开放平台的语音唤醒(新版)
2. 下载SDK
  • 使用的ubuntu 22.04,下载Linux AIKit

  • SDK 包下载(我只选择了语音唤醒一个包)
    在这里插入图片描述

3. SDK 修改编译

主函数在samples目录下:

$ tree samples/
samples/
├── ivw_record_sample
│   ├── build.sh
│   ├── ivw_record_sample.cpp		# 本次使用有文件唤醒和MIC唤醒
│   ├── linuxrec.c
│   ├── Makefile
│   ├── readme.txt
│   └── speech_recognizer.cpp
└── ivw_sample
    ├── build.sh
    ├── ivw_sample.cpp
    └── readme.txt
3.1 SDK初始化参数 (ivw_record_sample.cpp)
  • appID、apiKey、apiSecret 和 workDir 为必填项

在这里插入图片描述

AIKIT_Configurator::builder()
      .app()      
         .appID("")
         .apiKey("")
         .apiSecret("")
         .workDir("./")          //示例为当前路径
        .auth()
         .ability("e867a88f2")
        .log()  
         .logLevel(LOG_LVL_INFO)
         .logPath("./");
 
int ret = AIKIT_Init();
if(ret != 0){
    printf("AIKIT_Init failed:%d\n",ret);
    goto exit;
}
3.2 编译
  • ivw_record_sample

    $ cd samples
    make			# 直接编译
    $ cd ../bin		# bin目录下会生成一个同名可执行文件 ivw_record_sample
    $ export LD_LIBRARY_PATH=../libs:$LD_LIBRARY_PATH   # 环境变量
    $ ./ivw_record_sample	#运行
    

    按提示操作就可以运行了,但我觉得选来选去每次都麻烦,作了一些修改

3.3 自定义唤醒词
  • 自定义添加/修改唤醒词

    $ cd PATH/bin/resource
    $ vim many-keywords.txt			#直接按照里面格式进行添加,也可以自己创建 .txt 添加唤醒词
    # 例,添加‘你好老许’ : ‘ 你好老许;nCM:100; ‘
    $ cat many-keywords.txt 		# 也支持添加多个唤醒词
    你好老许;nCM:100;
    
4. 代码修改
4.1 特定唤醒词唤醒(当监测到 ‘你好老许’ 时唤醒并播放提示音)

指定唤醒词好处,可以使用特定唤醒词执行不同命令;坏处,每次添加唤醒词代码需要改动;

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "speech_recognizer.h"

#include "aikit_biz_api.h"
#include "aikit_constant.h"
#include "aikit_biz_config.h"

using namespace std;
using namespace AIKIT;
static const char *ABILITY = "e867a88f2";


void OnOutput(AIKIT_HANDLE* handle, const AIKIT_OutputData* output){
   
   
    printf("OnOutput abilityID :%s\n",handle->abilityID);
    printf("OnOutput key:%s\n",output->node->key
<think>好的,用户现在想要一个完全本地化的中文语音对话系统,不使用任何API,所有模型都要下载到本地。之前我已经给过一些方案,现在需要更详细的步骤。首先,用户可能有一定的技术背景,但可能对具体的实施步骤不太清楚,需要详细指导。用户可能希望搭建一个类似语音助手的东西,能够在本地运行,保护隐私,或者用于特定项目。 首先,我需要确定用户的需求是否包括离线唤醒词,比如像“小爱同学”这样的唤醒功能,或者只是直接按下按钮开始对话。之前的回复提到了树莓派和ReSpeaker麦克风阵列,可能用户有硬件方面的考虑。但用户也可能只是在普通的PC上运行,需要明确。 接下来,用户需要详细的步骤,包括环境配置、模型下载、代码示例等。可能需要分步骤说明,从环境准备开始,安装必要的库,下载模型,然后整合语音识别、语言模型和语音合成。同时要确保所有模型都是开源的,可以在本地运行,不需要调用外部API。 考虑到中文语音处理,可能需要特定的模型,比如Paraformer用于语音识别,ChatGLM3作为语言模型,VITS或Bert-VITS2用于语音合成。这些模型是否有现成的本地部署方式,是否需要转换格式,比如使用Hugging Face的Transformers库加载。 另外,用户可能遇到的环境问题,比如Python版本、依赖冲突、GPU支持等,需要提前说明。可能还需要指导如何测试每个组件,确保语音识别和合成正常工作,再整合到对话流程中。 还需要考虑实时性问题,语音识别是否是流式的,合成是否有延迟,是否需要优化。例如,使用faster-whisper代替原版Whisper提升速度,或者使用VITS的快速版本。 最后,可能需要提供一个完整的代码示例,将各个部分串联起来,并说明如何运行和调试。同时提醒用户注意中文特性,如标点预测、儿化音处理,以及可能的方言支持。 总结步骤大致是:环境准备→下载模型→语音识别配置→语言模型配置→语音合成配置→整合代码→测试优化。需要详细说明每个部分的具体操作,确保用户能一步步跟着做,避免遗漏关键点。</think>以下是完全基于本地模型的中文语音对话系统搭建指南,无需任何API调用,所有模型均可下载到本地运行: --- ### **环境准备(以Ubuntu 22.04为例)** ```bash # 创建虚拟环境 conda create -n voicebot python=3.10 conda activate voicebot # 安装核心依赖 pip install torch==2.0.1 torchaudio==2.0.2 --extra-index-url https://download.pytorch.org/whl/cu117 pip install modelscope transformers faster-whisper sounddevice pydub ``` --- ### **步骤一:本地语音识别(ASR)** **1. 下载Paraformer模型** ```bash git clone https://www.modelscope.cn/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch.git ``` **2. 实时语音识别脚本(`asr_service.py`)** ```python from modelscope.pipelines import pipeline from modelscope.utils.constant import Tasks asr_pipeline = pipeline( task=Tasks.auto_speech_recognition, model="./speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch" ) def audio_callback(indata, frames, time, status): audio_data = indata.copy() # 保存为16kHz单声道WAV sf.write('temp.wav', audio_data, 16000, subtype='PCM_16') result = asr_pipeline(audio_in='temp.wav') print("识别结果:", result['text']) # 开始录音 import sounddevice as sd with sd.InputStream(callback=audio_callback, blocksize=16000*2, samplerate=16000): print("正在监听...") sd.sleep(100000) ``` --- ### **步骤:本地语言模型(LLM)** **1. 下载ChatGLM3-6B模型** ```bash git lfs install git clone https://huggingface.co/THUDM/chatglm3-6b ``` **2. 本地推理API(`llm_server.py`)** ```python from transformers import AutoModel, AutoTokenizer import torch tokenizer = AutoTokenizer.from_pretrained("./chatglm3-6b", trust_remote_code=True) model = AutoModel.from_pretrained( "./chatglm3-6b", trust_remote_code=True, device_map="auto", torch_dtype=torch.float16 ).eval() def generate_response(text): response, _ = model.chat(tokenizer, text, history=[]) return response if __name__ == '__main__': while True: text = input("输入:") print("回复:", generate_response(text)) ``` --- ### **步骤三:本地语音合成(TTS)** **1. 下载Bert-VITS2模型** ```bash git clone https://huggingface.co/GaryGuan/Bert-VITS2-2.3 ``` **2. 语音合成脚本(`tts_service.py`)** ```python import torch from transformers import VitsModel, AutoTokenizer model = VitsModel.from_pretrained("./Bert-VITS2-2.3") tokenizer = AutoTokenizer.from_pretrained("./Bert-VITS2-2.3") def text_to_speech(text): inputs = tokenizer(text, return_tensors="pt") with torch.no_grad(): output = model(**inputs).waveform return output.numpy() # 测试播放 import sounddevice as sd audio = text_to_speech("欢迎使用本地语音助手") sd.play(audio, samplerate=22050) sd.wait() ``` --- ### **完整整合方案** **1. 创建主程序(`main.py`)** ```python import asyncio from llm_server import generate_response from tts_service import text_to_speech import sounddevice as sd class VoiceAssistant: def __init__(self): self.asr_running = False async def listen_loop(self): def callback(indata, frames, time, status): # 此处添加语音端点检测(VAD)逻辑 sf.write('temp.wav', indata, 16000) with sd.InputStream(callback=callback, samplerate=16000): while True: await asyncio.sleep(0.1) async def process_text(self, text): response = generate_response(text) audio = text_to_speech(response) sd.play(audio, samplerate=22050) async def run(self): await asyncio.gather( self.listen_loop(), self.process_text() ) if __name__ == '__main__': assistant = VoiceAssistant() asyncio.run(assistant.run()) ``` --- ### **优化技巧** 1. **加速推理**: ```bash # 量化语言模型 python convert.py --model_path ./chatglm3-6b --quantization int8 ``` 2. **语音端点检测**: ```python # 添加webrtcvad检测 pip install webrtcvad import webrtcvad vad = webrtcvad.Vad(3) # 灵敏度等级 ``` 3. **硬件加速**: ```bash # 使用TensorRT加速 docker pull nvcr.io/nvidia/tensorrt:23.09-py3 ``` --- ### **部署架构** ``` 麦克风输入 → Paraformer ASR → ChatGLM3 → Bert-VITS2 → 扬声器输出 ↑ ↑ 本地模型 本地模型 ``` --- ### **常见问题解决** 1. **中文标点缺失**: 在ASR输出后添加标点预测模型: ```bash git clone https://www.modelscope.cn/damo/punc_ct-transformer_zh-cn-common-vocab272727-pytorch.git ``` 2. **合成语音生硬**: 调整VITS参数: ```python inputs["speech_rate"] = 0.9 # 语速 inputs["intonation_scale"] = 1.2 # 语调 ``` 3. **实时延迟过高**: 启用流式ASR: ```python asr_pipeline = pipeline(..., streaming=True) ``` --- 以上方案可在配备至少16GB内存、支持CUDA的显卡(如RTX 3060)的本地环境中运行,完全无需互联网连接。建议使用WSL2(Windows)或原生Linux系统以获得最佳性能。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值