08.
21 1:01 PM
import speech_recognition as sr
import pyttsx3
import serial
import [Link].list_ports
import threading
import time
from fuzzywuzzy import fuzz
import pystray
from PIL import Image, ImageDraw
import queue
# ------------------- Voice Engine -------------------
engine = [Link]()
[Link]('rate', 170)
speech_queue = [Link]()
def speak_worker():
while True:
text = speech_queue.get()
if text == "EXIT":
break
[Link](text)
[Link]()
def speak(text):
speech_queue.put(text)
# Start the speak thread
[Link](target=speak_worker, daemon=True).start()
# ------------------- Find Arduino COM -------------------
def find_arduino():
ports = list([Link].list_ports.comports())
for p in ports:
if 'Arduino' in [Link] or 'CH340' in [Link]:
return [Link]
return None
arduino_port = find_arduino()
arduino = None
if arduino_port:
arduino = [Link](arduino_port, 9600, timeout=1)
[Link](2)
speak("Jarvis is online and ready.")
else:
speak("No Arduino detected.")
# ------------------- Commands -------------------
commands = {
"light on": ["turn on light", "switch on light", "activate light", "start
light"],
"light off": ["turn off light", "switch off light", "deactivate light", "stop
light"],
"fan on": ["turn on fan", "switch on fan", "activate fan"],
"fan off": ["turn off fan", "switch off fan", "stop fan"],
"tv on": ["turn on tv", "switch on tv", "activate tv"],
"tv off": ["turn off tv", "switch off tv", "stop tv"],
"charger on": ["turn on charger", "switch on charger"],
"charger off": ["turn off charger", "switch off charger"]
}
def match_command(phrase):
phrase = [Link]()
for key, variations in [Link]():
if fuzz.partial_ratio(phrase, key) > 80:
return key
for v in variations:
if fuzz.partial_ratio(phrase, v) > 80:
return key
return None
# ------------------- Voice Listener -------------------
recognizer = [Link]()
mic = [Link]()
with mic as source:
recognizer.adjust_for_ambient_noise(source)
def callback(recognizer, audio):
try:
text = recognizer.recognize_google(audio)
print(f"Heard: {text}")
if "jarvis" in [Link]():
command = match_command(text)
if command:
if arduino:
[Link]([Link]())
response = f"Turning {[Link]('_', ' ')}"
print(response)
speak(response)
except [Link]:
pass
except Exception as e:
print("Error:", e)
stop_listening = recognizer.listen_in_background(mic, callback)
# ------------------- Tray Icon -------------------
def create_image():
image = [Link]('RGB', (64, 64), color=(0, 0, 0))
draw = [Link](image)
[Link]([16, 16, 48, 48], fill=(0, 255, 0))
return image
def on_quit(icon, item):
speak("EXIT")
[Link]()
exit()
icon = [Link]("Jarvis", create_image(), "Jarvis", menu=[Link](
[Link]("Quit", on_quit)
))
[Link]()