Advertisement
rhandycan1

MainCode_VolumeControl_GestureControl

Jan 18th, 2025
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | Source Code | 0 0
  1. import cv2
  2. import time
  3. import numpy as np
  4. import handtracingModule as htm
  5. import math
  6. from ctypes import cast, POINTER
  7. from comtypes import CLSCTX_ALL
  8. from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
  9.  
  10. ################################
  11. wCam, hCam = 640, 480
  12. ################################
  13.  
  14. #configure camera
  15. cap=cv2.VideoCapture(0,cv2.CAP_DSHOW)
  16. cap.set(cv2.CAP_PROP_FRAME_WIDTH,wCam)
  17. cap.set(cv2.CAP_PROP_FRAME_HEIGHT,hCam)
  18. cap.set(cv2.CAP_PROP_FPS,30)
  19. cap.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc(*'MJPG'))#setting codec
  20.  
  21. pTime = 0
  22.  
  23. detector = htm.handDetector(detectionCon=0.7)
  24.  
  25. devices = AudioUtilities.GetSpeakers()
  26. interface = devices.Activate(
  27.     IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
  28. volume = cast(interface, POINTER(IAudioEndpointVolume))
  29. # volume.GetMute()
  30. # volume.GetMasterVolumeLevel()
  31. volRange = volume.GetVolumeRange()
  32. minVol = volRange[0]
  33. maxVol = volRange[1]
  34. vol = 0
  35. volBar = 400
  36. volPer = 0
  37. while True:
  38.     success, img = cap.read()
  39.     img = detector.findHands(img)
  40.     lmList = detector.findPosition(img, draw=False)
  41.     if len(lmList) != 0:
  42.         # print(lmList[4], lmList[8])
  43.  
  44.         x1, y1 = lmList[4][1], lmList[4][2]
  45.         x2, y2 = lmList[8][1], lmList[8][2]
  46.         cx, cy = (x1 + x2) // 2, (y1 + y2) // 2
  47.  
  48.         cv2.circle(img, (x1, y1), 10, (255, 0, 255), cv2.FILLED)
  49.         cv2.circle(img, (x2, y2), 10, (255, 0, 255), cv2.FILLED)
  50.         cv2.line(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
  51.         cv2.circle(img, (cx, cy), 10, (255, 0, 255), cv2.FILLED)
  52.  
  53.         length = math.hypot(x2 - x1, y2 - y1)
  54.         print(length)
  55.  
  56.         # Hand range 50 - 300   //17 - 194
  57.         # Volume Range -65 - 0  // 63.5
  58.        
  59.         #VOLUME BAR
  60.         vol = np.interp(length, [17, 194], [minVol, maxVol])
  61.         volBar = np.interp(length, [17, 194], [400, 150])
  62.         volPer = np.interp(length, [17, 194], [0, 100])
  63.         print(int(length), vol)
  64.         volume.SetMasterVolumeLevel(vol, None)
  65.  
  66.         #if circle meets
  67.         if length < 50:
  68.             cv2.circle(img, (cx, cy), 10, (0, 0, 255), cv2.FILLED)
  69.  
  70.     cv2.rectangle(img, (50, 150), (85, 400), (0, 0, 255), 3)
  71.     cv2.rectangle(img, (50, int(volBar)), (85, 400), (0, 0, 255), cv2.FILLED)
  72.     cv2.putText(img, f'{int(volPer)} %', (40, 450), cv2.FONT_HERSHEY_COMPLEX,
  73.                 1, (0, 255, 0), 3)
  74.  
  75.     #fps
  76.     cTime = time.time()
  77.     fps = 1 / (cTime - pTime)
  78.     pTime = cTime
  79.     cv2.putText(img, f'FPS: {int(fps)}', (40, 50), cv2.FONT_HERSHEY_COMPLEX,
  80.                 1, (255, 0, 0), 3)
  81.  
  82.     cv2.imshow("Img", img)
  83.     if cv2.waitKey(1) & 0xff ==ord('q'):
  84.         break
  85. cap.release()
  86. cv2.destroyAllWindows()
  87.  
  88. # from https://github.com/AndreMiras/pycaw
  89. # from comtypes import CLSCTX_ALL
  90. # from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
  91. # devices = AudioUtilities.GetSpeakers()
  92. # interface = devices.Activate(
  93. #     IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
  94. # volume = interface.QueryInterface(IAudioEndpointVolume)
  95. # volume.GetMute()
  96. # volume.GetMasterVolumeLevel()
  97. # volume.GetVolumeRange()
  98. # volume.SetMasterVolumeLevel(-20.0, None)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement