Bedirhan commited on
Commit
3e70ffd
1 Parent(s): a1e75ef

Upload tryVosk.py

Browse files
Files changed (1) hide show
  1. tryVosk.py +79 -0
tryVosk.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from vosk import Model, KaldiRecognizer
2
+ import wave
3
+ import json
4
+ import os
5
+
6
+ class vosk():
7
+ def __init__(self, file_folder, outputResult, outputText, lang):
8
+ self.file_folder = file_folder
9
+ self.outputResult = outputResult
10
+ self.outputText = outputText
11
+ self.lang = lang
12
+
13
+ def control(self):
14
+ if os.path.exists(self.outputResult):
15
+ os.remove(self.outputResult)
16
+ if os.path.exists(self.outputText):
17
+ os.remove(self.outputText)
18
+
19
+ def useVosk(self, file_name):
20
+ wf = wave.open(file_name, "rb")
21
+ # initialize a str to hold results
22
+ results = ""
23
+ textResults = ""
24
+
25
+ # build the model and recognizer objects.
26
+ if lang == "Turkish" or lang == "Türkçe" or lang == "türkçe" or lang == "turkish":
27
+ model = Model("vosk-modelss/vosk-model-tr")
28
+
29
+ elif lang == "English" or lang == "İngilizce" or lang == "ingilizce" or lang == "english":
30
+ model = Model("vosk-modelss/vosk-model-en")
31
+ else:
32
+ print("Dil bulunamadı!------The language has not been found!")
33
+
34
+ recognizer = KaldiRecognizer(model, wf.getframerate())
35
+ recognizer.SetWords(True)
36
+
37
+ while True:
38
+ data = wf.readframes(4000)
39
+ if len(data) == 0:
40
+ break
41
+ if recognizer.AcceptWaveform(data):
42
+ recognizerResult = recognizer.Result()
43
+ results = results + recognizerResult
44
+ results_y = (json.dumps(results, ensure_ascii=False).encode('utf-8')).decode()
45
+ resultDict = json.loads(recognizerResult)
46
+
47
+ # process "final" result
48
+ results = results + recognizer.FinalResult()
49
+ results_y = (json.dumps(results, ensure_ascii=False).encode('utf-8')).decode()
50
+ for i in range(results_y.count('text')):
51
+ txtt = results_y.split("text")[i + 1].split("\\n")[0].split('"')[2].split("\\")[0]
52
+ textResults += txtt
53
+ textResults += " "
54
+
55
+ resultDict = json.loads(recognizer.FinalResult())
56
+ if len(textResults) == 0:
57
+ textResults += txtt
58
+ return results, textResults
59
+
60
+ def playWav(self):
61
+ self.control()
62
+ outputResultt = open(self.outputResult, 'w', encoding="utf-8")
63
+ outputTextt = open(self.outputText, 'w', encoding="utf-8")
64
+ for i in os.listdir(self.file_folder):
65
+ if i.endswith("wav"):
66
+ results, textResults = self.useVosk(self.file_folder + i)
67
+ # write results to a file
68
+ outputResultt.write("%s\n" % results)
69
+
70
+ # write text portion of results to a file
71
+ outputTextt.write("%s\n" % textResults)
72
+ print("Metin alımı tamamlandı!----------Text retrieval has been completed!")
73
+ outputTextt.close()
74
+ outputResultt.close()
75
+
76
+
77
+ file_fold, outputR, outputT = "Sounds/tr/", "texts/resulttr.json", "texts/texttr.txt"
78
+ lang = "turkish"
79
+ vosk(file_fold, outputR, outputT, lang).playWav()