[go: up one dir, main page]

0% found this document useful (0 votes)
17 views13 pages

Parts of Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views13 pages

Parts of Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Mod 1

# Initialize speech engine, recognizer, and microphone


engine = pyttsx3.init()
recognizer = sr.Recognizer()
mic = sr.Microphone()
Mod 2

# Variables for profile handling


profiles = {}
current_user = None
profile_file = "profiles.db"
alpha = 1.5 # Sensitivity for mouse movement
voice_commands = [
"create profile", "switch profile", "open word", "save document",
"close word",
"open excel", "create sheet", "close excel", "play youtube", "pause
youtube",
"next track spotify", "open file explorer", "new folder", "open
folder", "click"
]

Mod 3-
app_stack = []
command_queue = Queue()

Mod 4
# Initialize Mediapipe and OpenCV for face tracking
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(min_detection_confidence=0.5,
min_tracking_confidence=0.5)
cap = cv2.VideoCapture(0)

Mod 5-
# Load profiles from a DBMS file
def load_profiles():
global profiles
try:
with open(profile_file, "r") as file:
profiles = json.load(file)
print("Profiles loaded successfully.")
except FileNotFoundError:
profiles = {}
print("No profile file found, creating new profiles.")
Mod 6
# Save profiles to a dbms file
def save_profiles():
with open(profile_file, "w") as file:
json.dump(profiles, file, indent=4)
print(f"Profiles saved: {profiles}")
Mod 7

# Create a new profile


def create_profile(user_name):
global current_user
profiles[user_name] = {"name": user_name, "preferences": {},
"sensitivity": alpha}
current_user = user_name
save_profiles()
say(f"Profile for {user_name} created.")
Mod 8
# Switch to an existing profile
def switch_profile(user_name):
global current_user
if user_name in profiles:
current_user = user_name
say(f"Switched to {user_name}'s profile.")
else:
say(f"No profile found for {user_name}. Creating a new one.")
create_profile(user_name)

Mod 9
# Function to open applications
def open_application(app_name, command):
say(f"Opening {app_name}")
os.system(command)
def close_last_application():
if app_stack:
last_app = app_stack.pop() # Pop the last opened application
say(f"Closing {last_app}.")
pyautogui.hotkey("alt", "f4") # Generic close command
else:
say("No applications to close.")
Mod 10
def add_command_to_queue(command):
command_queue.put(command)
# Function to process the queue commands
def process_command_queue():
while not command_queue.empty():
command = command_queue.get()
recognize_user_commands(command)
command_queue.task_done()
Mod 11
def binary_search(arr, x):
left, right = 0, len(arr) - 1
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == x:
return mid
elif arr[mid] < x:
left = mid + 1
else:
right = mid - 1
return -1
voice_commands.sort()
command_index = binary_search(voice_commands, "create profile")
Mod 12
### Word Commands ###
def word_create_document():
open_application("Word", "start winword")
time.sleep(2)
pyautogui.hotkey('ctrl', 'n') # Create new document
say("Creating a new document in Word.")
mod 13
def word_close():
pyautogui.hotkey('alt', 'f4') # Close Word
say("Closing Word.")
Mod 14
def word_open_document():
say("Please say the name of the document to open.")
file_name = get_voice_input()
if file_name:
pyautogui.hotkey('ctrl', 'o') # Open file dialog
pyautogui.write(file_name)
pyautogui.press('enter')
say(f"Opening document {file_name}.")
Mod 15
def start_voice_typing_in_word():
say("Starting voice typing in Word.")
while True:
text = get_voice_input()
if text == "stop typing":
say("Stopping voice typing in Word.")
break
pyautogui.write(text)

Mod 16
### Excel Commands ###
def excel_create_sheet():
open_application("Excel", "start excel")
time.sleep(2)
pyautogui.hotkey('ctrl', 'n') # Create new spreadsheet
say("Creating a new sheet in Excel.")
Mod 17
def excel_save_sheet():
pyautogui.hotkey('ctrl', 's') # Save the sheet
say("Saving the sheet.")
mod 18
def excel_close():
pyautogui.hotkey('alt', 'f4') # Close Excel
say("Closing Excel.")
mod 19
def excel_open_sheet():
say("Please say the name of the spreadsheet to open.")
file_name = get_voice_input()
if file_name:
pyautogui.hotkey('ctrl', 'o') # Open file dialog
pyautogui.write(file_name)
pyautogui.press('enter')
say(f"Opening spreadsheet {file_name}.")
Mod 20
def start_voice_typing_in_excel():
say("Starting voice typing in Excel.")
while True:
text = get_voice_input()
if text == "stop typing":
say("Stopping voice typing in Excel.")
break
pyautogui.write(text)

