Skip to content

Commit bbc45ab

Browse files
committed
update
1 parent 3072b85 commit bbc45ab

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

Gesture Volume Control/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 🎚️ Gesture Volume Control
2+
3+
Control your system volume with hand gestures using your webcam!
4+
This project uses **OpenCV**, **MediaPipe**, and **pycaw** to detect your hand in real time and map the distance between your thumb and index finger to system volume.
5+
6+
## ⚙️ Requirements
7+
```bash
8+
pip install opencv-python mediapipe pycaw comtypes
9+
```
10+
11+
Check out my github: [10mudassir007](https://github.com/10mudassir007)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import cv2
2+
import mediapipe as mp
3+
from ctypes import cast, POINTER
4+
from comtypes import CLSCTX_ALL
5+
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
6+
import math
7+
8+
# Mediapipe hands
9+
mp_hands = mp.solutions.hands
10+
hands = mp_hands.Hands(max_num_hands=1)
11+
mp_draw = mp.solutions.drawing_utils
12+
13+
# Audio setup
14+
devices = AudioUtilities.GetSpeakers()
15+
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
16+
volume = cast(interface, POINTER(IAudioEndpointVolume))
17+
vol_range = volume.GetVolumeRange() # minVol, maxVol, step
18+
19+
# Phone camera
20+
cap = cv2.VideoCapture(0)
21+
22+
while True:
23+
ret, frame = cap.read()
24+
if not ret:
25+
break
26+
frame = cv2.convertScaleAbs(frame, alpha=1.2, beta=30)
27+
frame = cv2.flip(frame, 1)
28+
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
29+
results = hands.process(rgb)
30+
31+
# Resize to 360x360
32+
frame = cv2.resize(frame, (640, 480))
33+
34+
if results.multi_hand_landmarks:
35+
for handLms in results.multi_hand_landmarks:
36+
mp_draw.draw_landmarks(frame, handLms, mp_hands.HAND_CONNECTIONS)
37+
38+
# Thumb and index finger tips
39+
thumb_tip = handLms.landmark[mp_hands.HandLandmark.THUMB_TIP]
40+
index_tip = handLms.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP]
41+
42+
h, w, _ = frame.shape
43+
x1, y1 = int(thumb_tip.x * w), int(thumb_tip.y * h)
44+
x2, y2 = int(index_tip.x * w), int(index_tip.y * h)
45+
46+
cv2.circle(frame, (x1, y1), 8, (255, 0, 0), cv2.FILLED)
47+
cv2.circle(frame, (x2, y2), 8, (255, 0, 0), cv2.FILLED)
48+
cv2.line(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
49+
50+
# Distance between fingers → volume
51+
length = math.hypot(x2 - x1, y2 - y1)
52+
min_vol, max_vol, _ = vol_range
53+
vol = (length / 200) * (max_vol - min_vol) + min_vol
54+
vol = max(min(vol, max_vol), min_vol)
55+
volume.SetMasterVolumeLevel(vol, None)
56+
57+
cv2.imshow("Hand Volume Control", frame)
58+
if cv2.waitKey(1) & 0xFF == ord("q"):
59+
break
60+
61+
cap.release()
62+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)