-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTM.py
66 lines (49 loc) · 2.19 KB
/
HTM.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
import cv2
import mediapipe as mp
import time
import math
import pyautogui
cap = cv2.VideoCapture(0)
mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDraw = mp.solutions.drawing_utils
def calculate_distance(x1, y1, x2, y2):
return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
pTime = 0
cTime = 0
VOLUME_THRESHOLD = 65
while True:
success, img = cap.read()
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = hands.process(imgRGB)
if results.multi_hand_landmarks:
for handLms in results.multi_hand_landmarks:
thumb_tip_x, thumb_tip_y = int(handLms.landmark[4].x * img.shape[1]), int(handLms.landmark[4].y * img.shape[0])
index_tip_x, index_tip_y = int(handLms.landmark[8].x * img.shape[1]), int(handLms.landmark[8].y * img.shape[0])
distance = calculate_distance(thumb_tip_x, thumb_tip_y, index_tip_x, index_tip_y)
cv2.line(img, (thumb_tip_x, thumb_tip_y), (index_tip_x, index_tip_y), (0, 255, 0), 5)
if distance< VOLUME_THRESHOLD:
hand_gesture = "vol_down"
elif distance > VOLUME_THRESHOLD:
hand_gesture = "vol_up"
else:
hand_gesture = "other"
if hand_gesture =="vol_up":
pyautogui.press('volumeup')
elif hand_gesture =="vol_down":
pyautogui.press("volumedown")
if distance < VOLUME_THRESHOLD:
cv2.putText(img, f"Distance:{round(distance,2)}<65, Volume Decreasing", (10, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 0), 2)
else:
cv2.putText(img, f"Distance:{round(distance,2)}>65, Volume Increasing", (10, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,0, 0), 2)
mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img, f" {str(int(fps))} FPS", (10, 70), cv2.FONT_HERSHEY_PLAIN, 3,
(0, 255, 0), 3)
cv2.imshow("Image", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()