0


Whisper.cpp 编译使用

Whisper.cpp 编译使用

  1. whisper.cpp

是牛人 ggerganov 对 openai 的

  1. whisper

语音识别模型用 C++ 重新实现的项目,开源在 github 上,具有轻量、性能高,实用性强等特点。这篇文章主要记录在 windows 平台,如何使用该模型在本地端进行语音识别。

下载

whisper.cpp 的开源地址在 ggerganov/whisper.cpp: Port of OpenAI’s Whisper model in C/C++ (github.com),首先将项目下载在本地。

  1. git clone https://github.com/ggerganov/whisper.cpp
  1. whisper.cpp

项目里提供了几个现成的模型。建议下载 small 以上的模型,不然识别效果完全无法使用。

我们可以使用下面命令下载指定模型,下载好之后,它们存储在 models 文件夹下。

  1. .\models\download-ggml-model.cmd small

编译

在项目根目录执行 make 即可编译,得到 main.exe 可执行文件,然后用 main 文件就可以转录本地语音文件了。

  1. > make

转录本地语言

samples 语言文件

  • 先我们用项目里自带的一段语音,来体验一下识别效果。其中 - -m 指定模型- -f 指定语音文件
  1. > PS C:\Users\aiyolo\whisper.cpp> ./main.exe -m C:\Users\aiyolo\whisper.cpp\models\ggml-base.bin -f C:\Users\aiyolo\whisper.cpp\samples\jfk.wav
  2. whisper_init_from_file_no_state: loading model from 'C:\Users\aiyolo\whisper.cpp\models\ggml-base.bin'
  3. whisper_model_load: loading model
  4. whisper_model_load: n_vocab = 51865
  5. whisper_model_load: n_audio_ctx = 1500
  6. whisper_model_load: n_audio_state = 512
  7. whisper_model_load: n_audio_head = 8
  8. whisper_model_load: n_audio_layer = 6
  9. whisper_model_load: n_text_ctx = 448
  10. whisper_model_load: n_text_state = 512
  11. whisper_model_load: n_text_head = 8
  12. whisper_model_load: n_text_layer = 6
  13. whisper_model_load: n_mels = 80
  14. whisper_model_load: f16 = 1
  15. whisper_model_load: type = 2
  16. whisper_model_load: mem required = 215.00 MB (+ 6.00 MB per decoder)
  17. whisper_model_load: adding 1608 extra tokens
  18. whisper_model_load: model ctx = 140.60 MB
  19. whisper_model_load: model size = 140.54 MB
  20. whisper_init_state: kv self size = 5.25 MB
  21. whisper_init_state: kv cross size = 17.58 MB
  22. system_info: n_threads = 4 / 16 | AVX = 1 | AVX2 = 1 | AVX512 = 0 | FMA = 1 | NEON = 0 | ARM_FMA = 0 | F16C = 1 | FP16_VA = 0 | WASM_SIMD = 0 | BLAS = 0 | SSE3 = 1 | VSX = 0 |
  23. main: processing 'C:\Users\aiyolo\whisper.cpp\samples\jfk.wav' (176000 samples, 11.0 sec), 4 threads, 1 processors, lang = en, task = transcribe, timestamps = 1 ...
  24. [00:00:00.000 --> 00:00:07.600] And so my fellow Americans ask not what your country can do for you,
  25. [00:00:07.600 --> 00:00:10.600] ask what you can do for your country.
  26. whisper_print_timings: load time = 108.72 ms
  27. whisper_print_timings: fallbacks = 0 p / 0 h
  28. whisper_print_timings: mel time = 88.37 ms
  29. whisper_print_timings: sample time = 104.12 ms / 28 runs ( 3.72 ms per run)
  30. whisper_print_timings: encode time = 824.49 ms / 1 runs ( 824.49 ms per run)
  31. whisper_print_timings: decode time = 138.39 ms / 28 runs ( 4.94 ms per run)
  32. whisper_print_timings: total time = 1302.72 ms

可以看到识别效果很好。

识别中文

用系统的录音机录制了 OpenAI 的 Whisper 模型是一个很伟大的发明

由于

  1. whisper.cpp

目前只支持

  1. 16 khz

  1. wav

文件格式的语言文件,需要先使用

  1. ffmpeg

将语音文件转成所需的格式。

windows 平台可以使用

  1. choco

命令来安装

  1. FFmpeg

  1. > choco install ffmpeg

然后,将其转成需要的格式。

  1. > ffmpeg -i '.\录音 (2).wav' -ar 16000 -ac 1 -c:a pcm_s16le output.wav

接着使用 whisper 进行识别。

  1. ./main.exe -l auto -m C:\Users\aiyolo\whisper.cpp\models\ggml-small.bin -f "C:\Users\aiyolo\Documents\录音\output.wav" -osrt

上面面命令中:

  • -l 选项指定的语种,设置成 auto 会自动检测。
  • -osrt 将输出 srt 文件格式的文本。

打开文件看看最终的效果。
image.png

相比之下 base 模型的输出结果不尽人意,而 small 模型基本能达到日常的用途。
image.png

实时转录

再来试一下,实时语音转录功能。实时语音转录需要得到

  1. stream

文件。使用

  1. make stream

命令编译, 发现需要依赖 sdl 库。

  1. > make stream
  2. In file included from examples/common-sdl.cpp:1:
  3. examples/common-sdl.h:3:10: fatal error: SDL.h: No such file or directory
  4. 3 | #include <SDL.h>

由于我使用 mingw 编译的,我可以直接使用下面命令安装 sdl 依赖库。

  1. $ pacman -S mingw-w64-x86_64-SDL2

依赖库安装成功后,编译也顺利完成,得到

  1. stream.exe

文件。但是在 windows 执行下面命令,还不能达到实时转录的效果。

  1. ./stream -m ./models/ggml-base.en.bin -t 8 --step 500 --length 5000

本文转载自: https://blog.csdn.net/aiyolo/article/details/129674728
版权归原作者 aiyolo 所有, 如有侵权,请联系我们删除。

“Whisper.cpp 编译使用”的评论:

还没有评论