-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile4.py
81 lines (63 loc) · 3.1 KB
/
file4.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
import cv2
import mediapipe as mp
import pyautogui
# Function to detect thumb and index finger touching and holding
def is_touch_hold(hand_keypoints):
# Threshold value for touch detection
touch_thresh = 0.04 # Adjust as needed
# Calculate distances between the top points of thumb and index finger for touch detection
thumb_top_y = hand_keypoints.landmark[4].y
index_top_y = hand_keypoints.landmark[8].y
thumb_index_dist = abs(thumb_top_y - index_top_y)
# Return True if thumb and index finger are touching and close enough (touch and hold gesture)
return thumb_index_dist < touch_thresh
# Function to draw top points of thumb and index finger and display distance
def draw_thumb_index_top_points(frame, hand_keypoints):
thumb_top_x = int(hand_keypoints.landmark[4].x * frame.shape[1])
thumb_top_y = int(hand_keypoints.landmark[4].y * frame.shape[0])
index_top_x = int(hand_keypoints.landmark[8].x * frame.shape[1])
index_top_y = int(hand_keypoints.landmark[8].y * frame.shape[0])
# Draw circles at the top points of thumb and index finger
cv2.circle(frame, (thumb_top_x, thumb_top_y), 5, (65, 176, 110), -1)
cv2.circle(frame, (index_top_x, index_top_y), 5, (242, 123, 189), -1)
return frame
# Initialize VideoCapture and MediaPipe Hands
cap = cv2.VideoCapture(0)
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1)
mp_drawing = mp.solutions.drawing_utils
# Main loop
while True:
_, frame = cap.read()
frame = cv2.flip(frame, 1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Process frame to detect hands
results = hands.process(rgb_frame)
# If hands are detected
if results.multi_hand_landmarks:
hand_landmarks = results.multi_hand_landmarks[0]
# Draw top points of thumb and index finger
frame = draw_thumb_index_top_points(frame, hand_landmarks)
# Get coordinates of thumb and index finger tips
thumb_tip_x = int(hand_landmarks.landmark[4].x * frame.shape[1])
thumb_tip_y = int(hand_landmarks.landmark[4].y * frame.shape[0])
index_tip_x = int(hand_landmarks.landmark[8].x * frame.shape[1])
index_tip_y = int(hand_landmarks.landmark[8].y * frame.shape[0])
# Move the mouse cursor to the position of the thumb tip
pyautogui.moveTo(index_tip_x, index_tip_y)
# Check for touch and hold gesture
if is_touch_hold(hand_landmarks):
# Perform left mouse click when touch and hold is detected
pyautogui.mouseDown()
else:
# Release left mouse button when touch and hold is released
pyautogui.mouseUp()
# Draw hand landmarks on the frame
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
cv2.imshow("Hand Gesture Control", frame)
# Exit loop if 'Esc' key is pressed
if cv2.waitKey(1) == 27:
break
# Release video capture and close OpenCV windows
cap.release()
cv2.destroyAllWindows()