服务好的企业网站怎么建设,服务好的建筑企业查询,站长素材官网,wordpress更换域名2017背景
本系列主要目标初步完成一款智能音箱的基础功能#xff0c;包括语音唤醒、语音识别(语音转文字)、处理用户请求#xff08;比如查天气等#xff0c;主要通过rasa自己定义意图实现#xff09;、语音合成(文字转语音)功能。
语音识别、语音合成采用离线方式实现。
语…背景
本系列主要目标初步完成一款智能音箱的基础功能包括语音唤醒、语音识别(语音转文字)、处理用户请求比如查天气等主要通过rasa自己定义意图实现、语音合成(文字转语音)功能。
语音识别、语音合成采用离线方式实现。
语音识别使用sherpa-onnx可以实现离线中英文语音识别。
本文用到的一些安装包在snowboy那一篇的必要条件中已经完成了部分构建在离线语音识别安装完成之后也会把相关代码写到snowboy项目中语音唤醒之后调用语音识别翻译用户说话的内容。
语音唤醒文章地址
snowboy 自定义唤醒词 实现语音唤醒【语音助手】_殷长庆的博客-CSDN博客
参考文章
sherpa-onnx教程强烈建议按官网的步骤安装
Installation — sherpa 1.3 documentation
sherpa-onnx的预编译模型
Pre-trained models — sherpa 1.3 documentation
实践
下载安装sherpa-onnx
cd /home/testgit clone https://github.com/k2-fsa/sherpa-onnx
cd sherpa-onnx
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPERelease ..
make -j6
安装完成之后会在bin目录下发现sherpa-onnx的可执行文件
下载预编译模型
我选择的是offline-paraformer版本的模型因为他同时支持中英文的离线识别这个离线识别是基于wav视频文件的正好满足要求。
参考官网地址
Paraformer models — sherpa 1.3 documentation
下面是操作步骤
cd /home/test/sherpa-onnxGIT_LFS_SKIP_SMUDGE1 git clone https://huggingface.co/csukuangfj/sherpa-onnx-paraformer-zh-2023-03-28
cd sherpa-onnx-paraformer-zh-2023-03-28
git lfs pull --include *.onnx
检查是否下载成功注意看模型文件的大小
sherpa-onnx-paraformer-zh-2023-03-28$ ls -lh *.onnx
-rw-r--r-- 1 kuangfangjun root 214M Apr 1 07:28 model.int8.onnx
-rw-r--r-- 1 kuangfangjun root 824M Apr 1 07:28 model.onnx
可以看到两个模型文件这俩模型本机测试感觉差距不是太大我选择的是int8这个版本
测试语音识别
测试以下语音识别效果
cd /home/test/sherpa-onnx./build/bin/sherpa-onnx-offline \--tokens./sherpa-onnx-paraformer-zh-2023-03-28/tokens.txt \--paraformer./sherpa-onnx-paraformer-zh-2023-03-28/model.int8.onnx \./sherpa-onnx-paraformer-zh-2023-03-28/test_wavs/0.wav
出现相应的正确打印就代表语音识别准备工作完成了
集成到snowboy
首先在sherpa-onnx目录的python-api-examples下有python的api我们需要的是offline-decode-files.py这个文件其中main()方法用来离线识别一个wav文件。
接下来我们对该文件进行一点点的修改主要是把模型的默认参数配置好然后识别完成之后返回识别内容
offlinedecode.py
把offline-decode-files.py文件更名为offlinedecode.py或者是新建一个offlinedecode.py文件
touch offlinedecode.pyvim offlinedecode.py
编辑文件的内容
#!/usr/bin/env python3
#
# Copyright (c) 2023 by manyeyes
This file demonstrates how to use sherpa-onnx Python API to transcribe
file(s) with a non-streaming model.
Please refer to
https://k2-fsa.github.io/sherpa/onnx/index.html
to install sherpa-onnx and to download the pre-trained models
used in this file.import time
import wave
from typing import List, Tupleimport numpy as np
import sherpa_onnxclass Constants:encoder # or 如果用zipformer模型需要修改成zipformer的 encoder-epoch-12-avg-4.int8.onnxdecoder # or 如果用zipformer模型需要修改成zipformer的decoder-epoch-12-avg-4.int8.onnxjoiner # or 如果用zipformer模型需要修改成zipformer的joiner-epoch-12-avg-4.int8.onnxtokens/home/test/sherpa-onnx/sherpa-onnx-paraformer-zh-2023-03-28/tokens.txt # 如果用zipformer模型需要修改成zipformer的tokens.txtnum_threads1sample_rate16000feature_dim80decoding_methodgreedy_search # Or modified_ Beam_ Search, only used when the encoder is not emptycontexts # 关键词微调只在modified_ Beam_ Search模式下有用context_score1.5debugFalsemodeling_unitcharparaformer/home/test/sherpa-onnx/sherpa-onnx-paraformer-zh-2023-03-28/model.int8.onnx # 实际上使用的是该模型global args,contexts_list,recognizer
args Constants()def encode_contexts(args, contexts: List[str]) - List[List[int]]:tokens {}with open(args.tokens, r, encodingutf-8) as f:for line in f:toks line.strip().split()tokens[toks[0]] int(toks[1])return sherpa_onnx.encode_contexts(modeling_unitargs.modeling_unit, contextscontexts, spNone, tokens_tabletokens)def read_wave(wave_filename: str) - Tuple[np.ndarray, int]:Args:wave_filename:Path to a wave file. It should be single channel and each sample shouldbe 16-bit. Its sample rate does not need to be 16kHz.Returns:Return a tuple containing:- A 1-D array of dtype np.float32 containing the samples, which arenormalized to the range [-1, 1].- sample rate of the wave filewith wave.open(wave_filename) as f:assert f.getnchannels() 1, f.getnchannels()assert f.getsampwidth() 2, f.getsampwidth() # it is in bytesnum_samples f.getnframes()samples f.readframes(num_samples)samples_int16 np.frombuffer(samples, dtypenp.int16)samples_float32 samples_int16.astype(np.float32)samples_float32 samples_float32 / 32768return samples_float32, f.getframerate()# 初始化因为用到的是paraformer所以实际上初始化的是paraformer的识别
def init():global argsglobal recognizerglobal contexts_listcontexts_list[]if args.encoder:contexts [x.strip().upper() for x in args.contexts.split(/) if x.strip()]if contexts:print(fContexts list: {contexts})contexts_list encode_contexts(args, contexts)recognizer sherpa_onnx.OfflineRecognizer.from_transducer(encoderargs.encoder,decoderargs.decoder,joinerargs.joiner,tokensargs.tokens,num_threadsargs.num_threads,sample_rateargs.sample_rate,feature_dimargs.feature_dim,decoding_methodargs.decoding_method,context_scoreargs.context_score,debugargs.debug,)elif args.paraformer:recognizer sherpa_onnx.OfflineRecognizer.from_paraformer(paraformerargs.paraformer,tokensargs.tokens,num_threadsargs.num_threads,sample_rateargs.sample_rate,feature_dimargs.feature_dim,decoding_methodargs.decoding_method,debugargs.debug,)# 语音识别
# *sound_files 要识别的音频路径
# return 识别后的结果
def asr(*sound_files):global argsglobal recognizerglobal contexts_liststart_time time.time()streams []total_duration 0for wave_filename in sound_files:samples, sample_rate read_wave(wave_filename)duration len(samples) / sample_ratetotal_duration durationif contexts_list:s recognizer.create_stream(contexts_listcontexts_list)else:s recognizer.create_stream()s.accept_waveform(sample_rate, samples)streams.append(s)recognizer.decode_streams(streams)results [s.result.text for s in streams]end_time time.time()for wave_filename, result in zip(sound_files, results):return f{result}
编辑完成保存把文件移动到snowboy的Python3目录下
mv offlinedecode.py /home/test/snowboy/examples/Python3/
demo.py
修改snowboy的demo.py文件
cd /home/test/snowboy/examples/Python3/vim demo.py
主要修改为snowboy唤醒设备之后开始录音当结束录音时调用sherpa-onnx识别语音内容把demo.py修改为以下内容
import snowboydecoder
import signal
import os
import offlinedecodeinterrupted Falsedef signal_handler(signal, frame):global interruptedinterrupted Truedef interrupt_callback():global interruptedreturn interrupted# 初始化语音识别
offlinedecode.init()# 唤醒词模型文件
model ../../model/hotword.pmdl# capture SIGINT signal, e.g., CtrlC
signal.signal(signal.SIGINT, signal_handler)detector snowboydecoder.HotwordDetector(model, sensitivity0.5)
print(Listening... Press CtrlC to exit)# 录音之后的回调
# fname 音频文件路径
def audio_recorder_callback(fname):text offlinedecode.asr(fname)# 打印识别内容print(text)# 删除录音文件if isinstance(fname, str) and os.path.exists(fname):if os.path.isfile(fname):os.remove(fname)# main loop
detector.start(detected_callbacksnowboydecoder.play_audio_file,audio_recorder_callbackaudio_recorder_callback,interrupt_checkinterrupt_callback,sleep_time0.03)detector.terminate()
编辑完成保存然后测试是否有识别成功
测试集成效果
cd /home/test/snowboy/examples/Python3/python demo.py
成功之后会打印识别内容然后删除本地录音文件。