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模型包)还是挺不错,
如果识别系统电脑声音(系统音频卡输出语音,我们使用外部输入语音)就基本是无法准确识别!可惜!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | 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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)