Advertisement
rhandycan1

VOLUME CONTROL VER2

Jun 14th, 2025
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | Source Code | 0 0
  1. # pycaw library : https://github.com/AndreMiras/pycaw
  2.  
  3. import cv2
  4. import time
  5. import numpy as np
  6. import handtracingModule as htm
  7. import math
  8. from ctypes import cast, POINTER
  9. from comtypes import CLSCTX_ALL
  10. from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
  11.  
  12. ################################
  13. wCam, hCam = 640, 480
  14. ################################
  15.  
  16. # Configure camera
  17. cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
  18. cap.set(cv2.CAP_PROP_FRAME_WIDTH, wCam)
  19. cap.set(cv2.CAP_PROP_FRAME_HEIGHT, hCam)
  20. cap.set(cv2.CAP_PROP_FPS, 30)
  21. cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))  # Setting codec
  22.  
  23. pTime = 0
  24.  
  25. detector = htm.handDetector(detectionCon=0.7)
  26.  
  27. devices = AudioUtilities.GetSpeakers()
  28. interface = devices.Activate(
  29.     IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
  30. volume = cast(interface, POINTER(IAudioEndpointVolume))
  31. volRange = volume.GetVolumeRange()
  32. minVol = volRange[0]
  33. maxVol = volRange[1]
  34. vol = 0
  35. volPer = 0
  36.  
  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.         # Get the coordinates of the index finger tip
  43.         x1, y1 = lmList[8][1], lmList[8][2]
  44.  
  45.         # Calculate the volume percentage based on the x-coordinate of the index finger
  46.         volPer = np.interp(x1, [80, 550], [0, 100])
  47.         vol = np.interp(x1, [80, 550], [minVol, maxVol])
  48.         volume.SetMasterVolumeLevel(vol, None)
  49.  
  50.         # Print the volume percentage and volume level for calibration
  51.         print(f'Volume Percentage: {volPer} %')
  52.         print(f'Volume Level: {vol}')
  53.  
  54.         # Calculate the width of the filled rectangle based on the volume percentage
  55.         fillWidth = int(np.interp(volPer, [0, 100], [80, 550]))
  56.  
  57.         # Draw the filled rectangle
  58.         cv2.rectangle(img, (80, 400), (fillWidth, 420), (0, 255, 0), -1)
  59.  
  60.         # Draw the outline of the volume control rectangle
  61.         cv2.rectangle(img, (80, 400), (550, 420), (0, 153, 76), 1)
  62.  
  63.         # Draw the volume icon
  64.         cv2.rectangle(img, (30, 405), (50, 415), (0, 0, 0), -1)  # Speaker box
  65.         cv2.line(img, (50, 410), (60, 400), (0, 0, 0), 2)  # Speaker cone
  66.         cv2.line(img, (50, 410), (60, 420), (0, 0, 0), 2)  # Speaker cone
  67.  
  68.         # Display the volume percentage
  69.         cv2.putText(img, f'{int(volPer)} %', (50, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 2)
  70.  
  71.     #fps
  72.     cTime = time.time()
  73.     fps = 1 / (cTime - pTime)
  74.     pTime = cTime
  75.     cv2.putText(img, f'FPS: {int(fps)}', (40, 50), cv2.FONT_HERSHEY_COMPLEX,
  76.                 1, (255, 0, 0), 3)
  77.  
  78.     # Display the image
  79.     cv2.imshow("Image", img)
  80.     if cv2.waitKey(1) & 0xFF == ord('q'):
  81.         break
  82.  
  83. cap.release()
  84. cv2.destroyAllWindows()
  85.  
Tags: python OpenCV
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement