-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrial1.py
More file actions
141 lines (112 loc) · 4.3 KB
/
trial1.py
File metadata and controls
141 lines (112 loc) · 4.3 KB
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
139
140
141
import cv2
import mediapipe as mp
import time
import math
import threading
from pynput.keyboard import Key, Controller
CAMERA_WIDTH = 640
CAMERA_HEIGHT = 480
GESTURE_COOLDOWN = 1.0
# ------------------ One Euro Filter ------------------
class OneEuroFilter:
def __init__(self, t0, x0, min_cutoff=1.0, beta=0.01):
self.min_cutoff = min_cutoff
self.beta = beta
self.x_prev = x0
self.dx_prev = 0.0
self.t_prev = t0
def smoothing_factor(self, dt, cutoff):
r = 2 * math.pi * cutoff * dt
return r / (r + 1)
def filter(self, t, x):
dt = t - self.t_prev
if dt <= 0:
return self.x_prev
dx = (x - self.x_prev) / dt
cutoff = self.min_cutoff + self.beta * abs(dx)
a = self.smoothing_factor(dt, cutoff)
x_hat = a * x + (1 - a) * self.x_prev
self.x_prev = x_hat
self.t_prev = t
return x_hat
# ------------------ Threaded Camera ------------------
class ThreadedCamera:
def __init__(self):
self.cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
self.cap.set(3, CAMERA_WIDTH)
self.cap.set(4, CAMERA_HEIGHT)
self.ret, self.frame = self.cap.read()
self.stopped = False
threading.Thread(target=self.update, daemon=True).start()
def update(self):
while not self.stopped:
self.ret, self.frame = self.cap.read()
def read(self):
return self.ret, self.frame
def stop(self):
self.stopped = True
self.cap.release()
# ------------------ Main ------------------
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1)
keyboard = Controller()
camera = ThreadedCamera()
vol_filter = OneEuroFilter(time.time(), 0.5)
previous_y = 0.5
last_gesture_time = 0
print("✋ Open = Play | ✊ Fist = Pause | ✌️ Peace = Mute | 🤏 Pinch = Volume")
while True:
success, frame = camera.read()
if not success:
break
frame = cv2.flip(frame, 1)
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(rgb)
h, w, _ = frame.shape
current_time = time.time()
if results.multi_hand_landmarks:
for hand in results.multi_hand_landmarks:
lm = hand.landmark
# -------- Finger detection --------
fingers = []
for tip, pip in [(8,6),(12,10),(16,14),(20,18)]:
fingers.append(lm[tip].y < lm[pip].y)
thumb = lm[4].x < lm[3].x
fingers.insert(0, thumb)
total = sum(fingers)
# -------- Pinch --------
pinch_dist = math.hypot(lm[4].x - lm[8].x, lm[4].y - lm[8].y)
hand_scale = math.hypot(lm[0].x - lm[5].x, lm[0].y - lm[5].y)
pinch_ratio = pinch_dist / hand_scale
# -------- Volume Control --------
if pinch_ratio < 0.4:
y = lm[8].y
smooth_y = vol_filter.filter(current_time, y)
delta = previous_y - smooth_y
if abs(delta) > 0.015:
if delta > 0:
keyboard.tap(Key.media_volume_up)
cv2.putText(frame, "VOL UP", (50,50), 1, 1.5, (0,255,0),2)
else:
keyboard.tap(Key.media_volume_down)
cv2.putText(frame, "VOL DOWN", (50,50), 1, 1.5, (0,0,255),2)
previous_y = smooth_y
# -------- Gestures --------
elif current_time - last_gesture_time > GESTURE_COOLDOWN:
if total == 5:
keyboard.tap(Key.media_play_pause)
last_gesture_time = current_time
cv2.putText(frame, "PLAY", (50,50), 1, 2, (0,255,0),2)
elif total == 0:
keyboard.tap(Key.media_play_pause)
last_gesture_time = current_time
cv2.putText(frame, "PAUSE", (50,50), 1, 2, (0,0,255),2)
elif fingers[1] and fingers[2] and not fingers[3] and not fingers[4]:
keyboard.tap(Key.media_volume_mute)
last_gesture_time = current_time
cv2.putText(frame, "MUTE", (50,50), 1, 2, (255,255,0),2)
cv2.imshow("Touchless Media Controller", frame)
if cv2.waitKey(1) & 0xFF == 27:
break
camera.stop()
cv2.destroyAllWindows()