import cv2
import mediapipe as mp
import math
import numpy as np
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from [Link] import AudioUtilities, IAudioEndpointVolume
# solution APIs
mp_drawing = [Link].drawing_utils
mp_drawing_styles = [Link].drawing_styles
mp_hands = [Link]
# Volume Control Library Usage
devices = [Link]()
interface = [Link]([Link], CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
volRange = [Link]()
minVol, maxVol, volBar, volPer = volRange[0], volRange[1], 400, 0
# Webcam Setup
wCam, hCam = 640, 480
cam = [Link](0)
[Link](3, wCam)
[Link](4, hCam)
# Mediapipe Hand Landmark Model
with mp_hands.Hands(
model_complexity=0,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as hands:
while [Link]():
success, image = [Link]()
image = [Link](image, cv2.COLOR_BGR2RGB)
results = [Link](image)
image = [Link](image, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
image,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style()
)
# multi_hand_landmarks method for Finding position of Hand landmarks
lmList = []
if results.multi_hand_landmarks:
myHand = results.multi_hand_landmarks[0]
for id, lm in enumerate([Link]):
h, w, c = [Link]
cx, cy = int(lm.x * w), int(lm.y * h)
[Link]([id, cx, cy])
# Assigning variables for Thumb and Index finger position
if len(lmList) != 0:
x1, y1 = lmList[4][1], lmList[4][2]
x2, y2 = lmList[8][1], lmList[8][2]
# Marking Thumb and Index finger
[Link](image, (x1, y1), 15, (255, 255, 255))
[Link](image, (x2, y2), 15, (255, 255, 255))
[Link](image, (x1, y1), (x2, y2), (0, 255, 0), 3)
length = [Link](x2 - x1, y2 - y1)
if length < 50:
[Link](image, (x1, y1), (x2, y2), (0, 0, 255), 3)
vol = [Link](length, [50, 220], [minVol, maxVol])
[Link](vol, None)
volBar = [Link](length, [50, 220], [400, 150])
volPer = [Link](length, [50, 220], [0, 100])
# Volume Bar
[Link](image, (50, 150), (85, 400), (0, 0, 0), 3)
[Link](image, (50, int(volBar)), (85, 400), (0, 0, 0),
[Link])
[Link](image, f'{int(volPer)} %', (40, 450),
cv2.FONT_HERSHEY_COMPLEX,
1, (0, 0, 0), 3)
[Link]('handDetector', image)
if [Link](1) & 0xFF == ord('q'):
break
[Link]()