from vosk import Model, KaldiRecognizer import wave import json import os class vosk(): def __init__(self, file_folder, outputResult, outputText, lang): self.file_folder = file_folder self.outputResult = outputResult self.outputText = outputText self.lang = lang def control(self): if os.path.exists(self.outputResult): os.remove(self.outputResult) if os.path.exists(self.outputText): os.remove(self.outputText) def useVosk(self, file_name): wf = wave.open(file_name, "rb") # initialize a str to hold results results = "" textResults = "" # build the model and recognizer objects. if lang == "Turkish" or lang == "Türkçe" or lang == "türkçe" or lang == "turkish": model = Model("vosk-modelss/vosk-model-tr") elif lang == "English" or lang == "İngilizce" or lang == "ingilizce" or lang == "english": model = Model("vosk-modelss/vosk-model-en") else: print("Dil bulunamadı!------The language has not been found!") recognizer = KaldiRecognizer(model, wf.getframerate()) recognizer.SetWords(True) while True: data = wf.readframes(4000) if len(data) == 0: break if recognizer.AcceptWaveform(data): recognizerResult = recognizer.Result() results = results + recognizerResult results_y = (json.dumps(results, ensure_ascii=False).encode('utf-8')).decode() resultDict = json.loads(recognizerResult) # process "final" result results = results + recognizer.FinalResult() results_y = (json.dumps(results, ensure_ascii=False).encode('utf-8')).decode() for i in range(results_y.count('text')): txtt = results_y.split("text")[i + 1].split("\\n")[0].split('"')[2].split("\\")[0] textResults += txtt textResults += " " resultDict = json.loads(recognizer.FinalResult()) if len(textResults) == 0: textResults += txtt return results, textResults def playWav(self): self.control() outputResultt = open(self.outputResult, 'w', encoding="utf-8") outputTextt = open(self.outputText, 'w', encoding="utf-8") for i in os.listdir(self.file_folder): if i.endswith("wav"): results, textResults = self.useVosk(self.file_folder + i) # write results to a file outputResultt.write("%s\n" % results) # write text portion of results to a file outputTextt.write("%s\n" % textResults) print("Metin alımı tamamlandı!----------Text retrieval has been completed!") outputTextt.close() outputResultt.close() file_fold, outputR, outputT = "Sounds/tr/", "texts/resulttr.json", "texts/texttr.txt" lang = "turkish" vosk(file_fold, outputR, outputT, lang).playWav()