File size: 1,909 Bytes
8f71f0f |
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 |
import speech_recognition as sr
import pyttsx3
import pickle
from model import MaestroAssistant
class VoiceAssistant:
def __init__(self):
self.recognizer = sr.Recognizer()
self.engine = pyttsx3.init()
try:
with open('maestro_model.pkl', 'rb') as f:
self.assistant = pickle.load(f)
except:
self.assistant = MaestroAssistant()
voices = self.engine.getProperty('voices')
try:
self.engine.setProperty('voice', voices[0].id)
except:
pass
self.engine.setProperty('rate', 150)
def listen(self):
with sr.Microphone() as source:
print("Слушаю...")
self.recognizer.adjust_for_ambient_noise(source)
audio = self.recognizer.listen(source)
try:
text = self.recognizer.recognize_google(audio, language='ru-RU').lower()
print(f"Распознано: {text}")
return text
except Exception as e:
print(f"Ошибка распознавания: {e}")
return ""
def speak(self, text):
print(f"Маэстро: {text}")
self.engine.say(text)
self.engine.runAndWait()
def run(self):
print("Ассистент Маэстро активирован! Говорите 'Эй Маэстро' для начала.")
while True:
text = self.listen()
if text.startswith(self.assistant.wake_word):
response = self.assistant.handle_command(text)
if response:
self.speak(response)
else:
self.speak("Не понял команду")
if __name__ == "__main__":
assistant = VoiceAssistant()
assistant.run() |