-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathVolume_Control.py
134 lines (101 loc) · 4.52 KB
/
Volume_Control.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import cv2
import time
import numpy as np
import HandTracking as ht
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
#############################################################
cam_width, cam_height = 640, 480
previous_time = 0
volume_per = 0
volume_bar = 0
vol = 400
color_vol = (255, 0, 0)
count = 0
#############################################################
cap = cv2.VideoCapture(0)
cap.set(3, cam_width)
cap.set(4, cam_height)
detector = ht.HandDetector(detection_confidence=0.7, max_hands=1)
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
# volume.GetMute()
# volume.GetMasterVolumeLevel()
volume_range = volume.GetVolumeRange()
min_vol = volume_range[0]
max_vol = volume_range[1]
while True:
success, img = cap.read()
# ---------------------------------------- #
# Find Hand #
# ---------------------------------------- #
img = detector.find_hands(img)
landmark_list, bounding_box = detector.find_position(img, draw=False)
if len(landmark_list) != 0:
# ---------------------------------------- #
# Filter based on size #
# ---------------------------------------- #
area = (bounding_box[2]-bounding_box[0]) * (bounding_box[3]-bounding_box[1])//100
# print(area)
if 100 < area < 1500:
# print(f"yes {area}")
# ---------------------------------------- #
# Find Distance between Index and Thumb #
# ---------------------------------------- #
length, img, line_info = detector.find_distance(4, 8, img)
# ---------------------------------------- #
# Convert Volume #
# ---------------------------------------- #
# Hand Range 50 - 300
# Volume Range -74 - 0
# print(f"{vol} : {length}")
# volume.SetMasterVolumeLevel(vol, None)
volume_bar = np.interp(length, [20, 180], [400, 150])
volume_per = np.interp(length, [20, 180], [0, 100])
# ---------------------------------------- #
# Reduce Resolution to make it smoother #
# ---------------------------------------- #
smoothness = 10
volume_per = smoothness * round(volume_per/smoothness)
# ---------------------------------------- #
# Check finger up #
# ---------------------------------------- #
fingers = detector.fingers_up()
# print(fingers)
# ---------------------------------------- #
# If pinky is down set volume #
# ---------------------------------------- #
if not fingers[3]:
volume.SetMasterVolumeLevelScalar(volume_per / 100, None)
cv2.circle(img, (line_info[4], line_info[5]), 10, (20, 255, 0), cv2.FILLED)
color_vol = (0, 255, 0)
else:
color_vol = (255, 0, 0)
# print(landmark_list[4], landmark_list[8])
# print(length)
count += 1
# else:
# print(f"no {area}")
# ---------------------------------------- #
# Drawings #
# ---------------------------------------- #
if count > 0:
cv2.rectangle(img, (50, 150), (85, 400), (0, 0, 0), 2)
cv2.rectangle(img, (51, int(volume_bar)), (84, 400), (255, 0, 0), cv2.FILLED)
cv2.putText(img, text=f"{int(volume_per)}%", org=(40, 450), fontFace=cv2.FONT_HERSHEY_PLAIN,
fontScale=2, color=(255, 0, 0), thickness=3)
current_volume = int(volume.GetMasterVolumeLevelScalar()*100)
cv2.putText(img, text=f"Volume Set : {current_volume}", org=(350, 50), fontFace=cv2.FONT_HERSHEY_PLAIN, fontScale=2,
color=color_vol, thickness=3)
# ---------------------------------------- #
# Frame rate #
# ---------------------------------------- #
current_time = time.time()
fps = 1 / (current_time - previous_time)
previous_time = current_time
cv2.putText(img, text=f"FPS : {int(fps)}", org=(10, 50), fontFace=cv2.FONT_HERSHEY_PLAIN, fontScale=2,
color=(255, 0, 0), thickness=3)
cv2.imshow("Img", img)
cv2.waitKey(1)