├── README.md └── PROJECT ├── This code is a tutorial on how to build a hand gesture.docx ├── gesture_volume_control.py └── tmp.py /README.md: -------------------------------------------------------------------------------- 1 | # hand_gesture_volume_control 2 | Control your device volume using ML (opencv) 3 | -------------------------------------------------------------------------------- /PROJECT/This code is a tutorial on how to build a hand gesture.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Antoniocolapso/hand_gesture_volume_control/HEAD/PROJECT/This code is a tutorial on how to build a hand gesture.docx -------------------------------------------------------------------------------- /PROJECT/gesture_volume_control.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import mediapipe as mp 3 | from math import hypot 4 | from ctypes import cast, POINTER 5 | from comtypes import CLSCTX_ALL 6 | from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume 7 | import numpy as np 8 | 9 | 10 | 11 | cap = cv2.VideoCapture(0) 12 | 13 | mpHands = mp.solutions.hands 14 | hands = mpHands.Hands() 15 | mpDraw = mp.solutions.drawing_utils 16 | 17 | 18 | 19 | devices = AudioUtilities.GetSpeakers() 20 | interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None) 21 | volume = cast(interface, POINTER(IAudioEndpointVolume)) 22 | 23 | 24 | volMin, volMax = volume.GetVolumeRange()[:2] 25 | 26 | while True: 27 | success, img = cap.read() 28 | imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 29 | results = hands.process(imgRGB) 30 | 31 | lmList = [] 32 | if results.multi_hand_landmarks: 33 | for handlandmark in results.multi_hand_landmarks: 34 | for id, lm in enumerate(handlandmark.landmark): 35 | h, w, c = img.shape 36 | cx, cy = int(lm.x * w), int(lm.y * h) 37 | lmList.append([id, cx, cy]) 38 | mpDraw.draw_landmarks(img, handlandmark, mpHands.HAND_CONNECTIONS) 39 | if lmList != []: 40 | x1, y1 = lmList[4][1], lmList[4][2] 41 | x2, y2 = lmList[8][1], lmList[8][2] 42 | cv2.circle(img, (x1, y1), 15, (255, 0, 0), cv2.FILLED) 43 | cv2.circle(img, (x2, y2), 15, (255, 0, 0), cv2.FILLED) 44 | cv2.line(img, (x1, y1), (x2, y2), (255, 0, 0), 3) 45 | length = hypot(x2 - x1, y2 - y1) 46 | vol = np.interp(length, [15, 220], [volMin, volMax]) 47 | print(vol, length) 48 | volume.SetMasterVolumeLevel(vol, None) 49 | cv2.imshow('Image', img) 50 | if cv2.waitKey(1) & 0xff == ord('q'): 51 | break -------------------------------------------------------------------------------- /PROJECT/tmp.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import mediapipe as mp 3 | from math import hypot 4 | from ctypes import cast, POINTER 5 | from comtypes import CLSCTX_ALL 6 | from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume 7 | import numpy as np 8 | 9 | cap = cv2.VideoCapture(0) #Checks for camera 10 | 11 | mpHands = mp.solutions.hands #detects hand/finger 12 | hands = mpHands.Hands() #complete the initialization configuration of hands 13 | mpDraw = mp.solutions.drawing_utils 14 | 15 | #To access speaker through the library pycaw 16 | devices = AudioUtilities.GetSpeakers() 17 | interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None) 18 | volume = cast(interface, POINTER(IAudioEndpointVolume)) 19 | # volbar=400 20 | # volper=0 21 | 22 | volMin,volMax = volume.GetVolumeRange()[:2] 23 | 24 | while True: 25 | success,img = cap.read() #If camera works capture an image 26 | imgRGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) #Convert to rgb 27 | 28 | #Collection of gesture information 29 | results = hands.process(imgRGB) #completes the image processing. 30 | 31 | lmList = [] #empty list 32 | if results.multi_hand_landmarks: #list of all hands detected. 33 | #By accessing the list, we can get the information of each hand's corresponding flag bit 34 | for handlandmark in results.multi_hand_landmarks: 35 | for id,lm in enumerate(handlandmark.landmark): #adding counter and returning it 36 | # Get finger joint points 37 | h,w,_ = img.shape 38 | cx,cy = int(lm.x*w),int(lm.y*h) 39 | lmList.append([id,cx,cy]) #adding to the empty list 'lmList' 40 | mpDraw.draw_landmarks(img,handlandmark,mpHands.HAND_CONNECTIONS) 41 | 42 | if lmList != []: 43 | #getting the value at a point 44 | #x #y 45 | x1,y1 = lmList[4][1],lmList[4][2] #thumb 46 | x2,y2 = lmList[8][1],lmList[8][2] #index finger 47 | #creating circle at the tips of thumb and index finger 48 | cv2.circle(img,(x1,y1),13,(255,0,0),cv2.FILLED) #image #fingers #radius #rgb 49 | cv2.circle(img,(x2,y2),13,(255,0,0),cv2.FILLED) #image #fingers #radius #rgb 50 | cv2.line(img,(x1,y1),(x2,y2),(255,0,0),3) #create a line b/w tips of index finger and thumb 51 | 52 | length = hypot(x2-x1,y2-y1) #distance b/w tips using hypotenuse 53 | # from numpy we find our length,by converting hand range in terms of volume range ie b/w -63.5 to 0 54 | vol = np.interp(length,[30,350],[volMin,volMax]) 55 | volbar=np.interp(length,[30,350],[400,150]) 56 | volper=np.interp(length,[30,350],[0,100]) 57 | 58 | 59 | print(vol,int(length)) 60 | volume.SetMasterVolumeLevel(vol, None) 61 | 62 | # Hand range 30 - 350 63 | # Volume range -63.5 - 0.0 64 | #creating volume bar for volume level 65 | cv2.rectangle(img,(50,150),(85,400),(0,0,255),4) # vid ,initial position ,ending position ,rgb ,thickness 66 | cv2.rectangle(img,(50,int(volbar)),(85,400),(0,0,255),cv2.FILLED) 67 | cv2.putText(img,f"{int(volper)}%",(10,40),cv2.FONT_ITALIC,1,(0, 255, 98),3) 68 | #tell the volume percentage ,location,font of text,length,rgb color,thickness 69 | cv2.imshow('Image',img) #Show the video 70 | if cv2.waitKey(1) & 0xff==ord(' '): #By using spacebar delay will stop 71 | break 72 | 73 | cap.release() #stop cam 74 | cv2.destroyAllWindows() #close window --------------------------------------------------------------------------------