-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.py
166 lines (155 loc) · 6.25 KB
/
Controller.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import pyautogui
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
import screen_brightness_control as sbcontrol
from Constants import Gest
class Controller:
tx_old = 0
ty_old = 0
trial = True
flag = False
grabflag = False
pinchmajorflag = False
pinchminorflag = False
pinchstartxcoord = None
pinchstartycoord = None
pinchdirectionflag = None
prevpinchlv = 0
pinchlv = 0
framecount = 0
prev_hand = None
pinch_threshold = 0.3
@staticmethod
def getpinchylv(hand_result):
dist = round((Controller.pinchstartycoord - hand_result.landmark[8].y) * 10, 1)
return dist
@staticmethod
def getpinchxlv(hand_result):
dist = round((hand_result.landmark[8].x - Controller.pinchstartxcoord) * 10, 1)
return dist
@staticmethod
def changesystembrightness():
currentBrightnessLv = sbcontrol.get_brightness()[0] / 100.0
currentBrightnessLv += Controller.pinchlv / 50.0
if currentBrightnessLv > 1.0:
currentBrightnessLv = 1.0
elif currentBrightnessLv < 0.0:
currentBrightnessLv = 0.0
sbcontrol.fade_brightness(int(100 * currentBrightnessLv), start=sbcontrol.get_brightness()[0])
@staticmethod
def changesystemvolume():
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
currentVolumeLv = volume.GetMasterVolumeLevelScalar()
currentVolumeLv += Controller.pinchlv / 50.0
if currentVolumeLv > 1.0:
currentVolumeLv = 1.0
elif currentVolumeLv < 0.0:
currentVolumeLv = 0.0
volume.SetMasterVolumeLevelScalar(currentVolumeLv, None)
@staticmethod
def scrollVertical():
pyautogui.scroll(120 if Controller.pinchlv > 0.0 else -120)
@staticmethod
def scrollHorizontal():
pyautogui.keyDown('shift')
pyautogui.keyDown('ctrl')
pyautogui.scroll(-120 if Controller.pinchlv > 0.0 else 120)
pyautogui.keyUp('ctrl')
pyautogui.keyUp('shift')
@staticmethod
def get_position(hand_result):
point = 9
position = [hand_result.landmark[point].x, hand_result.landmark[point].y]
sx, sy = pyautogui.size()
x_old, y_old = pyautogui.position()
x = int(position[0] * sx)
y = int(position[1] * sy)
if Controller.prev_hand is None:
Controller.prev_hand = x, y
delta_x = x - Controller.prev_hand[0]
delta_y = y - Controller.prev_hand[1]
distsq = delta_x ** 2 + delta_y ** 2
ratio = 1
Controller.prev_hand = [x, y]
if distsq <= 25:
ratio = 0
elif distsq <= 900:
ratio = 0.07 * (distsq ** (1 / 2))
else:
ratio = 2.1
x, y = x_old + delta_x * ratio, y_old + delta_y * ratio
return x, y
@staticmethod
def pinch_control_init(hand_result):
Controller.pinchstartxcoord = hand_result.landmark[8].x
Controller.pinchstartycoord = hand_result.landmark[8].y
Controller.pinchlv = 0
Controller.prevpinchlv = 0
Controller.framecount = 0
@staticmethod
def pinch_control(hand_result, controlHorizontal, controlVertical):
if Controller.framecount == 5:
Controller.framecount = 0
Controller.pinchlv = Controller.prevpinchlv
if Controller.pinchdirectionflag:
controlHorizontal()
else:
controlVertical()
lvx = Controller.getpinchxlv(hand_result)
lvy = Controller.getpinchylv(hand_result)
if abs(lvy) > abs(lvx) and abs(lvy) > Controller.pinch_threshold:
Controller.pinchdirectionflag = False
if abs(Controller.prevpinchlv - lvy) < Controller.pinch_threshold:
Controller.framecount += 1
else:
Controller.prevpinchlv = lvy
Controller.framecount = 0
elif abs(lvx) > Controller.pinch_threshold:
Controller.pinchdirectionflag = True
if abs(Controller.prevpinchlv - lvx) < Controller.pinch_threshold:
Controller.framecount += 1
else:
Controller.prevpinchlv = lvx
Controller.framecount = 0
@staticmethod
def handle_controls(gesture, hand_result):
x, y = None, None
if gesture != Gest.PALM:
x, y = Controller.get_position(hand_result)
if gesture != Gest.FIST and Controller.grabflag:
Controller.grabflag = False
pyautogui.mouseUp(button="left")
if gesture != Gest.PINCH_MAJOR and Controller.pinchmajorflag:
Controller.pinchmajorflag = False
if gesture != Gest.PINCH_MINOR and Controller.pinchminorflag:
Controller.pinchminorflag = False
if gesture == Gest.V_GEST:
Controller.flag = True
pyautogui.moveTo(x, y, duration=0.1)
elif gesture == Gest.FIST:
if not Controller.grabflag:
Controller.grabflag = True
pyautogui.mouseDown(button="left")
pyautogui.moveTo(x, y, duration=0.1)
elif gesture == Gest.MID and Controller.flag:
pyautogui.click()
Controller.flag = False
elif gesture == Gest.INDEX and Controller.flag:
pyautogui.click(button='right')
Controller.flag = False
elif gesture == Gest.TWO_FINGER_CLOSED and Controller.flag:
pyautogui.doubleClick()
Controller.flag = False
elif gesture == Gest.PINCH_MINOR:
if Controller.pinchminorflag is False:
Controller.pinch_control_init(hand_result)
Controller.pinchminorflag = True
Controller.pinch_control(hand_result, Controller.scrollHorizontal, Controller.scrollVertical)
elif gesture == Gest.PINCH_MAJOR:
if Controller.pinchmajorflag is False:
Controller.pinch_control_init(hand_result)
Controller.pinchmajorflag = True
Controller.pinch_control(hand_result, Controller.changesystembrightness, Controller.changesystemvolume)