Newer
Older
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
import cv2
import mediapipe as mp
import pyautogui
import random
import util
import pynput
from pynput.mouse import Button
from pynput.keyboard import Key
mouse = pynput.mouse.Controller()
keyboard = pynput.keyboard.Controller()
screen_width, screen_height = pyautogui.size()
mpHands = mp.solutions.hands
hands = mpHands.Hands(
static_image_mode=False,
model_complexity=1,
min_detection_confidence=0.7,
min_tracking_confidence=0.7,
max_num_hands=1
)
def find_finger_tip(processed):
if processed.multi_hand_landmarks:
hand_landmarks = processed.multi_hand_landmarks[0] # Assuming only one hand is detected
index_finger_tip = hand_landmarks.landmark[mpHands.HandLandmark.INDEX_FINGER_TIP]
return index_finger_tip
return None, None
def move_mouse(index_finger_tip):
if index_finger_tip is not None:
x = int(index_finger_tip.x * screen_width)
y = int(index_finger_tip.y / 2 * screen_height)
pyautogui.moveTo(x, y)
def is_left_click(landmark_list, thumb_index_dist):
return (
util.get_angle(landmark_list[5], landmark_list[6], landmark_list[8]) < 50 and
util.get_angle(landmark_list[9], landmark_list[10], landmark_list[12]) > 90 and
thumb_index_dist > 50
)
def is_right_click(landmark_list, thumb_index_dist):
return (
util.get_angle(landmark_list[9], landmark_list[10], landmark_list[12]) < 50 and
util.get_angle(landmark_list[5], landmark_list[6], landmark_list[8]) > 90 and
thumb_index_dist > 50
)
def is_double_click(landmark_list, thumb_index_dist):
return (
util.get_angle(landmark_list[5], landmark_list[6], landmark_list[8]) < 50 and
util.get_angle(landmark_list[9], landmark_list[10], landmark_list[12]) < 50 and
thumb_index_dist > 50
)
def is_screenshot(landmark_list, thumb_index_dist):
return (
util.get_angle(landmark_list[5], landmark_list[6], landmark_list[8]) < 50 and
util.get_angle(landmark_list[9], landmark_list[10], landmark_list[12]) < 50 and
thumb_index_dist < 50
)
def detect_gesture(frame, landmark_list, processed):
if len(landmark_list) >= 21:
index_finger_tip = find_finger_tip(processed)
thumb_index_dist = util.get_distance([landmark_list[4], landmark_list[5]])
if util.get_distance([landmark_list[4], landmark_list[5]]) < 50 and util.get_angle(landmark_list[5], landmark_list[6], landmark_list[8]) > 90:
move_mouse(index_finger_tip)
elif is_left_click(landmark_list, thumb_index_dist):
pyautogui.click()
#mouse.press(Button.left)
#mouse.release(Button.left)
cv2.putText(frame, "Left Click", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
elif is_right_click(landmark_list, thumb_index_dist):
pyautogui.rightClick()
#mouse.press(Button.right)
#mouse.release(Button.right)
cv2.putText(frame, "Right Click", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
elif is_double_click(landmark_list, thumb_index_dist):
#pyautogui.doubleClick()
keyboard.press(Key.space)
keyboard.release(Key.space)
cv2.putText(frame, "Space", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 2)
elif is_screenshot(landmark_list,thumb_index_dist ):
im1 = pyautogui.screenshot()
label = random.randint(1, 1000)
im1.save(f'my_screenshot_{label}.png')
cv2.putText(frame, "Screenshot Taken", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 2)
def main():
draw = mp.solutions.drawing_utils
cap = cv2.VideoCapture(0)
try:
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, 1)
frameRGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
processed = hands.process(frameRGB)
landmark_list = []
if processed.multi_hand_landmarks:
hand_landmarks = processed.multi_hand_landmarks[0] # Assuming only one hand is detected
draw.draw_landmarks(frame, hand_landmarks, mpHands.HAND_CONNECTIONS)
for lm in hand_landmarks.landmark:
landmark_list.append((lm.x, lm.y))
detect_gesture(frame, landmark_list, processed)
cv2.imshow('Frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()