C#利用Vosk开源模型语音识别
#C#利用Vosk开源模型语音识别
#by wgscd
模型下载:VOSK Models (alphacephei.com) 找到chinese
Chinese | ||||
vosk-model-small-cn-0.22 | 42M | 23.54 (SpeechIO-02) 38.29 (SpeechIO-06) 17.15 (THCHS) | Lightweight model for Android and RPi | Apache 2.0 |
vosk-model-cn-0.22 | 1.3G | 13.98 (SpeechIO-02) 27.30 (SpeechIO-06) 7.43 (THCHS) |
测试效果用麦克风+大模型(不是small模型包)还是挺不错,
如果识别系统电脑声音(系统音频卡输出语音,我们使用外部输入语音)就基本是无法准确识别!可惜!
using NAudio.Wave; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Vosk; namespace DYLive { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); } private Model model; //= new Model("modelcn");//程序根目录下 // modelsmall-cn-0.22 小模型 private WasapiLoopbackCapture waveIn;//WasapiLoopbackCapture 系统音频卡输出语音,我们使用外部输入语音 //new WaveIn() 如果是录制麦克风用WaveIn private VoskRecognizer rec; private void InitRec() { waveIn = new WasapiLoopbackCapture(); //new WaveIn()、如果是录制麦克风用WaveIn waveIn.WaveFormat = new WaveFormat(16000, 16, 1);//44100 采样率16K就可以,太高会导致识别率下降,百度也使用16K waveIn.DataAvailable += WaveIn_DataAvailable; rec = new VoskRecognizer(model, waveIn.WaveFormat.SampleRate);//加载模型 rec.SetMaxAlternatives(0);//设置备选项 rec.SetWords(false);//设置是否显示时间 } private void btnStart_Click(object sender, RoutedEventArgs e) { try { if (waveIn != null) { waveIn.StartRecording(); } else { InitRec(); } } catch { } } string result = ""; private void WaveIn_DataAvailable(object sender, WaveInEventArgs e) { //int recLen = e.BytesRecorded; //byte[] data = new byte[recLen]; //Array.Copy(e.Buffer, data, recLen); if (rec.AcceptWaveform(e.Buffer, e.BytesRecorded)) { result = rec.Result()+ rec.PartialResult(); if (result.Trim() != "") { Debug.Print(result); Dispatcher.Invoke(() => { txt.Text = result; }); } } else { //Console.WriteLine(rec.FinalResult());//不要片段去分析,不然因为语义太少分析不出来 //Console.WriteLine("---------"); // Debug.Print(rec.PartialResult()); Dispatcher.Invoke(() => { Title = rec.PartialResult(); }); } } private void btnStop_Click(object sender, RoutedEventArgs e) { try { if (waveIn != null) { waveIn.StopRecording(); } } catch { } } private void Window_Loaded(object sender, RoutedEventArgs e) { App.CloseSlashWindow(); model= new Model("modelcn");//程序根目录下 new Model("modelsmall-cn-0.22");//程序根目录下 // model = new Model("modelsmall-cn-0.22");//程序根目录下 InitRec(); } } }
fffffffffffffffff
test red font.