-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhand_detection.py
138 lines (110 loc) · 4.42 KB
/
hand_detection.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
import mediapipe as mp
import cv2
import pyautogui
import pygetwindow
import time
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
webcam = cv2.VideoCapture(0)
mutatoX = 0
huvelykX = 0
kozepsoX = 0
mutatoY = 0
huvelykY = 0
kozepsoY = 0
screenX, screenY = pyautogui.size()
cv2font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
fontColor = (255, 0, 0)
fontThickness = 2
windows = pygetwindow.getAllWindows()
print(windows)
with mp_hands.Hands(min_detection_confidence=0.8, min_tracking_confidence=0.5, max_num_hands=1) as hands:
while webcam.isOpened():
ret, frame = webcam.read()
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
height, width, _ = image.shape
image.flags.writeable = False
results = hands.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
line_setup = mp_drawing.DrawingSpec(
color=(121, 44, 250),
thickness=2,
circle_radius=2,
)
bump_setup = mp_drawing.DrawingSpec(
color=(121, 22, 76),
thickness=2,
circle_radius=4,
)
detected_hands = results.multi_hand_landmarks
if detected_hands:
for hand in detected_hands:
mp_drawing.draw_landmarks(image,
hand,
mp_hands.HAND_CONNECTIONS,
bump_setup,
line_setup,
)
landmarks = hand.landmark
for id, landmark in enumerate(landmarks):
x = int(landmark.x * width)
y = int(landmark.y * height)
# Mutatóujj
if id == 8:
cv2.circle(
img=image,
center=(x, y),
radius=8,
color=(240, 0, 0),
thickness=8,
)
mutatoX = x
mutatoY = y
# Hüvelykujj
if id == 4:
cv2.circle(
img=image,
center=(x, y),
radius=8,
color=(0, 0, 240),
thickness=8,
)
huvelykX = x
huvelykY = y
# Kozepsoujj
if id == 12:
cv2.circle(
img=image,
center=(x, y),
radius=8,
color=(0, 0, 240),
thickness=8,
)
kozepsoX = x
kozepsoY = y
volume_distance = ((huvelykX-mutatoX)**2 + (huvelykY-mutatoY)**2)**(0.5)//4
middle_distance = ((huvelykX-kozepsoX)**2 + (huvelykY-kozepsoY)**2)**(0.5)//4
# vonal a hangerőcsík mutatására
cv2.line(image, (mutatoX, mutatoY), (huvelykX, huvelykY), color=(0, 250, 0), thickness=5)
# vonal a középső gombhoz
cv2.line(image, (kozepsoX, kozepsoY), (huvelykX, huvelykY), color=(0, 250, 250), thickness=5)
# hangerőtávolság kiírása
org = (mutatoX+20, mutatoY)
if volume_distance:
cv2.putText(image, str(volume_distance), org, cv2font, fontScale, fontColor, fontThickness, cv2.LINE_AA)
# hangerőszabályzás
if volume_distance > 65:
pyautogui.press("volumeup")
if volume_distance < 10:
pyautogui.press("volumedown")
# print(results.multi_hand_landmarks)
cv2.imshow("Hand tracking", image)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
webcam.release()
cv2.destroyAllWindows()
#pyautogui.moveTo(sreenX/2, screenY/2, 2) # moves mouse to X of 100, Y of 200 over 2 seconds
# pyautogui.doubleClick() # perform a left-button double click
# pyautogui.click(button='right', clicks=3, interval=0.25) ## triple-click the right mouse button with a quarter second pause in between clicks