-
Notifications
You must be signed in to change notification settings - Fork 0
/
hand_module.py
156 lines (120 loc) · 4.57 KB
/
hand_module.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
import cv2
import mediapipe as mp
import time
import mouse_movement
import threading
from xgbModel import xgbModel
mouseX = -1
mouseY = -1
updated = False
continueRunning = True
action = 30
class handDetector():
def __init__(self, mode = False, maxHands = 1, detectionCon = 0.5, trackCon = 0.5):
self.mode = mode
self.maxHands = maxHands
self.detectionCon = detectionCon
self.trackCon = trackCon
self.mouseX = -1
self.mouseY = -1
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(static_image_mode=False,
max_num_hands=1,
min_detection_confidence=0.5,
min_tracking_confidence=0.5)
self.mpDraw = mp.solutions.drawing_utils
def findHands(self,img, draw = True):
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.results = self.hands.process(imgRGB)
# print(self.results.multi_hand_landmarks)
if self.results.multi_hand_landmarks:
for handLms in self.results.multi_hand_landmarks:
if draw:
self.mpDraw.draw_landmarks(img, handLms, self.mpHands.HAND_CONNECTIONS)
return img
def findPosition(self, img, handNo = 0, draw = True):
lmlist = []
if self.results.multi_hand_landmarks:
myHand = self.results.multi_hand_landmarks[handNo]
for id, lm in enumerate(myHand.landmark):
h, w, c = img.shape
cx, cy = int(lm.x* w), int(lm.y * h)
lmlist.append([id, cx, cy, lm.z * -10])
if draw:
cv2.circle(img, (cx, cy), 3, (255, 0, 255), cv2.FILLED)
return lmlist
def main():
videoThread = threading.Thread(target=video, args=())
mouseThread = threading.Thread(target=mouse, args=())
videoThread.start()
mouseThread.start()
mouseThread.join()
videoThread.join()
def video():
pTime = 0
cTime = 0
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH,1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,1080)
"""cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)"""
detector = handDetector()
model = xgbModel()
global continueRunning
while continueRunning:
success, img = cap.read()
img = detector.findHands(img)
lmlist = detector.findPosition(img)
if len(lmlist) != 0:
global mouseX
global mouseY
global updated
global action
updated = True
mouseX = lmlist[9][1]
mouseY = lmlist[9][2]
#
# ~~~ READ ACTION ~~~~
#
action = model.classify(lmlist)
action = action*10+10
else:
updated = False
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
# cv2.imshow("window", img)
cv2.waitKey(1)
def mouse():
global continueRunning
while continueRunning:
if (mouseX != -1 and mouseY != -1 and updated):
mouse_movement.updateMouse(mouseX, mouseY) # Pointer finger tip = index 8
"""# Pinky tip = index 20
# Ring tip = index 16
# Middle tip = index 12
# Index tip = index 8
# Thumb tip = index 4
tipDists = [math.dist(lmlist[4][1:2], lmlist[20][1:2]), math.dist(lmlist[4][1:2], lmlist[16][1:2]), math.dist(lmlist[4][1:2], lmlist[12][1:2]), math.dist(lmlist[4][1:2], lmlist[8][1:2])]
minDists = min(tipDists)
if minDists < 10 + 5*lmlist[4][3]:
finger = tipDists.index(minDists) #0: pinky 1: ring 2: middle 3: index
if finger == 0:
print("pinky-thumb")
if finger == 1:
print("ring-thumb")
if finger == 2:
print("middle-thumb")
if finger == 3:
print("index-thumb")"""
if action == 10: # Thumbs up = middle click
mouse_movement.middleClickHold()
elif action == 20: # Thumbs down = right click
continueRunning = False
elif action == 30: # Open palm = no click
mouse_movement.unclick()
elif action == 40: # Closed fist = left click
mouse_movement.leftClickHold()
if __name__ == "__main__":
main()