1
+ """ I have used pycaw by Andre Miras at https://github.com/AndreMiras/pycaw
2
+ To control audio. It is an open-source module which can be installed by --> pip install pycaw
3
+ """
4
+ import math
5
+ import cv2
6
+ import time
7
+ import numpy as np
8
+ import HTrack as ht
9
+ from comtypes import CLSCTX_ALL
10
+ from pycaw .pycaw import AudioUtilities , IAudioEndpointVolume
11
+
12
+ height = 720 # height of webwindow
13
+ width = 1080 # width of webwindow
14
+ cap = cv2 .VideoCapture (0 ) # using default system web cam 0
15
+ cap .set (3 , width ) # 3 is a index specifying width
16
+ cap .set (4 , height ) # 4 is a index specifying height
17
+ prevTime = 0
18
+ detector = ht .handDetector (detectionCon = 0.7 ) # we need to detect hand more precisely
19
+ devices = AudioUtilities .GetSpeakers () # instantiating system speakers as objects
20
+ interface = devices .Activate (IAudioEndpointVolume ._iid_ , CLSCTX_ALL , None ) # activating the interface to control volume
21
+ volume = interface .QueryInterface (IAudioEndpointVolume )
22
+ rangeVol = volume .GetVolumeRange () # volume range for my system is found to be -96 to 0
23
+ volLeast = rangeVol [0 ]
24
+ volHigh = rangeVol [1 ]
25
+ vol = volLeast #initialising volume to least volume
26
+ volBar = 400 #initialising volume bar
27
+ while True :
28
+ success , img = cap .read () # reading the image
29
+ img = detector .drawHands (img ) # detecting the hands
30
+ lmList = detector .getPositions (img ) # finding landmarks of detected hands
31
+ if len (lmList )!= 0 :
32
+ # print(lmList[8],lmList[12])
33
+ x1 ,y1 = lmList [8 ][1 ],lmList [8 ][2 ]
34
+ x2 ,y2 = lmList [12 ][1 ],lmList [12 ][2 ]
35
+ cx ,cy = (x1 + x2 )// 2 ,(y1 + y2 )// 2
36
+ cv2 .circle (img ,(x1 ,y1 ),10 ,(170 ,153 ,255 ),cv2 .FILLED ) # drawing circle around index finger
37
+ cv2 .circle (img ,(x2 ,y2 ),10 ,(170 ,153 ,255 ),cv2 .FILLED ) # drawing circle around middle finger
38
+ cv2 .circle (img ,(cx ,cy ),10 ,(170 ,153 ,255 ),cv2 .FILLED ) # drawing circle around center point of both fingers
39
+ cv2 .line (img ,(x1 ,y1 ),(x2 ,y2 ),(25 ,102 ,180 ),2 )
40
+ distance = math .dist ([x1 ,y1 ],[x2 ,y2 ])
41
+ # print(distance) # range of distance found to be 25 and 150
42
+ vol = np .interp (distance ,[25 ,150 ],[volLeast ,volHigh ]) # To interpolate distance between index and middle fingers to the volume of system
43
+ volBar = np .interp (distance ,[25 ,150 ],[400 ,100 ]) # To interpolate distance between index and middle fingers to the volume bar length
44
+ volume .SetMasterVolumeLevel (vol , None )
45
+ print (int (distance ),vol )
46
+ if distance < 25 :
47
+ cv2 .circle (img ,(cx ,cy ),10 ,(0 ,0 ,120 ),cv2 .FILLED ) # indicating volume is 0 bby changing color of center circle
48
+ #drawing volume bar
49
+ cv2 .rectangle (img ,(80 ,100 ),(60 ,400 ),(230 ,153 ,0 ),2 ) # outer rectangle
50
+ cv2 .rectangle (img ,(80 ,int (volBar )),(60 ,400 ),(150 ,153 ,0 ),cv2 .FILLED ) # actual volume
51
+ cv2 .putText (img ,"Volume" ,(40 ,420 ),cv2 .FONT_HERSHEY_SIMPLEX ,0.5 ,(149 ,100 ,149 ),2 )
52
+
53
+ currTime = time .time ()
54
+ fps = 1 / (currTime - prevTime ) # using time to get timestamp of previous iteration and current iteration to know the frames per second
55
+ prevTime = currTime
56
+ cv2 .putText (img , str (int (fps )), (30 , 40 ), cv2 .FONT_HERSHEY_PLAIN , 2 , (255 , 255 , 0 ), 3 ) # writing FPS on screen
57
+ cv2 .imshow ("Image" , img )
58
+ cv2 .waitKey (1 )
59
+
0 commit comments