Skip to content

Commit 98796bd

Browse files
added gesture control
1 parent a79996f commit 98796bd

File tree

5 files changed

+280
-0
lines changed

5 files changed

+280
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import cv2
2+
import mediapipe as mp
3+
import Hand_detection_module as hdm
4+
import numpy as np
5+
import time
6+
import math
7+
from comtypes import CLSCTX_ALL
8+
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
9+
10+
devices = AudioUtilities.GetSpeakers()
11+
interface = devices.Activate(
12+
IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
13+
volume = interface.QueryInterface(IAudioEndpointVolume)
14+
# volume.GetMute()
15+
# volume.GetMasterVolumeLevel()
16+
17+
vol = volume.GetVolumeRange()
18+
19+
20+
vMin = vol[0]
21+
vMax = vol[1]
22+
23+
24+
25+
26+
########################
27+
wCam ,hCam = 720 , 620
28+
########################
29+
30+
31+
cap = cv2.VideoCapture(0)
32+
cap.set(3 , wCam)
33+
cap.set(4,hCam)
34+
pTime = 0
35+
per = 0
36+
37+
delector = hdm.HandDetection(min_detection_confidence= .6)
38+
39+
while True:
40+
rec , frame = cap.read()
41+
42+
frame = delector.findHand(frame )
43+
lmlist = delector.findPosition(frame , draw= False)
44+
if len(lmlist) != 0:
45+
46+
print(lmlist[4] , lmlist[8])
47+
x1, y1 = lmlist[4][1] , lmlist[4][2]
48+
x2, y2 = lmlist[8][1] , lmlist[8][2]
49+
cv2.circle(frame , (x1, y1) ,15 , (255, 0 ,255) , -1 )
50+
cv2.circle(frame , (x2, y2) ,15 , (255, 0 ,255) , -1 )
51+
cv2.line(frame , (x1, y1) , (x2 , y2) , (255, 0 , 255), 3)
52+
cx , cy = (x1 + x2)//2 , (y1+y2)//2
53+
cv2.circle(frame , (cx , cy) ,15 , (255, 0 ,255) , -1 )
54+
55+
dis = math.hypot(x2-x1 , y2 - y1)
56+
# print(dis)
57+
# range of dis = ( 50 , 300)
58+
59+
finalVol = np.interp(dis , [50 , 280] , [vMin , vMax])
60+
height = np.interp(dis , [50 , 280] , [400 , 150])
61+
vol = np.interp(dis , [50 , 280] , [0 , 100])
62+
volume.SetMasterVolumeLevel(finalVol, None)
63+
64+
print(finalVol)
65+
66+
cv2.rectangle(frame , (50 , 150) , (85 , 400) , (0, 255, 0) , 3)
67+
68+
cv2.rectangle(frame , (50 , int(height)) , (85 , 400) , (0, 256 , 0) , -1)
69+
cv2.putText(frame , f'{str(int(vol))} %' , (48 , 458) , cv2.FONT_HERSHEY_COMPLEX , 1 , (0, 256 , 0) , 2 )
70+
71+
72+
73+
74+
75+
if dis < 50:
76+
cv2.circle(frame , (cx , cy) ,15 , (0, 0 ,255) , -1 )
77+
78+
if dis > 280:
79+
cv2.circle(frame , (cx , cy) ,15 , (0, 255 ,0) , -1 )
80+
81+
82+
83+
84+
85+
86+
cTime = time.time()
87+
fps = 1/(cTime - pTime)
88+
pTime = cTime
89+
90+
cv2.putText(frame , f'FPS : {str(int(fps))}' , (10 , 40) , cv2.FONT_HERSHEY_COMPLEX , 1 , (0, 255 , 0) , 2 )
91+
92+
cv2.imshow("webcam" , frame)
93+
if cv2.waitKey(1) & 0xFF == ord('x'):
94+
break
95+
96+
cap.release()
97+
cv2.destroyAllWindows()
98+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import cv2
2+
import time
3+
import mediapipe as mp
4+
5+
# mode = False , maxHands = 2 , detectionCon = 0.5 , TrackCon = 0.5
6+
class HandDetection:
7+
def __init__(self , min_detection_confidence = 0.5):
8+
# self.mode = mode
9+
# self.maxHand = maxHands
10+
self.min_detection_confidence = min_detection_confidence
11+
# self.TrackCon = TrackCon
12+
13+
self.mpHands = mp.solutions.hands
14+
self.hands = self.mpHands.Hands( self.min_detection_confidence )
15+
self.mpDraw = mp.solutions.drawing_utils
16+
17+
18+
def findHand(self , frame , flag = True):
19+
RGB_frame = cv2.cvtColor(frame , cv2.COLOR_BGR2RGB)
20+
self.results = self.hands.process(RGB_frame)
21+
22+
# print(self.results.multi_hand_landmarks)
23+
24+
if self.results.multi_hand_landmarks:
25+
for multihands in self.results.multi_hand_landmarks:
26+
if flag:
27+
self.mpDraw.draw_landmarks(frame , multihands , self.mpHands.HAND_CONNECTIONS)
28+
29+
return frame
30+
31+
def findPosition(self , frame , handno = 0 , draw = True):
32+
33+
lmList = []
34+
35+
if self.results.multi_hand_landmarks:
36+
myHand = self.results.multi_hand_landmarks[handno]
37+
38+
for id , lm in enumerate(myHand.landmark):
39+
h , w , c = frame.shape
40+
cx , cy = int(lm.x*w) , int(lm.y*h)
41+
# print(id , cx , cy)
42+
lmList.append([id , cx , cy])
43+
44+
if draw:
45+
46+
cv2.circle(frame , (cx , cy) , 7 , (255 , 0 , 9) , cv2.FILLED)
47+
48+
return lmList
49+
50+
51+
def main():
52+
pTime = 0
53+
cTime = 0
54+
55+
56+
cap = cv2.VideoCapture(0)
57+
58+
detector = HandDetection()
59+
60+
61+
while True:
62+
63+
rec , frame = cap.read()
64+
65+
frame = detector.findHand(frame)
66+
lmlist = detector.findPosition(frame)
67+
68+
if len(lmlist) != 0:
69+
print(lmlist[4])
70+
71+
cTime =time.time()
72+
Fps = 1/(cTime - pTime)
73+
pTime = cTime
74+
75+
cv2.putText(frame , str(int(Fps)) , (10 , 40) , cv2.FONT_HERSHEY_COMPLEX , 1 , (0,255 , 0) , 2 )
76+
77+
78+
79+
cv2.imshow("webcam" , frame)
80+
if cv2.waitKey(1) & 0xFF == ord("x"):
81+
break
82+
83+
cap.release()
84+
cv2.destroyAllWindows()
85+
86+
if __name__ == "__main__":
87+
main()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import mediapipe as mp
2+
import cv2
3+
import numpy as np
4+
import time
5+
6+
mpHands = mp.solutions.hands
7+
hands = mpHands.Hands()
8+
mpDraw = mp.solutions.drawing_utils
9+
10+
pTime = 0
11+
cTime = 0
12+
13+
14+
cap = cv2.VideoCapture(0)
15+
16+
while True:
17+
18+
rec , frame = cap.read()
19+
20+
gray_frame = cv2.cvtColor(frame , cv2.COLOR_BGR2RGB)
21+
results = hands.process(gray_frame)
22+
23+
print(results.multi_hand_landmarks)
24+
25+
if results.multi_hand_landmarks:
26+
for multihands in results.multi_hand_landmarks:
27+
for id , lm in enumerate(multihands.landmark):
28+
h , w , c = frame.shape
29+
cx , cy = int(lm.x*w) , int(lm.y*h)
30+
print(id , cx , cy)
31+
32+
if id == 4:
33+
cv2.circle(frame , (cx , cy) , 15 , (255 , 255 , 9) , cv2.FILLED)
34+
35+
mpDraw.draw_landmarks(frame , multihands , mpHands.HAND_CONNECTIONS)
36+
37+
cTime =time.time()
38+
Fps = 1/(cTime - pTime)
39+
pTime = cTime
40+
41+
cv2.putText(frame , str(int(Fps)) , (10 , 40) , cv2.FONT_HERSHEY_COMPLEX , 1 , (0,255 , 0) , 2 )
42+
43+
44+
45+
cv2.imshow("webcam" , frame)
46+
if cv2.waitKey(1) & 0xFF == ord("x"):
47+
break
48+
49+
cap.release()
50+
cv2.destroyAllWindows()
51+
52+
2.05 KB
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Gesture Control System
2+
3+
## Overview
4+
The Gesture Control System allows users to control the volume of their computer using hand gestures. This is achieved by detecting the distance between the thumb and index finger using a webcam, and then mapping this distance to a volume level. The system is implemented using OpenCV for video capture, MediaPipe for hand detection, and the Pycaw library for controlling the system audio.
5+
6+
## Features
7+
- **Hand Detection:** Utilizes MediaPipe's hand detection module to detect and track the position of the hand in real-time.
8+
- **Gesture Control:** Calculates the distance between the thumb and index finger and maps this distance to control the system's audio volume.
9+
- **Visual Feedback:** Provides real-time visual feedback of the hand positions and the current volume level on the webcam feed.
10+
11+
## Requirements
12+
- Python 3.x
13+
- OpenCV
14+
- MediaPipe
15+
- Numpy
16+
- Pycaw
17+
- Comtypes
18+
19+
To install the required libraries, you can use the following pip command:
20+
```bash
21+
pip install opencv-python mediapipe numpy pycaw comtypes
22+
```
23+
## How to Run
24+
25+
1. Ensure that your system has a working webcam.
26+
2. Install the required libraries as mentioned in the Requirements section.
27+
3. Run the Python script:
28+
```bash
29+
python Gesture_Control.py
30+
```
31+
4. Use your thumb and index finger to control the volume:
32+
- Bring them closer to decrease the volume.
33+
- Move them apart to increase the volume.
34+
35+
5. Press `x` on the keyboard to exit the program.
36+
37+
## Advantages
38+
- **Contactless Control:** Allows users to control volume without any physical contact, making it ideal for environments where hands-free operation is essential.
39+
- **Real-time Operation:** The system operates in real-time, providing immediate feedback and control.
40+
41+
## Limitations
42+
- **Lighting Conditions:** The performance of the hand detection might vary depending on the lighting conditions.
43+
- **Single-Purpose:** The system is designed specifically for volume control; extending it to other applications would require additional development.

0 commit comments

Comments
 (0)