-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
runva_speechrecognition.py
68 lines (50 loc) · 2.17 KB
/
runva_speechrecognition.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
58
59
60
61
62
63
64
65
66
67
68
# you need to install:
# pip install PyAudio
# pip install SpeechRecognition
# if you have problems with PyAudio install, check this:
# https://github.com/EnjiRouz/Voice-Assistant-App/blob/master/README.md
import speech_recognition
import traceback
from vacore import VACore
# most from @EnjiRouz code: https://habr.com/ru/post/529590/
def record_and_recognize_audio(*args: tuple):
"""
Запись и распознавание аудио
"""
with microphone:
recognized_data = ""
try:
#print("Listening...")
audio = recognizer.listen(microphone, 5, 5)
except speech_recognition.WaitTimeoutError:
print("Can you check if your microphone is on, please?")
return
# использование online-распознавания через Google
try:
#print("Started recognition...")
recognized_data = recognizer.recognize_google(audio, language="ru").lower()
except speech_recognition.UnknownValueError:
pass
# в случае проблем с доступом в Интернет происходит выброс ошибки
except speech_recognition.RequestError:
print("Check your Internet Connection, please")
return recognized_data
# ------------------- vosk ------------------
if __name__ == "__main__":
# инициализация инструментов распознавания и ввода речи
recognizer = speech_recognition.Recognizer()
microphone = speech_recognition.Microphone()
with microphone:
# регулирование уровня окружающего шума
recognizer.adjust_for_ambient_noise(microphone, duration=2)
# initing core
core = VACore()
#core.init_plugin("core")
#core.init_plugins(["core"])
core.init_with_plugins()
while True:
# старт записи речи с последующим выводом распознанной речи
voice_input_str = record_and_recognize_audio()
if voice_input_str != "":
core.run_input_str(voice_input_str)
core._update_timers()