Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # pycaw library : https://github.com/AndreMiras/pycaw
- import cv2
- import time
- import numpy as np
- import handtracingModule as htm
- import math
- from ctypes import cast, POINTER
- from comtypes import CLSCTX_ALL
- from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
- ################################
- wCam, hCam = 640, 480
- ################################
- # Configure camera
- cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
- cap.set(cv2.CAP_PROP_FRAME_WIDTH, wCam)
- cap.set(cv2.CAP_PROP_FRAME_HEIGHT, hCam)
- cap.set(cv2.CAP_PROP_FPS, 30)
- cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG')) # Setting codec
- pTime = 0
- detector = htm.handDetector(detectionCon=0.7)
- devices = AudioUtilities.GetSpeakers()
- interface = devices.Activate(
- IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
- volume = cast(interface, POINTER(IAudioEndpointVolume))
- volRange = volume.GetVolumeRange()
- minVol = volRange[0]
- maxVol = volRange[1]
- vol = 0
- volPer = 0
- while True:
- success, img = cap.read()
- img = detector.findHands(img)
- lmList = detector.findPosition(img, draw=False)
- if len(lmList) != 0:
- # Get the coordinates of the index finger tip
- x1, y1 = lmList[8][1], lmList[8][2]
- # Calculate the volume percentage based on the x-coordinate of the index finger
- volPer = np.interp(x1, [80, 550], [0, 100])
- vol = np.interp(x1, [80, 550], [minVol, maxVol])
- volume.SetMasterVolumeLevel(vol, None)
- # Print the volume percentage and volume level for calibration
- print(f'Volume Percentage: {volPer} %')
- print(f'Volume Level: {vol}')
- # Calculate the width of the filled rectangle based on the volume percentage
- fillWidth = int(np.interp(volPer, [0, 100], [80, 550]))
- # Draw the filled rectangle
- cv2.rectangle(img, (80, 400), (fillWidth, 420), (0, 255, 0), -1)
- # Draw the outline of the volume control rectangle
- cv2.rectangle(img, (80, 400), (550, 420), (0, 153, 76), 1)
- # Draw the volume icon
- cv2.rectangle(img, (30, 405), (50, 415), (0, 0, 0), -1) # Speaker box
- cv2.line(img, (50, 410), (60, 400), (0, 0, 0), 2) # Speaker cone
- cv2.line(img, (50, 410), (60, 420), (0, 0, 0), 2) # Speaker cone
- # Display the volume percentage
- cv2.putText(img, f'{int(volPer)} %', (50, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 2)
- #fps
- cTime = time.time()
- fps = 1 / (cTime - pTime)
- pTime = cTime
- cv2.putText(img, f'FPS: {int(fps)}', (40, 50), cv2.FONT_HERSHEY_COMPLEX,
- 1, (255, 0, 0), 3)
- # Display the image
- cv2.imshow("Image", img)
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- cap.release()
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement