Volume Control Using Hand Gesture with Python
Control your computer's volume using hand gestures with Python
Blog Post by Jerwell Savella - Published at 3/10/22023
Have you ever wondered if you could control the volume of your feelings for something or even someone with just a wave of your hand? Well, while I can't help you with matters of the heart, I can show you how I used Python to create a volume controller that responds to hand gestures. With this program, you can now adjust the volume of your favorite songs or videos, all without ever touching a button. It's almost like the perfect metaphor for trying to control your emotions for something you can't really have or even someone who doesn't feel the same way. You can adjust the intensity of your feelings, but ultimately, you can't change how the other person feels (jk :3 haha). Why not channel that energy into something creative, like building your own volume controller?
Step 1: Import the required libraries.
The first step is to import the necessary libraries required for the program. In this case, we will be using OpenCV, Mediapipe, Math, ctypes, PyCAW, and NumPy libraries.
```python
import cv2
import mediapipe as mp from math
import hypot from ctypes
import cast, POINTER from comtypes
import CLSCTX_ALL from pycaw.pycaw
import AudioUtilities, IAudioEndpointVolume
import numpy as np
```
Step 2: Create a HandVolumeController class.
Next, we create a class called HandVolumeController, which will contain the functions required for hand gesture volume control. We initialize the class by creating a video capture object and loading the required Mediapipe models.
```python
class HandVolumeController:
def __init__(self):
self.cap = cv2.VideoCapture(0)
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands()
self.mpDraw = mp.solutions.drawing_utils
```
Step 3: Access the speaker.
To access the speaker through the library PyCAW, we first get the speakers and then activate the audio endpoint volume interface.
```python
#To access speaker through the library pycaw
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
self.volume = cast(interface, POINTER(IAudioEndpointVolume)) self.volbar = 400 self.volper = 0
```
Step 4: Define the volume range.
We define the minimum and maximum values of the volume range, which is required for the hand gesture volume control.
```python
self.volMin,self.volMax = self.volume.GetVolumeRange()[:2]
self.volTextPos = (30, 110)
self.volBarStart = (50, 150)
self.volBarEnd = (85,400)
```
Step 5: Process the video feed.
We process the video feed using OpenCV and Mediapipe libraries. We use the landmarks generated by Mediapipe to track the hand movements.
```python
def run(self):
while True:
success,img = self.cap.read()
img = cv2.flip(img, 1)
imgRGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
results = self.hands.process(imgRGB)
```
Step 6: Calculate hand gesture length.
We calculate the length of the hand gesture, which is used to determine the volume level. We use the numpy.interp function to map the hand gesture length to the volume range.
```python
length = hypot(x2-x1,y2-y1)
vol = np.interp(length,[30,100],[self.volMin,self.volMax])
self.volbar = np.interp(length,[30,150],[400,150])
self.volper = np.interp(length,[30,150],[0,100])
```
Step 7: Update the volume level.
We update the volume level based on the hand gesture length using the PyCAW library.
```python
self.volume.SetMasterVolumeLevel(vol, None)
```
Step 8: Display the volume level.
We display the volume level on the screen by drawing a rectangle representing the volume bar and printing the volume percentage on the screen.
```python
cv2.rectangle(img, self.volBarStart, self.volBarEnd, (0,0,255), 4) cv2.rectangle(img, (self.volBarStart[0], int(self.volbar)), self.volBarEnd, (0,0,255), cv2.FILLED)
cv2.putText(img, f"{int(self.volper)}%", self.volTextPos, cv2.FONT_ITALIC, 1, (0, 255, 98), 3)
```
Step 9: Release the video capture object.
We release the video capture object and close all windows when the program is stopped.
That's it! These are the steps to create a hand gesture volume control using Python.