Mod 21
### PowerPoint Commands ###
def powerpoint_create_presentation():
open_application("PowerPoint", "start powerpoint")
time.sleep(2)
pyautogui.hotkey('ctrl', 'n') # Create new presentation
say("Creating a new presentation in PowerPoint.")
mod 22
def powerpoint_save_presentation():
pyautogui.hotkey('ctrl', 's') # Save the presentation
say("Saving the presentation.")
mod 23
def powerpoint_close():
pyautogui.hotkey('alt', 'f4') # Close PowerPoint
say("Closing PowerPoint.")
mod 24
def powerpoint_open_presentation():
say("Please say the name of the presentation to open.")
file_name = get_voice_input()
if file_name:
pyautogui.hotkey('ctrl', 'o') # Open file dialog
pyautogui.write(file_name)
pyautogui.press('enter')
say(f"Opening presentation {file_name}.")
Mod 25
def start_voice_typing_in_powerpoint():
say("Starting voice typing in PowerPoint.")
while True:
text = get_voice_input()
if text == "stop typing":
say("Stopping voice typing in PowerPoint.")
break
pyautogui.write(text)

Mod 26
def switch_to_next_tab():
pyautogui.hotkey('ctrl', 'tab')
say("Switching to the next tab.")
def switch_to_previous_tab():
pyautogui.hotkey('ctrl', 'shift', 'tab')
say("Switching to the previous tab.")

mod 27
### YouTube Commands ###
def youtube_play_pause():
pyautogui.press('k') # Play/pause video
say("Toggling play/pause on YouTube.")

def youtube_volume_up():
pyautogui.press('volumeup')
say("Increasing YouTube volume.")

def youtube_volume_down():
pyautogui.press('volumedown')
say("Decreasing YouTube volume.")

def youtube_next_video():
pyautogui.press('shift', 'n') # Next video
say("Skipping to the next video.")

def youtube_mute_unmute():
pyautogui.press('m') # Mute/unmute
say("Toggling mute/unmute on YouTube.")
Mod28
def spotify_play_pause():
pyautogui.press('playpause') # Play/pause Spotify
say("Toggling play/pause on Spotify.")
mod 29
def spotify_next_track():
pyautogui.press('nexttrack') # Next track
say("Skipping to the next track.")
mod 30
def spotify_previous_track():
pyautogui.press('prevtrack') # Previous track
say("Going back to the previous track.")
mod 31
def spotify_volume_up():
pyautogui.press('volumeup') # Volume up
say("Increasing Spotify volume.")

mod 32
def spotify_mute_unmute():
pyautogui.press('volumemute') # Mute/unmute
say("Toggling mute/unmute on Spotify.")

mod 33
### File Explorer Commands ###
def open_file_explorer():
open_application("File Explorer", "explorer")
say("Opening File Explorer.")

def create_new_folder():
pyautogui.hotkey('ctrl', 'shift', 'n') # Create a new folder
say("Creating a new folder.")

def open_folder():
say("Please say the name of the folder to open.")
folder_name = get_voice_input()
if folder_name:
pyautogui.hotkey('ctrl', 'l') # Focus on address bar
pyautogui.write(folder_name)
pyautogui.press('enter')
say(f"Opening folder {folder_name}.")

def open_file():
say("Please say the name of the file to open.")
file_name = get_voice_input()
if file_name:
pyautogui.write(file_name)
pyautogui.press('enter')
say(f"Opening file {file_name}.")
Mod 34
def minimize_window():
"""Minimizes the active window."""
pyautogui.hotkey("win", "down") # Minimizes the current window

def maximize_window():
"""Maximizes the active window."""
pyautogui.hotkey("win", "up") # Maximizes the current window

def click_at_cursor():
"""Clicks at the current cursor position."""
pyautogui.click()

