-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
315 lines (251 loc) · 14.1 KB
/
app.py
File metadata and controls
315 lines (251 loc) · 14.1 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import platform
import subprocess
import tkinter as tk
import oldversion.camera as ca
import oldversion.utile as utile
import gesture.mouse_simulator as ms
import GestureRecognition.gesture_recognition as gs
import oldversion.cleanup as cleanup
import tkinter.messagebox as messagebox
import tkinter.ttk as ttk
import configparser
from playsound import playsound
import time
import os
settings = {
"selected_camera": "",
"selected_music_app": "",
"hotkey": "",
"hotkey2": "",
"hotkey3": "",
"hotkey4": "",
"hotkey5": "",
}
hotkey_entry = tk.Entry
recognitior = gs.GestureRecognition()
def start_mouse_simulation():
ms.start_recognition()
messagebox.showinfo("message", "simulation closed!")
def show_tutorial():
tutorial_window = tk.Toplevel(root)
tutorial_window.title("Tutorial")
tutorial_window.geometry("600x400")
tutorial_window.configure(bg="#f0f0f0")
scrollbar = tk.Scrollbar(tutorial_window)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
text_widget = tk.Text(tutorial_window, wrap=tk.WORD, yscrollcommand=scrollbar.set,
font=("Helvetica", 14), bg="#ffffff", fg="#333333")
text_widget.pack(expand=True, fill='both', padx=20, pady=20)
tutorial_text = """
1. Move the mouse: Extend your right hand palm to move (note that it is the right hand).
2. Left mouse button: Keep your fingers upward, use your index finger and thumb to touch; keep the other fingers upward, this is the left click.
3. Right mouse button: Similarly, use your middle finger and thumb to touch.
4. Drag, hold the left button: Make a fist, then extend your thumb to trigger the drag mode, moving with the thumb as the coordinate. Switch to using your palm to engage the drag mode.
"""
text_widget.insert(tk.END, tutorial_text)
text_widget.config(state='disabled')
scrollbar.config(command=text_widget.yview)
def start_gesture_recognition():
recognitior.start()
messagebox.showinfo("message", "recognition closed!")
config = configparser.ConfigParser()
def load_settings(): # load from config file
try:
config.read('GestureRecognition/config.ini')
settings["hotkey"] = config.get('hotkey', 'value')
settings["hotkey2"] = config.get('hotkey2', 'value')
settings["hotkey3"] = config.get('hotkey3', 'value')
settings["hotkey4"] = config.get('hotkey4', 'value')
settings["hotkey5"] = config.get('hotkey5', 'value')
except Exception as e:
messagebox.showerror('config file not exist, will load from preset file', f'fail {str(e)}')
def save_settings(selected_camera, selected_music_app, hotkey):
# Update the global setting
settings["selected_camera"] = selected_camera.get()
settings["selected_music_app"] = selected_music_app.get()
settings["hotkey"] = hotkey[0].get()
settings["hotkey2"] = hotkey[1].get()
settings["hotkey3"] = hotkey[2].get()
settings["hotkey4"] = hotkey[3].get()
settings["hotkey5"] = hotkey[4].get()
config['hotkey'] = {'value': hotkey[0].get()}
config['hotkey2'] = {'value': hotkey[1].get()}
config['hotkey3'] = {'value': hotkey[2].get()}
config['hotkey4'] = {'value': hotkey[3].get()}
config['hotkey5'] = {'value': hotkey[4].get()}
with open('GestureRecognition/config.ini', 'w') as configfile:
config.write(configfile)
messagebox.showinfo('Saved', 'Saved to file')
messagebox.showinfo("Save Setting", "Configuration saved!")
def load_preset(preset, hotkey_entry_var):
config2 = configparser.ConfigParser()
config2.read('GestureRecognition/preset.ini')
try:
hotkey_entry_var[0].set(config2.get(preset, 'h1'))
hotkey_entry_var[1].set(config2.get(preset, 'h2'))
hotkey_entry_var[2].set(config2.get(preset, 'h3'))
hotkey_entry_var[3].set(config2.get(preset, 'h4'))
hotkey_entry_var[4].set(config2.get(preset, 'h5'))
except Exception as e:
messagebox.showerror('Error to load preset file', f'fail {str(e)}')
def save_preset(preset, hotkey):
config2 = configparser.ConfigParser()
config2.read('GestureRecognition/preset.ini')
config2[preset.get()] = {
'preset': preset.get(),
'h1': hotkey[0].get(),
'h2': hotkey[1].get(),
'h3': hotkey[2].get(),
'h4': hotkey[3].get(),
'h5': hotkey[4].get()
}
with open('GestureRecognition/preset.ini', 'w') as configfile:
config2.write(configfile)
messagebox.showinfo(preset.get(), 'New preset saved')
def open_settings():
load_settings()
# here to open the setting page
settings_window = tk.Toplevel(root)
settings_window.title("Setting")
# settings_window.geometry("300x250")
# Camera and music app
avaible_camera = ca.find_available_cameras()
camera_options = avaible_camera
music_app_options = ["App music 1", "Spotify 2"]
selected_camera = tk.StringVar()
selected_music_app = tk.StringVar()
hotkey_entry_var = [tk.StringVar(), tk.StringVar(), tk.StringVar(), tk.StringVar(), tk.StringVar()]
preset = tk.StringVar()
# Rollback to previous saved setting
selected_camera.set(camera_options[0] if camera_options else "No available Camera ")
selected_music_app.set(settings["selected_music_app"] if settings["selected_music_app"] else music_app_options[0])
hotkey_entry_var[0].set(settings["hotkey"])
hotkey_entry_var[1].set(settings["hotkey2"])
hotkey_entry_var[2].set(settings["hotkey3"])
hotkey_entry_var[3].set(settings["hotkey4"])
hotkey_entry_var[4].set(settings["hotkey5"])
# Create UI
tk.Label(settings_window, text="Select Camera:").grid(row=0, column=0, pady=10, padx=10, sticky="w")
camera_menu = ttk.Combobox(settings_window, textvariable=selected_camera, values=camera_options)
camera_menu.grid(row=0, column=1, padx=10, sticky="ew")
tk.Label(settings_window, text="Select music app:").grid(row=1, column=0, pady=10, padx=10, sticky="w")
music_app_menu = ttk.Combobox(settings_window, textvariable=selected_music_app, values=music_app_options)
music_app_menu.grid(row=1, column=1, padx=10, sticky="ew")
tk.Button(settings_window, text="Open App", command=launch_app).grid(row=2, column=0, pady=10, padx=10, sticky="ew",
columnspan=2)
tk.Label(settings_window, text="👆Set hotkey 1:").grid(row=3, column=0, pady=10, padx=10, sticky="w")
hotkey_entry = tk.Entry(settings_window, textvariable=hotkey_entry_var[0])
hotkey_entry.grid(row=3, column=1, padx=10, sticky="ew")
tk.Label(settings_window, text="👇Set hotkey 2:").grid(row=4, column=0, pady=10, padx=10, sticky="w")
tk.Entry(settings_window, textvariable=hotkey_entry_var[1]).grid(row=4, column=1, padx=10, sticky="ew")
tk.Label(settings_window, text="🤙Set hotkey 3:").grid(row=5, column=0, pady=10, padx=10, sticky="w")
tk.Entry(settings_window, textvariable=hotkey_entry_var[2]).grid(row=5, column=1, padx=10, sticky="ew")
tk.Label(settings_window, text="💅Set hotkey 4:").grid(row=6, column=0, pady=10, padx=10, sticky="w")
tk.Entry(settings_window, textvariable=hotkey_entry_var[3]).grid(row=6, column=1, padx=10, sticky="ew")
tk.Label(settings_window, text="✋Set hotkey 5:").grid(row=7, column=0, pady=10, padx=10, sticky="w")
tk.Entry(settings_window, textvariable=hotkey_entry_var[4]).grid(row=7, column=1, padx=10, sticky="ew")
tk.Label(settings_window, text="🤘Preset Settings").grid(row=8, column=0, pady=10, padx=10, sticky="ew",
columnspan=2, rowspan=2)
tk.Button(settings_window, text="Default", command=lambda: load_preset('default', hotkey_entry_var)).grid(row=10,
column=0,
pady=10,
padx=10,
sticky="ew")
tk.Button(settings_window, text="PowerPoint", command=lambda: load_preset('powerpoint', hotkey_entry_var)).grid(
row=10, column=1, pady=10, padx=10, sticky="ew")
tk.Entry(settings_window, textvariable=preset).grid(row=11, column=0, padx=10, sticky="ew", columnspan=2)
tk.Button(settings_window, text="Load", command=lambda: load_preset(preset.get(), hotkey_entry_var)).grid(row=12,
column=0,
pady=10,
padx=10,
sticky="ew")
tk.Button(settings_window, text="Save New", command=lambda: save_preset(preset, hotkey_entry_var)).grid(row=12,
column=1,
pady=10,
padx=10,
sticky="ew")
tk.Label(settings_window, text="‧₊˚ ☁️⋅♡𓂃 ࣪ ִֶָ☾. ⋆。°•☁️.").grid(row=13, column=0, pady=10, padx=10, sticky="ew",
columnspan=2)
# Create save and back button
save_button = tk.Button(settings_window, text="Save",
command=lambda: save_settings(selected_camera, selected_music_app, hotkey_entry_var))
save_button.grid(row=14, column=0, pady=10, padx=10, sticky="ew")
back_button = tk.Button(settings_window, text="Back", command=settings_window.destroy)
back_button.grid(row=14, column=1, pady=10, padx=10, sticky="ew")
settings_window.grid_columnconfigure(1, weight=1)
def exit_app():
cleanup.cleanup()
root.destroy()
exit(0)
def launch_app():
spotify_path = utile.find_spotify_path()
if spotify_path:
print(f"Launching Spotify from: {spotify_path}")
if platform.system() == "Darwin": # macOS
subprocess.Popen(["open", spotify_path])
else:
subprocess.Popen([spotify_path])
else:
print("Spotify installation not found.")
# Create the main windows
root = tk.Tk()
root.title("WavEase!")
# root.geometry("500x500") # initial size
root.resizable(False, False)
# root.minsize(500, 300)
root.configure(background="#87CEEB")
# layout
# root.grid_columnconfigure([1, 2], weight=1, minsize=70)
# root.grid_columnconfigure(1, weight=1, minsize=70)
# root.grid_rowconfigure(0, weight=1, minsize=50)
# root.grid_rowconfigure(1, weight=1, minsize=10)
# root.grid_rowconfigure([2,3], weight=1, minsize=10) ## does not work for Mac
root.attributes('-alpha', 1.0)
# Add a label
# label = tk.Label(root, text=r"""\
# __ __ _ _ __ __ ______
# \ \ / / | | | | \ \ / / | ____|
# \ \ /\ / / ___ | | ___ ___ _ __ ___ ___ | |_ ___ \ \ /\ / / __ _ __ __ | |__ __ _ ___ ___
# \ \/ \/ / / _ \ | | / __| / _ \ | '_ ` _ \ / _ \ | __| / _ \ \ \/ \/ / / _` | \ \ / / | __| / _` | / __| / _ \
# \ /\ / | __/ | | | (__ | (_) | | | | | | | | __/ | |_ | (_) | \ /\ / | (_| | \ V / | |____ | (_| | \__ \ | __/
# \/ \/ \___| |_| \___| \___/ |_| |_| |_| \___| \__| \___/ \/ \/ \__,_| \_/ |______| \__,_| |___/ \___|""", background='#c3c3c3')
label = tk.Label(root, text=r""" ༄ Welcome to WavEase ༄ """, background='#87CEEB', fg="white")
label.configure(font=("Comic Sans MS", 28, "bold"))
label.grid(row=0, column=1, sticky="nsew", padx=20, pady=10, columnspan=2)
# tree graphic
frameCnt = 120
frames = [tk.PhotoImage(file='.assets/tree-01.gif', format='gif -index %i' % (i)) for i in range(frameCnt)]
def update(ind):
frame = frames[ind]
ind += 1
if ind == frameCnt:
ind = 0
tree.configure(image=frame)
root.after(100, update, ind)
tree = tk.Label(root, background='#87CEEB')
tree.grid(row=1, column=1, sticky="nsew", columnspan=2)
root.after(0, update, 0)
# Add Button
start_button = tk.Button(root, text="Start Recognition",
command=start_gesture_recognition,
background='#87CEEB', fg="white")
start_button.grid(row=2, column=1, padx=8, pady=8, ipadx=30, ipady=5, sticky='ew')
mouse_button = tk.Button(root, text="Start Mouse Simulation",
command=start_mouse_simulation,
background='#87CEEB', fg="white")
mouse_button.grid(row=2, column=2, padx=8, pady=8, ipadx=30, ipady=5, sticky='ew')
tutorial_button = tk.Button(root, text="Show Tutorial",
command=show_tutorial,
background='#87CEEB', fg="white")
tutorial_button.grid(row=2, column=3, padx=8, pady=8, ipadx=30, ipady=5, sticky='ew')
settings_button = tk.Button(root, text="Settings",
command=open_settings,
background='#87CEEB', fg="white")
settings_button.grid(row=3, column=1, padx=8, pady=8, ipadx=30, ipady=5, sticky='ew')
exit_button = tk.Button(root, text="Exit",
command=exit_app,
background='#87CEEB', fg="white")
exit_button.grid(row=3, column=2, padx=8, pady=8, ipadx=30, ipady=5, sticky='ew')
playsound('.assets/bird_audio.wav')
# start the evert loop
root.mainloop()