-
Notifications
You must be signed in to change notification settings - Fork 2
/
assistant.py
143 lines (113 loc) · 3.5 KB
/
assistant.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import win32.win32gui as win32gui
import win32.lib.win32con as win32con
global window
window = win32gui.GetForegroundWindow()
import recognizer
import threading
import time
import os
from pystray import Menu, MenuItem
from PIL import Image
import pystray
import speech_recognition as sr
from pynput import keyboard
import atexit
import whisper
global audio_model
model = "base.en"
audio_model = whisper.load_model(
model, download_root=f"{os.path.dirname(os.path.abspath(__file__))}\\assets\\models")
global hidden
hidden = False
global vkeyboard
vkeyboard = keyboard.Controller()
global r
r = sr.Recognizer()
def gui_command():
os.system("gui_command.py 1")
def calibrate():
vkeyboard.release(keyboard.Key.ctrl)
time.sleep(0.1)
vkeyboard.release(keyboard.Key.alt)
time.sleep(0.1)
vkeyboard.release(keyboard.KeyCode.from_char('c'))
time.sleep(0.1)
with sr.Microphone() as source:
print("Please wait. Calibrating microphone...")
# listen for 5 seconds and calculate the ambient noise energy level
r.adjust_for_ambient_noise(source, duration=5)
print("Calibrated")
def show_window():
global window
win32gui.ShowWindow(window, win32con.SW_SHOW)
def hide_window():
global window
win32gui.ShowWindow(window, win32con.SW_HIDE)
def toggle_window():
vkeyboard.release(keyboard.Key.ctrl)
time.sleep(0.1)
vkeyboard.release(keyboard.Key.alt)
time.sleep(0.1)
vkeyboard.release(keyboard.KeyCode.from_char('s'))
time.sleep(0.1)
global hidden
if hidden:
show_window()
hidden = False
else:
hide_window()
hidden = True
def quit_program():
assistant_icon.visible = False
vkeyboard.release(keyboard.Key.ctrl)
time.sleep(0.1)
vkeyboard.release(keyboard.Key.alt)
time.sleep(0.1)
vkeyboard.release(keyboard.KeyCode.from_char('q'))
time.sleep(0.1)
os._exit(0)
def exit_action(icon):
icon.visible = False
os._exit(0)
def execute():
threading.Thread(target=recognizer.main,
args=(r.energy_threshold, audio_model)).start()
vkeyboard.release(keyboard.Key.ctrl)
time.sleep(0.1)
vkeyboard.release(keyboard.Key.alt)
time.sleep(0.1)
vkeyboard.release(keyboard.Key.shift)
time.sleep(0.1)
def setup(icon):
icon.visible = True
# toggle_window()
atexit.register(exit_action, icon)
calibrate()
print("Sentinel is running. Press Ctrl+Alt+Q to quit.")
print()
print("--------------------")
# Command hotkeys
print("Press Ctrl+Alt+C to calibrate microphone.")
print("Press Ctrl+Alt+S to toggle window visibility.")
print("Press Ctrl+Alt+Shift to execute a command.")
print("Press Ctrl+Alt+X to edit commands.")
with keyboard.GlobalHotKeys({
'<ctrl>+<alt>+<shift>': execute,
'<ctrl>+<alt>+q': quit_program,
'<ctrl>+<alt>+c': calibrate,
'<ctrl>+<alt>+s': toggle_window,
'<ctrl>+<alt>+x': gui_command}) as h:
h.join()
# System tray icon
assistant_logo_path = os.path.dirname(
os.path.abspath(__file__)) + "\\assets\\images\\logo.png"
assistant_logo = Image.open(assistant_logo_path)
assistant_icon = pystray.Icon("Sentinel")
assistant_icon.menu = Menu(
MenuItem("Exit", lambda: exit_action(assistant_icon)),
MenuItem("Calibrate", calibrate),
MenuItem("Toggle Window", toggle_window),
MenuItem("Edit Commands", gui_command),)
assistant_icon.icon = assistant_logo
assistant_icon.title = "Sentinel"
assistant_icon.run(setup)