-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (56 loc) · 2.32 KB
/
Copy pathmain.py
File metadata and controls
69 lines (56 loc) · 2.32 KB
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
import cv2
import mediapipe as mp
from math import hypot
import numpy as np
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
import requests
mpHands = mp.solutions.hands
hands = mpHands.Hands(
static_image_mode=False,
model_complexity=1,
min_detection_confidence=0.75,
min_tracking_confidence=0.75,
max_num_hands=2)
Draw = mp.solutions.drawing_utils
cap = cv2.VideoCapture(0)
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
_, frame = cap.read()
height, width, color_channels = frame.shape
while True:
frame = cv2.flip(frame, 1)
frameRGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
Process = hands.process(frameRGB)
landmarkList = []
if Process.multi_hand_landmarks:
for handlm in Process.multi_hand_landmarks:
for _id, landmarks in enumerate(handlm.landmark):
x, y = int(landmarks.x * width), int(landmarks.y * height)
landmarkList.append([_id, x, y])
Draw.draw_landmarks(frame, handlm, mpHands.HAND_CONNECTIONS)
if landmarkList != []:
x_1, y_1 = landmarkList[4][1], landmarkList[4][2]
x_2, y_2 = landmarkList[8][1], landmarkList[8][2]
cv2.circle(frame, (x_1, y_1), 7, (0, 255, 0), cv2.FILLED)
cv2.circle(frame, (x_2, y_2), 7, (0, 255, 0), cv2.FILLED)
cv2.line(frame, (x_1, y_1), (x_2, y_2), (0, 255, 0), 3)
L = hypot(x_2 - x_1, y_2 - y_1)
volume_level = np.interp(L, [15, 220], [0, 100])
volume.SetMasterVolumeLevelScalar(volume_level / 100, None)
volume_text = f"Volume: {int(volume_level)}%"
cv2.putText(frame, volume_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# Send volume data to the server
try:
print(f"Sending volume data: {int(volume_level)}") # Debugging line
requests.post('http://localhost:3000/volume', json={'volume': int(volume_level)})
except requests.exceptions.RequestException as e:
print(f"Error sending volume data: {e}")
cv2.imshow('Image', frame)
if cv2.waitKey(1) & 0xff == ord('q'):
break
_, frame = cap.read()
cap.release()
cv2.destroyAllWindows()