Skip to content

Commit 9f518b1

Browse files
authored
Merge pull request #335 from lisa1612/gesture-controlled-volume
Add GestureControlledVolume script
2 parents 790ab4d + 6d67c80 commit 9f518b1

File tree

3 files changed

+233
-0
lines changed

3 files changed

+233
-0
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "bbcc9707",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"conda install opencv"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"id": "9503238e",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"import cv2"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"id": "e7af804f",
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"import mediapipe as mp"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"id": "953e7623",
37+
"metadata": {},
38+
"outputs": [],
39+
"source": [
40+
"import math "
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": null,
46+
"id": "889f973c",
47+
"metadata": {},
48+
"outputs": [],
49+
"source": [
50+
"pip install pycaw"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": null,
56+
"id": "0bbc1460",
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"from ctypes import cast, POINTER\n",
61+
"from comtypes import CLSCTX_ALL\n",
62+
"from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": null,
68+
"id": "fb4f9175",
69+
"metadata": {},
70+
"outputs": [],
71+
"source": [
72+
"import numpy as np"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"id": "f47550a4",
79+
"metadata": {},
80+
"outputs": [],
81+
"source": [
82+
"devices = AudioUtilities.GetSpeakers()\n",
83+
"interface = devices.Activate(\n",
84+
" IAudioEndpointVolume._iid_, CLSCTX_ALL, None)\n",
85+
"volume = cast(interface, POINTER(IAudioEndpointVolume))"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"id": "0f9c586f",
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"#volume.GetMute()\n",
96+
"#print(volume.GetMasterVolumeLevel())\n",
97+
"#print(volume.GetVolumeRange())\n"
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": null,
103+
"id": "3a7cc9ac",
104+
"metadata": {},
105+
"outputs": [],
106+
"source": [
107+
"cap = cv2.VideoCapture(0)\n",
108+
"mpHands = mp.solutions.hands\n",
109+
"hands = mpHands.Hands()\n",
110+
"mpDraw = mp.solutions.drawing_utils\n",
111+
"\n",
112+
"\n",
113+
"while True:\n",
114+
" success,img = cap.read()\n",
115+
" imgRGB = cv2.cvtColor(img , cv2.COLOR_BGR2RGB)\n",
116+
" results= hands.process(imgRGB)\n",
117+
" #print(results.multi_hand_landmarks)\n",
118+
" \n",
119+
" if results.multi_hand_landmarks:\n",
120+
" for handLms in results.multi_hand_landmarks:\n",
121+
" lmList =[]\n",
122+
" for id, lm in enumerate (handLms.landmark):\n",
123+
" #print(id, lm)\n",
124+
" h , w , c = img.shape\n",
125+
" cx , cy = int(lm.x*w), int(lm.y*h)\n",
126+
" #print(id, cx, cy)\n",
127+
" lmList.append([id, cx, cy])\n",
128+
" #print(lmList)\n",
129+
" mpDraw.draw_landmarks(img,handLms,mpHands.HAND_CONNECTIONS)\n",
130+
" \n",
131+
" if lmList:\n",
132+
" x1,y1 = lmList[4][1],lmList[4][2]\n",
133+
" x2,y2 = lmList[8][1],lmList[8][2]\n",
134+
" \n",
135+
" cv2.circle(img,(x1,y1),10,(255,0,9),cv2.FILLED)\n",
136+
" cv2.circle(img,(x2,y2),10,(255,0,9),cv2.FILLED)\n",
137+
" cv2.line(img,(x1,y1),(x2,y2),(134,1,175),3)\n",
138+
" \n",
139+
" length = math.hypot(x2-x1,y2-y1)\n",
140+
" print(length)\n",
141+
" \n",
142+
" if length <50:\n",
143+
" z1 = (x1+x2)//2\n",
144+
" z2 = (y1+y2)//2\n",
145+
" cv2.circle(img,(z1,z2),10,(255,0,9),cv2.FILLED)\n",
146+
" \n",
147+
" volRange = volume.GetVolumeRange()\n",
148+
" minVol = volRange[0]\n",
149+
" maxVol = volRange[1]\n",
150+
" vol = np.interp(length,[50,300],[minVol,maxVol])\n",
151+
" volBar = np.interp(length, [50,300],[400,150])\n",
152+
" volPer = np.interp(length, [50,300],[0,100])\n",
153+
" volume.SetMasterVolumeLevel(vol,None)\n",
154+
" cv2.rectangle(img,(50,150),(85,400),(191,62,255),3)\n",
155+
" cv2.rectangle(img,(50,int(volBar)),(85,400),(191,62,255),cv2.FILLED)\n",
156+
" cv2.putText(img,str(int(volPer)),(40,100),cv2.FONT_HERSHEY_DUPLEX,4,(126,58,234))\n",
157+
" \n",
158+
" \n",
159+
" cv2.imshow(\"Image\",img) \n",
160+
" cv2.waitKey(1)"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": null,
166+
"id": "f5735e95",
167+
"metadata": {},
168+
"outputs": [],
169+
"source": [
170+
"#length 50-300\n",
171+
"#volRange -24 to +21"
172+
]
173+
}
174+
],
175+
"metadata": {
176+
"kernelspec": {
177+
"display_name": "Python 3",
178+
"language": "python",
179+
"name": "python3"
180+
},
181+
"language_info": {
182+
"codemirror_mode": {
183+
"name": "ipython",
184+
"version": 3
185+
},
186+
"file_extension": ".py",
187+
"mimetype": "text/x-python",
188+
"name": "python",
189+
"nbconvert_exporter": "python",
190+
"pygments_lexer": "ipython3",
191+
"version": "3.8.8"
192+
}
193+
},
194+
"nbformat": 4,
195+
"nbformat_minor": 5
196+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Introduction
2+
3+
Gesture Controlled Volume is an interactive Python project that allows you to control your computer's audio volume using hand gestures detected by your camera. It combines computer vision, hand tracking, and audio control to provide a unique and intuitive way to adjust audio settings.
4+
5+
## Features
6+
7+
- Real-time hand tracking using the MediaPipe library.
8+
- Interactive volume control based on the distance between two fingers.
9+
- Visual feedback on the video feed for a user-friendly experience.
10+
11+
## Requirements
12+
13+
To run Gesture Controlled Volume, you need the following:
14+
15+
- Python 3.x
16+
- OpenCV
17+
- MediaPipe
18+
- NumPy
19+
- pycaw
20+
21+
You can install these dependencies using the provided `requirements.txt` file.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# requirements.txt
2+
3+
# Specify the required Python version
4+
python_version >= 3.6
5+
6+
# OpenCV for computer vision
7+
opencv-python==4.5.3
8+
9+
# MediaPipe for hand tracking
10+
mediapipe==0.8.3
11+
12+
# NumPy for numerical operations
13+
numpy==1.21.2
14+
15+
# pycaw for audio control
16+
pycaw==2021.6.24

0 commit comments

Comments
 (0)