-
Notifications
You must be signed in to change notification settings - Fork 0
/
file6.py
74 lines (57 loc) · 2.24 KB
/
file6.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
import cv2
import mediapipe as mp
import pyautogui
import serial
# Function to detect thumb and index finger touching and holding
def is_touch_hold(hand_keypoints):
touch_thresh = 0.04 # Threshold 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 thumb_index_dist < touch_thresh
# Initialize VideoCapture and MediaPipe Hands
cap = cv2.VideoCapture(1)
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1)
mp_drawing = mp.solutions.drawing_utils
# Initialize serial communication with ESP32
try:
ser = serial.Serial('COM3', 9600) # Replace 'COM3' with the correct port
except serial.SerialException as e:
print("Error:", e)
exit()
# Main loop
while True:
ret, frame = cap.read()
if not ret:
print("Error: Unable to capture frame")
break
frame = cv2.flip(frame, 1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(rgb_frame)
if results.multi_hand_landmarks:
hand_landmarks = results.multi_hand_landmarks[0]
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])
pyautogui.moveTo(index_tip_x, index_tip_y)
if is_touch_hold(hand_landmarks):
pyautogui.mouseDown()
else:
pyautogui.mouseUp()
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
if ser.in_waiting > 0:
serial_data = ser.readline().decode().strip()
print("Received:", serial_data)
if serial_data == "scroll_up":
pyautogui.scroll(1) # Scroll up
elif serial_data == "scroll_down":
pyautogui.scroll(-1) # Scroll down
cv2.imshow("Hand Gesture Control", frame)
if cv2.waitKey(1) == 27:
break
# Release resources
cap.release()
ser.close()
cv2.destroyAllWindows()