import json
from vosk import Model, KaldiRecognizer, SetLogLevel
import os
import wave
def main() -> None:
wf = wave.open('decoder-test.wav', 'rb')
if wf.getnchannels() != 1 or wf.getsampwidth() != 2 or wf.getcomptype() != 'NONE':
print('Audio file must be WAY format mono PCM')
exit(1)
model = Model('vosk-model-ru-0.10')
rec = KaldiRecognizer(model, wf.getframerate())
with open('voice_to_text.txt', 'w', encoding='utf-8') as file:
while True:
data = wf.readframes(4000)
if len(data) == 0:
break
if rec.AcceptWaveform(data):
rec_text = json.loads(rec.Result())
print(rec_text.get('text'))
file.writelines(f'{rec_text.get("text")}\n')
else:
pass
if name == 'main':
main()
Этот код из аудио файла достаёт текст и записывает в файл. Модель находится в папке проекта, но выдаёт ошибку.
File "D:\Desktop\mp3\main.py", line 14, in main
model = Model('vosk-model-ru-0.10')
Exception: Failed to create a model
Вот логи:
LOG (VoskAPI:ReadDataFiles():model.cc:213) Decoding params beam=13 max-active=7000 lattice-beam=6
LOG (VoskAPI:ReadDataFiles():model.cc:216) Silence phones 1:2:3:4:5:6:7:8:9:10
LOG (VoskAPI:RemoveOrphanNodes():nnet-nnet.cc:948) Removed 0 orphan nodes.
LOG (VoskAPI:RemoveOrphanComponents():nnet-nnet.cc:847) Removing 0 orphan components.
LOG (VoskAPI:ReadDataFiles():model.cc:248) Loading i-vector extractor from vosk-model-ru-0.10/ivector/final.ie
LOG (VoskAPI:ComputeDerivedVars():ivector-extractor.cc:183) Computing derived variables for iVector extractor
LOG (VoskAPI:ComputeDerivedVars():ivector-extractor.cc:204) Done.
LOG (VoskAPI:ReadDataFiles():model.cc:279) Loading HCLG from vosk-model-ru-0.10/graph/HCLG.fst
LOG (VoskAPI:ReadDataFiles():model.cc:294) Loading words from vosk-model-ru-0.10/graph/words.txt
LOG (VoskAPI:ReadDataFiles():model.cc:303) Loading winfo vosk-model-ru-0.10/graph/phones/word_boundary.int
LOG (VoskAPI:ReadDataFiles():model.cc:310) Loading subtract G.fst model from vosk-model-ru-0.10/rescore/G.fst
LOG (VoskAPI:ReadDataFiles():model.cc:312) Loading CARPA model from vosk-model-ru-0.10/rescore/G.carpa