-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommunication.py
More file actions
45 lines (37 loc) · 1.9 KB
/
communication.py
File metadata and controls
45 lines (37 loc) · 1.9 KB
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
import pyttsx3
import speech_recognition as sr
from PyQt6.QtWidgets import QLabel
from PyQt6.QtCore import QTimer
class Communication:
def __init__(self, ui):
"""Initialize the communication system."""
self.engine = pyttsx3.init()
self.recognizer = sr.Recognizer()
self.ui = ui # Reference to UI for updating labels
def speak(self, text):
"""Speaks and updates UI with text output."""
self.ui.status_label.setText(f"Jarvis: {text}") # Update UI status
self.engine.say(text)
self.engine.runAndWait()
def start_communication(self):
"""Start Eco Mode for interactive communication."""
self.speak("Eco mode activated. I am listening.")
while True:
try:
with sr.Microphone() as source:
self.ui.status_label.setText("Listening in Eco Mode...") # Update UI
print("Listening in Eco Mode...")
audio = self.recognizer.listen(source)
command = self.recognizer.recognize_google(audio)
self.ui.result_label.setText(f"You said: {command}") # Update UI with input
print("You said:", command)
if "exit eco mode" in command.lower():
self.speak("Exiting eco mode.")
self.ui.status_label.setText("Returning to normal mode...")
break # Exit loop
except sr.UnknownValueError:
self.ui.result_label.setText("Sorry, I didn't catch that.")
self.speak("Sorry, I didn't catch that.")
except sr.RequestError:
self.ui.result_label.setText("Speech recognition service unavailable.")
self.speak("Speech recognition service unavailable.")