import mediapipe as mp
from math import hypot
import _ctypes
from _ctypes import pointer
import os
from ctypes import *
from comtypes import HRESULT
from pycaw.pycaw import IAudioEndpointVolume
import numpy as np
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
import numpy as np
capture = cv2.VideoCapture(0)
mpHands = mp.solutions.hands
hands = mpHands.Hands()
mp_draw = mp.solutions.drawing_utils
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
volbar = 200
volper = 0
volmin,volmax = volume.GetVolumeRange()[0:2]
while True:
success, img = capture.read()
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
result = hands.process(imgRGB)
llist = []
if result.multi_hand_landmarks:
for handLms in result.multi_hand_landmarks:
for id, lm in enumerate(handLms.landmark):
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
llist.append([id, cx, cy])
mp_draw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
if llist != []:
x1, y1 = llist[4][1], llist[4][2]
x2, y2 = llist[8][1], llist[8][2]
cv2.circle(img, (x1, y1), 15, (255, 0, 255), cv2.FILLED)
cv2.circle(img, (x2, y2), 15, (255, 0, 255), cv2.FILLED)
length = hypot(x2 - x1, y2 - y1)
vol = np.interp(length, [50, 300], [volmin, volmax])
volbar = np.interp(length, [50, 300], [400, 150])
volper= np.interp(length, [50, 300], [0, 100])
print(int(length), vol)
volume.SetMasterVolumeLevel(vol, None)
cv2.rectangle(img, (50, 150), (85, 400), (0, 255, 0), 3)
cv2.rectangle(img, (50, int(volbar)), (85, 400), (0, 255, 0), cv2.FILLED)
cv2.putText(img, f'{int(volper)}%', (40, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 3)
cv2.imshow("Image", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
capture.release()
cv2.destroyAllWindows()