File size: 3,098 Bytes
3e70ffd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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()