import wave
import sys
import json
from vosk import Model, KaldiRecognizer, SetLogLevel
# You can set log level to -1 to disable debug messages
SetLogLevel(-1)
wf = wave.open(sys.argv[1], "rb")
if wf.getnchannels() != 1 or wf.getsampwidth() != 2 or wf.getcomptype() != "NONE":
print("Audio file must be WAV format mono PCM.")
sys.exit(1)
# model = Model(lang="en-us")
# You can also init model by name or with a folder path
# model = Model(model_name="vosk-model-en-us-0.21")
# 设置模型所在路径,刚刚4.1中解压出来的路径 《《《《
# model = Model("model")
model = Model("../Downloads/vosk-model-small-cn-0.22")
rec = KaldiRecognizer(model, wf.getframerate())
rec.SetWords(True)
# rec.SetPartialWords(True) # 注释这行 《《《《
str_ret = ""
while True:
data = wf.readframes(4000)
if len(data) == 0:
break
if rec.AcceptWaveform(data):
result = rec.Result()
# print(result)
result = json.loads(result)
if 'text' in result:
str_ret += result['text'] + ' '
# else:
# print(rec.PartialResult())
result = json.loads(rec.FinalResult())
if 'text' in result:
str_ret += result['text']
print(str_ret)
wf.close()
模型分享:
通过网盘分享的文件:vosk-model-small-cn-0.22.zip
链接: https://pan.baidu.com/s/1FEH1xwDucdC3cEZSAyDOwQ?pwd=k8p5 提取码: k8p5
通过网盘分享的文件:vosk-model-cn-0.22.zip
链接: https://pan.baidu.com/s/1dISCahVsWppnS-bbvyLWEA?pwd=ymgp 提取码: ymgp
通过网盘分享的文件:vosk-model-en-us-0.22.zip
链接: https://pan.baidu.com/s/1z-d1A8wHvBs7m2dpMjYjXQ?pwd=8hg9 提取码: 8hg9
通过网盘分享的文件:vosk-model-ja-0.22.zip
链接: https://pan.baidu.com/s/1eQCEzNvdjnbyLILdu56RDw?pwd=8nhv 提取码: 8nhv
模型下载自https://alphacephei.com/vosk/models
创建于2412261646,修改于2412261646
标签:wf,0.22,vosk,result,语音,rec,model,识别 From: https://www.cnblogs.com/tellw/p/18633496