|
| 1 | +import cv2 |
| 2 | +import time |
| 3 | +import numpy as np |
| 4 | +import handTrackingModule as htm |
| 5 | +import math |
| 6 | + |
| 7 | +from ctypes import cast, POINTER |
| 8 | +from comtypes import CLSCTX_ALL |
| 9 | +from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume |
| 10 | + |
| 11 | +################################ |
| 12 | +wCam, hCam = 640, 480 |
| 13 | +################################ |
| 14 | + |
| 15 | +cap = cv2.VideoCapture(0) |
| 16 | +cap.set(3, wCam) |
| 17 | +cap.set(4, hCam) |
| 18 | +pTime = 0 |
| 19 | + |
| 20 | +detector = htm.handDetector(detectionCon=0.7) |
| 21 | + |
| 22 | +devices = AudioUtilities.GetSpeakers() |
| 23 | +interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None) |
| 24 | +volume = cast(interface, POINTER(IAudioEndpointVolume)) |
| 25 | +# volume.GetMute() |
| 26 | +# volume.GetMasterVolumeLevel() |
| 27 | +volRange = volume.GetVolumeRange() |
| 28 | +minVol = volRange[0] |
| 29 | +maxVol = volRange[1] |
| 30 | +vol = 0 |
| 31 | +volBar = 400 |
| 32 | +volPer = 0 |
| 33 | + |
| 34 | +while True: |
| 35 | + success, img = cap.read() |
| 36 | + img = detector.findHands(img) |
| 37 | + lmList = detector.findPosition(img, draw=False) |
| 38 | + if len(lmList) != 0: |
| 39 | + # print(lmList[4], lmList[8]) |
| 40 | + x1, y1 = lmList[4][1], lmList[4][2] |
| 41 | + x2, y2 = lmList[8][1], lmList[8][2] |
| 42 | + |
| 43 | + cv2.circle(img, (x1, y1), 15, (0, 255, 255), cv2.FILLED) |
| 44 | + cv2.circle(img, (x2, y2), 15, (0, 255, 255), cv2.FILLED) |
| 45 | + cv2.line(img, (x1, y1), (x2, y2), (100, 255, 255), 3) |
| 46 | + cx, cy = (x1 + x2) // 2, (y1 + y2) // 2 |
| 47 | + cv2.circle(img, (cx, cy), 10, (0, 255, 255), cv2.FILLED) |
| 48 | + |
| 49 | + length = math.hypot(x2 - x1, y2 - y1) |
| 50 | + # print(length) |
| 51 | + |
| 52 | + #Hand range 50 - 260 |
| 53 | + #volume range -63.5 to 0 |
| 54 | + vol = np.interp(length, [50,260], [minVol, maxVol]) |
| 55 | + volBar = np.interp(length, [50,270], [400, 150]) |
| 56 | + volPer = np.interp(length, [50, 270], [0,100]) |
| 57 | + print(vol) |
| 58 | + volume.SetMasterVolumeLevel(vol, None) |
| 59 | + |
| 60 | + if length < 50: |
| 61 | + cv2.circle(img, (cx, cy), 10, (0, 255, 0), cv2.FILLED) |
| 62 | + |
| 63 | + cv2.rectangle(img, (50,150), (85,400), (0,255,0), 3) |
| 64 | + cv2.rectangle(img, (50,int(volBar)), (85,400), (0,255,0), cv2.FILLED) |
| 65 | + cv2.putText(img, f'FPS: {int(volPer)} %', (40, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (255,0 , 0), 2) |
| 66 | + |
| 67 | + |
| 68 | + cTime = time.time() |
| 69 | + fps = 1 / (cTime - pTime) |
| 70 | + pTime = cTime |
| 71 | + |
| 72 | + cv2.putText(img, f'FPS: {int(fps)}', (30, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2) |
| 73 | + cv2.imshow("Img", img) |
| 74 | + cv2.waitKey(1) |
0 commit comments