-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
64 lines (49 loc) · 2.04 KB
/
utils.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
import yaml
import subprocess
import moosegesture
# moosegesture._MIN_STROKE_LEN = 100
gesture_map = None
command_map = None
tuple_gesture_keys = None
def reload_config(gesture_command_map_file='gesture_map.yml'):
with open(gesture_command_map_file, 'r') as f:
map_config = yaml.full_load(f)
global gesture_map, command_map, tuple_gesture_keys
gesture_map = map_config['gesture_map']
command_map = map_config['command_map']
tuple_gesture_keys = [eval(gesture) for gesture in gesture_map.keys()]
def execute_command(gesture):
subprocess.Popen([command_map[gesture]['command']], shell=True)
def sanitize_and_notify(coord_set):
timestamp_vals = {}
# filter all X axis events and set their timestamps
for _, x_event in coord_set:
if x_event is not None:
timestamp_vals[f'{x_event.timestamp()}'] = [x_event.value]
# filter all Y axis events and set their timestamps
for y_event, _ in coord_set:
try:
if y_event is not None:
try:
timestamp_vals[f'{y_event.timestamp()}'][1] = y_event.value
except IndexError:
timestamp_vals[f'{y_event.timestamp()}'].append(y_event.value)
except KeyError:
pass
sanitized_tuple_list = []
for item in timestamp_vals.values():
if len(item) == 2:
sanitized_tuple_list.append(tuple(item))
# ignore the first 10% events cause it's garbage sometimes
trim_beginning_len = 0.1*len(sanitized_tuple_list)
detected_gesture = moosegesture.getGesture(
sanitized_tuple_list[int(trim_beginning_len):])
# print(f'Gesture is :- {detected_gesture}')
closest_match = moosegesture.findClosestMatchingGesture(
detected_gesture, tuple_gesture_keys, maxDifference=4)
if closest_match is None:
return None
notify(f"Gesture Detected :- {gesture_map[str(closest_match[0])]}")
return gesture_map[str(closest_match[0])]
def notify(notif_text):
subprocess.Popen(['notify-send', 'Midas', notif_text])