-
Notifications
You must be signed in to change notification settings - Fork 0
/
trial2.py
43 lines (34 loc) · 1.5 KB
/
trial2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from handDetector import handdetector
import cv2
import math
import numpy as np
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
handDetector=handdetector(min_detection_confidence=0.7)
video = cv2.VideoCapture(0)
#Volume related initializations
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(
IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
print(volume.GetVolumeRange()) #(-65.25, 0.0)
while True:
status, image = video.read()
handLandmarks = handDetector.findHandLandMarks(image=image, draw=True)
if(len(handLandmarks) != 0):
#for volume control we need 4th and 8th landmark
#details: https://google.github.io/mediapipe/solutions/hands
x1, y1 = handLandmarks[4][1], handLandmarks[4][2]
x2, y2 = handLandmarks[8][1], handLandmarks[8][2]
length = math.hypot(x2-x1, y2-y1)
print(length)
#Hand range(length): 50-250
#Volume Range: (-65.25, 0.0)
volumeValue = np.interp(length, [50, 250], [-65.25, 0.0]) #coverting length to proportionate to volume range
volume.SetMasterVolumeLevel(volumeValue, None)
cv2.circle(image, (x1, y1), 15, (255, 0, 255), cv2.FILLED)
cv2.circle(image, (x2, y2), 15, (255, 0, 255), cv2.FILLED)
cv2.line(image, (x1, y1), (x2, y2), (255, 0, 255), 3)
cv2.imshow("Volume", image)
cv2.waitKey(1)