-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
116 lines (97 loc) · 4.6 KB
/
main.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
import cv2
import mediapipe as mp
import math
import numpy as np
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
import screen_brightness_control as sbc
# Solution APIs
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
# Volume Control Library Usage
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
volRange = volume.GetVolumeRange()
minVol, maxVol, volBar, volPer = volRange[0], volRange[1], 400, 0
# Brightness Control Library Usage
monitors = sbc.list_monitors()
minBrightness, maxBrightness = 0, 150
# Webcam Setup
wCam, hCam = 640, 480
cam = cv2.VideoCapture(0)
cam.set(3, wCam)
cam.set(4, hCam)
# Mediapipe Hand Landmark Model
with mp_hands.Hands(
model_complexity=0,
min_detection_confidence=0.5,
min_tracking_confidence=0.5,
max_num_hands=2) as hands:
while cam.isOpened():
success, image = cam.read()
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = hands.process(image)
image = cv2.cvtColor(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(myHand.landmark):
h, w, c = image.shape
cx, cy = int(lm.x * w), int(lm.y * h)
lmList.append([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]
if x1 < wCam // 2:
# Left hand controls brightness
# Use set_brightness function with monitor index
# Visualize left hand
cv2.circle(image, (x1, y1), 15, (255, 255, 255))
cv2.circle(image, (x2, y2), 15, (255, 255, 255))
cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 3)
length = math.hypot(x2 - x1, y2 - y1)
if length < 50:
cv2.line(image,(x1,y1),(x2,y2),(0,0,255),3)
# Brightness Bar
brightness = np.interp(length, [50, 220], [0, 100])
sbc.set_brightness(brightness, display=monitors[0])
brightness_bar = np.interp(length, [50, 220], [400, 150])
cv2.rectangle(image, (50, 150), (85, 400), (0, 0, 0), 3)
cv2.rectangle(image, (50, int(brightness_bar)), (85, 400), (0, 0, 0), cv2.FILLED)
cv2.putText(image, f'{int(brightness)} %', (40, 450), cv2.FONT_HERSHEY_COMPLEX,
1, (0, 0, 0), 3)
else:
# Right hand controls volume
cv2.circle(image, (x1, y1), 15, (255, 255, 255))
cv2.circle(image, (x2, y2), 15, (255, 255, 255))
cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 3)
length = math.hypot(x2 - x1, y2 - y1)
if length < 50:
cv2.line(image,(x1,y1),(x2,y2),(0,0,255),3)
vol = np.interp(length, [50, 220], [minVol, maxVol])
volume.SetMasterVolumeLevel(vol, None)
volBar = np.interp(length, [50, 220], [400, 150])
volPer = np.interp(length, [50, 220], [0, 100])
# Volume Bar
cv2.rectangle(image, (50, 150), (85, 400), (0, 0, 0), 3)
cv2.rectangle(image, (50, int(volBar)), (85, 400), (0, 0, 0), cv2.FILLED)
cv2.putText(image, f'{int(volPer)} %', (40, 450), cv2.FONT_HERSHEY_COMPLEX,
1, (0, 0, 0), 3)
cv2.imshow('handDetector', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cam.release()