mod 35
# Voice command for taking file/folder name
def get_voice_input():
with mic as source:
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
try:
command = recognizer.recognize_google(audio).lower()
return command
except sr.UnknownValueError:
say("I didn't catch that. Please try again.")
except sr.RequestError as e:
say("Sorry, I am having trouble connecting.")
return None

Mod 36
# Function to track head movements and control the mouse
def track_head_for_mouse():
global prev_x, prev_y, should_click, activate_keyboard, running
while cap.isOpened() and running:
success, frame = cap.read()
if not success:
break

frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)


results = face_mesh.process(frame_rgb)

if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
# Get coordinates for the nose tip (use landmark 1 as an
example)
nose_tip = face_landmarks.landmark[1]
x = int(nose_tip.x * screen_width)
y = int(nose_tip.y * screen_height)

# Smooth out movements by averaging previous position with


new position
new_x = prev_x + (x - prev_x) / alpha
new_y = prev_y + (y - prev_y) / alpha

# Move the mouse


pyautogui.moveTo(new_x, new_y)

# Store the new position


prev_x, prev_y = new_x, new_y
# Check for click activation
if should_click:
pyautogui.click()
should_click = False

# Break the loop if running is set to False


if not running:
break

cap.release()

Mod 37
# Recognize and process voice commands
def recognize_user_commands():
global should_click, activate_keyboard, running
with mic as source:
print("Adjusting for ambient noise...")
recognizer.adjust_for_ambient_noise(source)
print("Listening for command...")
audio = recognizer.listen(source)

try:
command = recognizer.recognize_google(audio).lower()
print(f"Recognized command: {command}")

mod 38
if "word" in command:
if "create" in command:
word_create_document()
elif "save" in command:
word_save_document()
elif "close" in command:
word_close()
elif "open" in command:
word_open_document()
elif "start typing in word" in command:
start_voice_typing_in_word()
mod 39
# Excel-related commands
elif "excel" in command:
if "create" in command:
excel_create_sheet()
elif "save" in command:
excel_save_sheet()
elif "close" in command:
excel_close()
elif "open" in command:
excel_open_sheet()
elif "start typing in excel" in command:
start_voice_typing_in_word()
mod 40
# PowerPoint-related commands
elif "powerpoint" in command:
if "create" in command:
powerpoint_create_presentation()
elif "save" in command:
powerpoint_save_presentation()
elif "close" in command:
powerpoint_close()
elif "open" in command:
powerpoint_open_presentation()
elif "start typing in powerpoint" in command:
start_voice_typing_in_word()
mod 41
# YouTube commands
elif "youtube" in command:
if "play" in command or "pause" in command:
youtube_play_pause()
elif "volume up" in command:
youtube_volume_up()
elif "volume down" in command:
youtube_volume_down()
elif "next" in command:
youtube_next_video()
elif "mute" in command or "unmute" in command:
youtube_mute_unmute()
mod 42
# Spotify commands
elif "spotify" in command:
if "play" in command or "pause" in command:
spotify_play_pause()
elif "next" in command:
spotify_next_track()
elif "previous" in command:
spotify_previous_track()
elif "volume up" in command:
spotify_volume_up()
elif "volume down" in command:
spotify_volume_down()
elif "mute" in command or "unmute" in command:
spotify_mute_unmute()
mod 43
# File Explorer commands
elif "file explorer" in command:
open_file_explorer()
elif "new folder" in command:
create_new_folder()
elif "open folder" in command:
open_folder()
elif "open file" in command:
open_file()

mod 44
say(f"Executed command: {command}")

except sr.UnknownValueError:
say("I did not understand that. Could you please repeat?")
except sr.RequestError as e:
say("There was an issue with the recognition service.")
Mod 45
# Start a thread for face tracking
face_thread = threading.Thread(target=track_head_for_mouse)
face_thread.daemon = True
face_thread.start()

# Voice command loop


while running:
recognize_user_commands()

