From 98796bded77baf15d03172c594fc748b359c7658 Mon Sep 17 00:00:00 2001 From: shantnu Date: Thu, 15 Aug 2024 23:18:58 +0530 Subject: [PATCH] added gesture control --- .../Gesture Control .py | 98 ++++++++++++++++++ .../Hand_detection_module.py | 87 ++++++++++++++++ .../Raw code module.py | 52 ++++++++++ .../Hand_detection_module.cpython-38.pyc | Bin 0 -> 2101 bytes Hand Gesture Volume Control/readme.md | 43 ++++++++ 5 files changed, 280 insertions(+) create mode 100644 Hand Gesture Volume Control/Gesture Control .py create mode 100644 Hand Gesture Volume Control/Hand_detection_module.py create mode 100644 Hand Gesture Volume Control/Raw code module.py create mode 100644 Hand Gesture Volume Control/__pycache__/Hand_detection_module.cpython-38.pyc create mode 100644 Hand Gesture Volume Control/readme.md diff --git a/Hand Gesture Volume Control/Gesture Control .py b/Hand Gesture Volume Control/Gesture Control .py new file mode 100644 index 000000000..807e34e97 --- /dev/null +++ b/Hand Gesture Volume Control/Gesture Control .py @@ -0,0 +1,98 @@ +import cv2 +import mediapipe as mp +import Hand_detection_module as hdm +import numpy as np +import time +import math +from comtypes import CLSCTX_ALL +from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume + +devices = AudioUtilities.GetSpeakers() +interface = devices.Activate( + IAudioEndpointVolume._iid_, CLSCTX_ALL, None) +volume = interface.QueryInterface(IAudioEndpointVolume) +# volume.GetMute() +# volume.GetMasterVolumeLevel() + +vol = volume.GetVolumeRange() + + +vMin = vol[0] +vMax = vol[1] + + + + +######################## +wCam ,hCam = 720 , 620 +######################## + + +cap = cv2.VideoCapture(0) +cap.set(3 , wCam) +cap.set(4,hCam) +pTime = 0 +per = 0 + +delector = hdm.HandDetection(min_detection_confidence= .6) + +while True: + rec , frame = cap.read() + + frame = delector.findHand(frame ) + lmlist = delector.findPosition(frame , draw= False) + if len(lmlist) != 0: + + print(lmlist[4] , lmlist[8]) + x1, y1 = lmlist[4][1] , lmlist[4][2] + x2, y2 = lmlist[8][1] , lmlist[8][2] + cv2.circle(frame , (x1, y1) ,15 , (255, 0 ,255) , -1 ) + cv2.circle(frame , (x2, y2) ,15 , (255, 0 ,255) , -1 ) + cv2.line(frame , (x1, y1) , (x2 , y2) , (255, 0 , 255), 3) + cx , cy = (x1 + x2)//2 , (y1+y2)//2 + cv2.circle(frame , (cx , cy) ,15 , (255, 0 ,255) , -1 ) + + dis = math.hypot(x2-x1 , y2 - y1) + # print(dis) + # range of dis = ( 50 , 300) + + finalVol = np.interp(dis , [50 , 280] , [vMin , vMax]) + height = np.interp(dis , [50 , 280] , [400 , 150]) + vol = np.interp(dis , [50 , 280] , [0 , 100]) + volume.SetMasterVolumeLevel(finalVol, None) + + print(finalVol) + + cv2.rectangle(frame , (50 , 150) , (85 , 400) , (0, 255, 0) , 3) + + cv2.rectangle(frame , (50 , int(height)) , (85 , 400) , (0, 256 , 0) , -1) + cv2.putText(frame , f'{str(int(vol))} %' , (48 , 458) , cv2.FONT_HERSHEY_COMPLEX , 1 , (0, 256 , 0) , 2 ) + + + + + + if dis < 50: + cv2.circle(frame , (cx , cy) ,15 , (0, 0 ,255) , -1 ) + + if dis > 280: + cv2.circle(frame , (cx , cy) ,15 , (0, 255 ,0) , -1 ) + + + + + + + cTime = time.time() + fps = 1/(cTime - pTime) + pTime = cTime + + cv2.putText(frame , f'FPS : {str(int(fps))}' , (10 , 40) , cv2.FONT_HERSHEY_COMPLEX , 1 , (0, 255 , 0) , 2 ) + + cv2.imshow("webcam" , frame) + if cv2.waitKey(1) & 0xFF == ord('x'): + break + +cap.release() +cv2.destroyAllWindows() + diff --git a/Hand Gesture Volume Control/Hand_detection_module.py b/Hand Gesture Volume Control/Hand_detection_module.py new file mode 100644 index 000000000..fe6eebb3d --- /dev/null +++ b/Hand Gesture Volume Control/Hand_detection_module.py @@ -0,0 +1,87 @@ +import cv2 +import time +import mediapipe as mp + +# mode = False , maxHands = 2 , detectionCon = 0.5 , TrackCon = 0.5 +class HandDetection: + def __init__(self , min_detection_confidence = 0.5): + # self.mode = mode + # self.maxHand = maxHands + self.min_detection_confidence = min_detection_confidence + # self.TrackCon = TrackCon + + self.mpHands = mp.solutions.hands + self.hands = self.mpHands.Hands( self.min_detection_confidence ) + self.mpDraw = mp.solutions.drawing_utils + + + def findHand(self , frame , flag = True): + RGB_frame = cv2.cvtColor(frame , cv2.COLOR_BGR2RGB) + self.results = self.hands.process(RGB_frame) + + # print(self.results.multi_hand_landmarks) + + if self.results.multi_hand_landmarks: + for multihands in self.results.multi_hand_landmarks: + if flag: + self.mpDraw.draw_landmarks(frame , multihands , self.mpHands.HAND_CONNECTIONS) + + return frame + + def findPosition(self , frame , handno = 0 , draw = True): + + lmList = [] + + if self.results.multi_hand_landmarks: + myHand = self.results.multi_hand_landmarks[handno] + + for id , lm in enumerate(myHand.landmark): + h , w , c = frame.shape + cx , cy = int(lm.x*w) , int(lm.y*h) + # print(id , cx , cy) + lmList.append([id , cx , cy]) + + if draw: + + cv2.circle(frame , (cx , cy) , 7 , (255 , 0 , 9) , cv2.FILLED) + + return lmList + + +def main(): + pTime = 0 + cTime = 0 + + + cap = cv2.VideoCapture(0) + + detector = HandDetection() + + + while True: + + rec , frame = cap.read() + + frame = detector.findHand(frame) + lmlist = detector.findPosition(frame) + + if len(lmlist) != 0: + print(lmlist[4]) + + cTime =time.time() + Fps = 1/(cTime - pTime) + pTime = cTime + + cv2.putText(frame , str(int(Fps)) , (10 , 40) , cv2.FONT_HERSHEY_COMPLEX , 1 , (0,255 , 0) , 2 ) + + + + cv2.imshow("webcam" , frame) + if cv2.waitKey(1) & 0xFF == ord("x"): + break + + cap.release() + cv2.destroyAllWindows() + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/Hand Gesture Volume Control/Raw code module.py b/Hand Gesture Volume Control/Raw code module.py new file mode 100644 index 000000000..184d19a2e --- /dev/null +++ b/Hand Gesture Volume Control/Raw code module.py @@ -0,0 +1,52 @@ +import mediapipe as mp +import cv2 +import numpy as np +import time + +mpHands = mp.solutions.hands +hands = mpHands.Hands() +mpDraw = mp.solutions.drawing_utils + +pTime = 0 +cTime = 0 + + +cap = cv2.VideoCapture(0) + +while True: + + rec , frame = cap.read() + + gray_frame = cv2.cvtColor(frame , cv2.COLOR_BGR2RGB) + results = hands.process(gray_frame) + + print(results.multi_hand_landmarks) + + if results.multi_hand_landmarks: + for multihands in results.multi_hand_landmarks: + for id , lm in enumerate(multihands.landmark): + h , w , c = frame.shape + cx , cy = int(lm.x*w) , int(lm.y*h) + print(id , cx , cy) + + if id == 4: + cv2.circle(frame , (cx , cy) , 15 , (255 , 255 , 9) , cv2.FILLED) + + mpDraw.draw_landmarks(frame , multihands , mpHands.HAND_CONNECTIONS) + + cTime =time.time() + Fps = 1/(cTime - pTime) + pTime = cTime + + cv2.putText(frame , str(int(Fps)) , (10 , 40) , cv2.FONT_HERSHEY_COMPLEX , 1 , (0,255 , 0) , 2 ) + + + + cv2.imshow("webcam" , frame) + if cv2.waitKey(1) & 0xFF == ord("x"): + break + +cap.release() +cv2.destroyAllWindows() + + \ No newline at end of file diff --git a/Hand Gesture Volume Control/__pycache__/Hand_detection_module.cpython-38.pyc b/Hand Gesture Volume Control/__pycache__/Hand_detection_module.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..984f35607dbb6de01a34434cddc9c92da670541e GIT binary patch literal 2101 zcmZuyOK%)S5bnpkcD(C2k+GbFhZQ(5C`1r$C<;kz#}1fvWIH@ohS4zHwr4Xh_q5|U znz^tRzhn1CF8m07MxT&4ApZp}P&F$jR-ji?UHz)A{_3mRC%v9ep#6H<*<0xl@+S^1 zHV%V(@Kq=T5i})(X-@~V9kT&nBZ3KjN(3LVfh8>W;KWgu=;f61Znz zh4f>T7u%~_K7v_E(DHzZnbs;DaM2aEa84=M?g>wHK(mDp_GV3QJ<7$3)H2pdk^eO{ z`o|YByI>x5ykQx>>K2Ha)+8jF4%v`{X4Pb9jct%*f-AgTaX@ng*(NV(Kk&?AmgJ#0 zzbK50d@m6)kELN*X*yMr9-#-7u@Atb%D7pHu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