import time
import datetime
from vosk import Model, KaldiRecognizer # оффлайн-распознавание от Vosk
import speech_recognition # распознавание пользовательской речи (Speech-To-Text)
import wave # создание и чтение аудиофайлов формата wav
import json # работа с json-файлами и json-строками
import os # работа с файлами
import subprocess
import requests
def assistent(*args: tuple):
with microphone:
value = ""
one = 'echo Алиса слушает | RHVoice-test -p anna'
subprocess.call(one, shell=True)
recognizer.adjust_for_ambient_noise(microphone, duration=2)
try:
audio = recognizer.listen(microphone, 6, 5)
with open("voices-my.wav", "wb") as file:
file.write(audio.get_wav_data())
except speech_recognition.WaitTimeoutError:
print("Повтори пожалуйста:\n")
one = 'echo Повтори пожалуйста | RHVoice-test -p anna'
subprocess.call(one, shell=True)
return
try:
if not os.path.exists("images/vosk-model-small-ru-0.15"):
print("Нет пакета Vosk")
exit(1)
audio_file = wave.open("voices-my.wav", "rb")
model = Model("images/vosk-model-small-ru-0.15")
offline = KaldiRecognizer(model, audio_file.getframerate())
data = audio_file.readframes(audio_file.getnframes())
if len(data) > 0:
if offline.AcceptWaveform(data):
value = offline.Result()
value = json.loads(value)
value = value["text"]
except:
error = 'echo не могу распознать речь| RHVoice-test -p anna'
subprocess.call(error, shell=True)
return value
if __name__ == "__main__":
recognizer = speech_recognition.Recognizer()
microphone = speech_recognition.Microphone()
while True:
voice_input = assistent()
os.remove("voices-my.wav")
print(voice_input)