-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
93 lines (80 loc) · 2.97 KB
/
Copy pathcontroller.py
File metadata and controls
93 lines (80 loc) · 2.97 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
import pyautogui
import numpy as np
import time
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
class Controller:
"""
Executes system commands based on gestures.
"""
def __init__(self):
pyautogui.FAILSAFE = False
self.screen_w, self.screen_h = pyautogui.size()
self.smoothening = 7
self.ploc_x, self.ploc_y = 0, 0
self.cloc_x, self.cloc_y = 0, 0
# Audio Setup
self.volume = None
self.min_vol = 0
self.max_vol = 0
try:
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
self.volume = cast(interface, POINTER(IAudioEndpointVolume))
self.vol_range = self.volume.GetVolumeRange()
self.min_vol = self.vol_range[0]
self.max_vol = self.vol_range[1]
except Exception as e:
print(f"Warning: Audio control unavailable. Error: {e}")
def move_mouse(self, x, y, frame_w, frame_h):
"""
Moves the mouse with smoothing.
Maps camera coordinates to screen coordinates.
"""
# Interpolate
# Use a frame reduction to reach corners easier
frame_r = 100 # Frame Reduction
x3 = np.interp(x, (frame_r, frame_w - frame_r), (0, self.screen_w))
y3 = np.interp(y, (frame_r, frame_h - frame_r), (0, self.screen_h))
# Smoothening
self.smoothening = 5
self.cloc_x = self.ploc_x + (x3 - self.ploc_x) / self.smoothening
self.cloc_y = self.ploc_y + (y3 - self.ploc_y) / self.smoothening
# Move Mouse
pyautogui.moveTo(self.cloc_x, self.cloc_y)
self.ploc_x, self.ploc_y = self.cloc_x, self.cloc_y
def click(self):
pyautogui.click()
def scroll(self, direction):
if direction == "up":
pyautogui.scroll(300)
elif direction == "down":
pyautogui.scroll(-300)
def minimize(self):
"""
Minimizes the current window.
"""
pyautogui.hotkey('win', 'down')
def change_video(self, direction):
"""
Changes video using media keys.
"""
if direction == "next":
pyautogui.press('nexttrack')
elif direction == "prev":
pyautogui.press('prevtrack')
def set_volume(self, length, max_length=200, min_length=50):
"""
Sets volume based on pinch distance.
"""
if self.volume:
vol = np.interp(length, [min_length, max_length], [self.min_vol, self.max_vol])
self.volume.SetMasterVolumeLevel(vol, None)
def perform_action(self, action):
if action == "Swipe Right":
self.change_video("next")
elif action == "Swipe Left":
self.change_video("prev")
elif action == "Open Palm":
pyautogui.press('win') # Open Menu