cap.release()
Front end
Mod 46
class VoiceAssistantUI(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
self.engine = pyttsx3.init()
self.capture = cv2.VideoCapture(0)
self.profile_name = "" # Stores the current profile name
mod 47
# Start the profile selection process
threading.Thread(target=self.askForProfile).start()
threading.Thread(target=self.continuousHeadTracking).start() #

Start head tracking thread

Mod 48
def initUI(self):
# Setup main window properties
self.setWindowTitle("Voice Assistant")
self.setGeometry(50, 50, 600, 900) # Increased size for better
readability

# Main container for stacked views (Profile selection and Main UI)
self.stack = QStackedWidget(self)
self.setCentralWidget(self.stack)

mod 49
# Profile selection view
self.profileSelectionView = QWidget()
profile_layout = QVBoxLayout(self.profileSelectionView)
self.profile_label = QLabel("Please say your profile name", self)
self.profile_label.setAlignment(Qt.AlignCenter)
profile_layout.addWidget(self.profile_label)
self.stack.addWidget(self.profileSelectionView) # Index 0

Mod 50
# Profile and Mouse Sensitivity Section
profile_info_layout = QHBoxLayout()
self.profile_name_label = QLabel("Profile: ", self)
self.mouse_sensitivity_label = QLabel("Mouse Sensitivity:", self)

Mod 51
# Mouse sensitivity slider
self.mouse_sensitivity_slider = QSlider(Qt.Horizontal)
self.mouse_sensitivity_slider.setMinimum(1)
self.mouse_sensitivity_slider.setMaximum(10)
self.mouse_sensitivity_slider.setValue(5) # Default sensitivity
profile_info_layout.addWidget(self.profile_name_label)
profile_info_layout.addWidget(self.mouse_sensitivity_label)
profile_info_layout.addWidget(self.mouse_sensitivity_slider)
main_layout.addLayout(profile_info_layout)
Mod 52
# Webcam Display
self.webcam_label = QLabel("Webcam Feed", self)
self.webcam_label.setFixedSize(580, 300) # Increased size for better
visibility
main_layout.addWidget(self.webcam_label)

Mod 53
# Command Display
self.command_label = QLabel("Status: Waiting for command...", self)
self.command_label.setAlignment(Qt.AlignCenter)
main_layout.addWidget(self.command_label)

Mod 54
# Add main UI to stack
self.stack.addWidget(self.mainUIView) # Index 1

# Timer for webcam feed update


self.timer = QTimer()
self.timer.timeout.connect(self.updateWebcamFeed)
self.timer.start(30) # Update every 30ms

Mod 55
def continuousHeadTracking(self):
"""Continuously tracks head movement for mouse control."""
main.track_head_for_mouse()

mod 56
recognizer = sr.Recognizer()
with sr.Microphone() as source:
audio = recognizer.listen(source)
try:
profile = recognizer.recognize_google(audio)
self.profile_name = profile
self.profile_name_label.setText(f"Profile: {profile}")
self.speak(f"Welcome, {profile}")
Mod 57

# Switch to main UI view after profile is confirmed


self.switchToMainUI()
mod 58
# Start continuous listening in main UI
threading.Thread(target=self.listenContinuously).start()
except sr.UnknownValueError:
self.profile_label.setText("Could not understand profile name")
self.speak("Sorry, I didn't catch that.")
except Exception as e:
self.profile_label.setText(f"Error: {e}")

Mod 59
def switchToMainUI(self):
"""Switch to the main UI after profile is set."""
self.stack.setCurrentIndex(1) # Switch to the main UI view

mod 60
def listenContinuously(self):
"""Continuously listens for voice commands."""
recognizer = sr.Recognizer()
while True:
with sr.Microphone() as source:
self.mic_label.setText("🎤 Listening...")
audio = recognizer.listen(source)
try:
command = recognizer.recognize_google(audio).lower()
self.command_label.setText(f"Command: {command}")
self.command_list.addItem(command) # Add command to the
list
self.speak(f"Executing {command}")
self.executeCommand(command)
except sr.UnknownValueError:
self.command_label.setText("Could not understand audio")
self.speak("Sorry, I didn't catch that.")
except Exception as e:
self.command_label.setText(f"Error: {e}")

mod 61
def executeCommand(self, command):
# Profile Management
if "create profile" in command:
user_name = self.get_voice_input("Please say the profile name.")
main.create_profile(user_name)
elif "switch profile" in command:
user_name = self.get_voice_input("Please say the profile name.")
main.switch_profile(user_name)
elif "load profiles" in command:
main.load_profiles()
elif "save profiles" in command:
main.save_profiles()
main.open_application("Browser", "start chrome")
elif "switch to next tab" in command:
self.command_label.setText("Switching tabs...")
main.switch_to_next_tab()
elif "switch to previous tab" in command:
self.command_label.setText("Switching tabs...")
main.switch_to_previous_tab()
if "minimize window" in command:
self.command_label.setText("Minimizing Window...")
main.minimize_window()
elif "maximize window" in command:
self.command_label.setText("Maximizing Window...")
main.maximize_window()
if "tap" in command:
self.command_label.setText("Clicking at cursor position...")
main.click_at_cursor()
mod 62
# Microsoft Word
elif "open word" in command:
self.command_label.setText("Opening word...")
main.open_application("word 2007", "word 2007")
elif "create word document" in command:
main.word_create_document()
elif "save word document" in command:
main.word_save_document()
elif "close word" in command:
main.word_close()
elif "open word document" in command:
main.word_open_document()
elif "start voice typing in word" in command:
main.start_voice_typing_in_word()
mod 63
# Microsoft Excel
elif "open excel" in command:
self.command_label.setText("Opening excel...")
main.open_application("excel 2007", "excel 2007")
elif "create excel sheet" in command:
main.excel_create_sheet()
elif "save excel sheet" in command:
main.excel_save_sheet()
elif "close excel" in command:
main.excel_close()
elif "open excel sheet" in command:
main.excel_open_sheet()
elif "start voice typing in excel" in command:
main.start_voice_typing_in_excel()
mod 64
# Microsoft PowerPoint
elif "open powerpoint" in command:
self.command_label.setText("Opening powerpoint...")
main.open_application("powerpoint 2007", "powerpoint 2007")
elif "create powerpoint presentation" in command:
main.powerpoint_create_presentation()
elif "save powerpoint presentation" in command:
main.powerpoint_save_presentation()
elif "close powerpoint" in command:
main.powerpoint_close()
elif "open powerpoint presentation" in command:
main.powerpoint_open_presentation()
elif "start voice typing in powerpoint" in command:
main.start_voice_typing_in_powerpoint()
mod 65
# YouTube
elif "open youtube" in command:
self.command_label.setText("Opening youtube...")
main.open_application("youtube", "youtube")
elif "play youtube" in command or "pause youtube" in command:
main.youtube_play_pause()
elif "youtube volume up" in command:
main.youtube_volume_up()
elif "youtube volume down" in command:
main.youtube_volume_down()
elif "next youtube video" in command:
main.youtube_next_video()
elif "mute youtube" in command or "unmute youtube" in command:
main.youtube_mute_unmute()
mod 66
# Spotify
elif "open spotify" in command:
self.command_label.setText("Opening Spotify...")
main.open_application("Spotify", "spotify")
elif "play spotify" in command or "pause spotify" in command:
main.spotify_play_pause()
elif "next spotify track" in command:
main.spotify_next_track()
elif "previous spotify track" in command:
main.spotify_previous_track()
elif "spotify volume up" in command:
main.spotify_volume_up()
elif "spotify volume down" in command:
main.spotify_volume_down()
elif "mute spotify" in command or "unmute spotify" in command:
main.spotify_mute_unmute()
mod 67
# File Explorer
elif "open file explorer" in command:
main.open_file_explorer()
elif "create new folder" in command:
main.create_new_folder()
elif "open folder" in command:
main.open_folder()
elif "open file" in command:
main.open_file()
mod 68
# tabs Navigation
elif "switch to next tab" in command:
main.switch_to_next_tab()
elif "switch to previous tab" in command:
main.switch_to_previous_tab()
mod 69

else:
self.command_label.setText("Command not recognized")
self.command_list.addItem("Command not recognized")

mod 70
def speak(self, text):
self.engine.say(text)
self.engine.runAndWait()

mod 71
def updateWebcamFeed(self):
"""Update the webcam feed on the UI."""
ret, frame = self.capture.read()
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
h, w, ch = frame.shape
bytes_per_line = ch * w
convert_to_qt_format = QImage(frame.data, w, h, bytes_per_line,
QImage.Format_RGB888)

self.webcam_label.setPixmap(QPixmap.fromImage(convert_to_qt_format))

You might also like