-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
57 lines (45 loc) · 1.76 KB
/
main.py
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
import json
import queue
from vosk import Model, KaldiRecognizer
from vosk import GpuInit, GpuThreadInit
import sounddevice as sd
# Получение информации о микрофоне
device_info = sd.query_devices(sd.default.device[0], 'input')
samplerate = int(device_info['default_samplerate'])
GpuInit()
GpuThreadInit()
# Подготовка модели
model = Model("model/vosk-model-small-ru-0.22")
# model = Model("model/vosk-model-ru-0.42")
recognizer = KaldiRecognizer(model, samplerate)
recognizer.SetWords(False)
q = queue.Queue()
# Чтение корпуса слов
with open("data/obscene_corpus.txt", 'r') as file:
word_list = file.read().splitlines()
def recordCallback(indata, frames, time, status):
q.put(bytes(indata))
temp_partial_result = ""
with sd.RawInputStream(
dtype='int16', samplerate=samplerate,
blocksize=2000, channels=1, callback=recordCallback):
print("Start Recognized")
while True:
data = q.get()
if recognizer.AcceptWaveform(data):
# Вывод полного результата
text = json.loads(recognizer.Result())["text"]
if len(text) == 0:
continue
print(f"Text: {text}")
text_arr = text.split()
check = any(text_arr.count(item) for item in word_list)
if check:
print("Obscene checked!")
print("\n")
else:
# Вывод промежуточного результата
partial_result = json.loads(recognizer.PartialResult())["partial"]
if len(partial_result) != 0 and temp_partial_result != partial_result:
print(f"Partial result: {partial_result}")
temp_partial_result = partial_result