测试离线音频转文本模型Whisper.net的基本用法

测试离线音频转文本模型Whisper.net的基本用法

    正在检查是否收录...
一言准备中...

  微信公众号“dotNET跨平台”中的文章《OpenAI的离线音频转文本模型Whisper的.NET封装项目》介绍了基于.net封装的开源语音辨识Whisper神经网络项目Whisper.net,其GitHub地址见参考文献2。本文基于Whisper.net帮助文档中的示例,测试Whisper.net的基本用法。
  创建基于.net6的Winform项目,然后在NuGet包管理器中搜索并安装Whisper.net包,如下图所示。注意,如果搜索Whisper,还会搜到一个名为WhisperNet的包,这个包跟Whisper.net的用法不同,也不能通用,注意不要安装错了。



  在测试程序之前,需要下载语言模型文件,可以从参考文献3中下载。根据参考文献1中的介绍,下载了ggml-large.bin、ggml-medium.bin、ggml-small.bin等3个支持中文的语言模型文件,并在测试程序中优先选用ggml-large.bin进行测试。
  除了语言模型文件,Whisper.net对音频文件要求比较严格,必须是采样率为16KHz的wav格式的音频文件,具体的文件格式要求参见下面列出的Whisper.net包中WaveParser类中的部分格式检查代码。

 byte[] array = new byte[36]; if (waveStream.Read(array, 0, 36) != 36) { throw new CorruptedWaveException("Invalid wave file, the size is too small."); } if (array[0] != 82 || array[1] != 73 || array[2] != 70 || array[3] != 70) { throw new CorruptedWaveException("Invalid wave file RIFF header."); } if (array[8] != 87 || array[9] != 65 || array[10] != 86 || array[11] != 69 || array[12] != 102 || array[13] != 109 || array[14] != 116 || array[15] != 32) { throw new CorruptedWaveException("Invalid wave file header."); } int num = BitConverter.ToInt32(array, 16); if (num < 0) { throw new CorruptedWaveException("Invalid wave format size."); } if (BitConverter.ToUInt16(array, 20) != 1) { throw new CorruptedWaveException("Unsupported wave file"); } channels = BitConverter.ToUInt16(array, 22); sampleRate = BitConverter.ToUInt32(array, 24); if (sampleRate != 16000) { throw new NotSupportedWaveException("Only 16KHz sample rate is supported."); } bitsPerSample = BitConverter.ToUInt16(array, 34); if (bitsPerSample != 16) { throw new NotSupportedWaveException("Only 16 bits per sample is supported."); } 

  本文采用两个音频文件进行测试,第一个是Whisper.net项目自带示例程序中的kennedy.wav文件,用于测试识别英文,另一个是在B站上下载古诗《春晓》的mp4视频文件(参考文献4),用于测试识别中文,通过参考文献5在线将其转换为指定采样率的wav文件,如下图所示。

  测试程序的主要代码参考自Whisper.net项目中的示例程序Whisper.net.Tests中的代码。采用Whisper.net识别语音可以采用同步方式或异步方式,示例程序中都有相应的代码,本文采用同步方式的代码进行测试。主要代码如下所示:

 try { txtResult.Text = String.Empty; var segments = new List<SegmentData>(); var encoderBegins = new List<EncoderBeginData>(); using var factory = WhisperFactory.FromPath("ggml-large.bin"); using var processor = factory.CreateBuilder() .WithLanguage("auto") .WithEncoderBeginHandler((e) => { encoderBegins.Add(e); return true; }) .WithSegmentEventHandler(segments.Add) .Build(); using var fileReader = File.OpenRead(txtFilePath.Text); processor.Process(fileReader); foreach (var segment in segments) { txtResult.Text += "\r\n" + ($"New Segment: {segment.Start} ==> {segment.End} : {segment.Text}"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } 

  程序运行效果如下面的截图所示:


  后续还会继续学习Whisper.net的用法。

参考文献:
[1]https://it.sohu.com/a/670010700_121124363
[2]https://github.com/sandrohanea/whisper.net
[3]https://huggingface.co/ggerganov/whisper.cpp/tree/main
[4]https://www.bilibili.com/video/BV19W411k7Bo/?spm_id_from=333.337.search-card.all.click&vd_source=db4a1f65c18549c78df3e9d579e59e19
[5]https://www.aconvert.com/cn/audio/

whispercode参考文献cto语言模型githubgit公众号url文件格式createmediumparse微信公众号帮助文档huggingfacecligan跨平台音频转文本神经网络openaiide
  • 本文作者:WAP站长网
  • 本文链接: https://wapzz.net/post-185.html
  • 版权声明:本博客所有文章除特别声明外,均默认采用 CC BY-NC-SA 4.0 许可协议。
本站部分内容来源于网络转载,仅供学习交流使用。如涉及版权问题,请及时联系我们,我们将第一时间处理。
文章很赞!支持一下吧 还没有人为TA充电
为TA充电
还没有人为TA充电
0
  • 支付宝打赏
    支付宝扫一扫
  • 微信打赏
    微信扫一扫
感谢支持
文章很赞!支持一下吧
关于作者
2.7W+
9
1
2
WAP站长官方

AI绘画,画渣or画神?百度文心·一格AI作画平台初体验与总结

上一篇

快速训练自己的大语言模型:基于LLAMA-7B的lora指令微调

下一篇
  • 复制图片
按住ctrl可打开默认菜单