-
Notifications
You must be signed in to change notification settings - Fork 1
/
powerpoint.py
60 lines (53 loc) · 2.17 KB
/
powerpoint.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
import cv2
import mediapipe as mp
import pyautogui
cap = cv2.VideoCapture(1) # Adjust the camera index if necessary
hand_detector = mp.solutions.hands.Hands()
drawing_utils = mp.solutions.drawing_utils
screen_width, screen_height=pyautogui.size()
index_y=0
index_x=0
while True:
ret, frame = cap.read()
if not ret or frame is None:
print("Error reading frame from camera")
continue
frame = cv2.flip(frame, 1)
frame_height, frame_width, _ = frame.shape
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
output = hand_detector.process(rgb_frame)
hands = output.multi_hand_landmarks
if hands:
print("Detected hands:", len(hands))
for hand in hands:
drawing_utils.draw_landmarks(frame, hand)
landmarks = hand.landmark
# print("Number of landmarks:", len(landmarks))
for id, landmark in enumerate(landmarks):
x = int(landmark.x * frame_width)
y = int(landmark.y * frame_height)
# print("Landmark", id, ":", x, y)
if id == 17: #pointer
cv2.circle(frame, center=(x, y), radius=15, color=(0, 255, 255))
index_x=screen_width/frame_width*x
index_y=screen_height/frame_height*y
# pyautogui.moveTo(index_x,index_y)
if id == 4: #thumb
cv2.circle(frame, center=(x, y), radius=15, color=(0, 255, 255))
middle_x=screen_width/frame_width*x
middle_y=screen_height/frame_height*y
# pyautogui.moveTo(middle_x, middle_y)
# print('outside', abs(index_y-middle_y))
if abs(index_x)<abs(middle_x):
print('right')
pyautogui.press('right')
pyautogui.sleep(2)
if abs(index_x) > abs(middle_x):
print('left')
pyautogui.press('left')
pyautogui.sleep(2)
cv2.imshow('Virtual Mouse', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()