Hand Controlling Volume using Python Programming!!

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

import cv2

import numpy as np

import math

from ctypes import cast, POINTER

from comtypes import CLSCTX_ALL

from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume

import mediapipe as mp

#Initialize Mediapipe hand detection

mpHands mp.solutions.hands

hands mpHands. Hands()

mpDraw mp.solutions.drawing_utils

#Initialize camera

wCam, hCam = 640, 480

cap = cv2.VideoCapture(0)

cap.set(3, wCam)

cap.set(4, hCam)

#Initialize Pycaw for volume control

devices AudioUtilities.GetSpeakers()

interface devices.Activate(

IAudioEndpointVolume._iid, CLSCTX_ALL, None)

volume cast(interface, POINTER (IAudioEndpointVolume))

#Get volume range

volRange volume.GetVolumeRange()

minvol volRange [0]

maxVol volRange [1]

while True:

success, img = cap.read()

imgRGB= cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

results hands.process(imgRGB)

lmList = []

if results.multi_hand_landmarks:

for handLms in results.multi_hand_landmarks:

for id, lm in enumerate (handLms.landmark):

h, w, cimg.shape

cx, cy = int(lm.xw), int(lm.y * h)

ImList.append([id, cx, cy])
mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)

if len(lmList) != 0:

#Positions for thumb and little finger

x1, y1 InList[4][1], InList[4][2]

x2, y2 lmList[20] [1], InList[20][2]

cx, cy (x1+x2) // 2, (y1 y2) // 2

cv2.circle(img, center: (x1, y1),

radius 15,

color: (255, 0, 255), cv2.FILLED)

cv2.circle(img, center: (x2, y2), radius 15, color (255, 0, 255), cv2.FILLED)

cv2.line(img, pti (x1, y1), pt2 (x2, y2), color: (255, 0, 255), thickness: 3)

cv2.circle(img, center (cx, cy), radius: 15, color: (255, 0, 255), cv2.FILLED)

length math.hypot(x2 x1, y2 y1)

vol np.interp(length, xp [50, 300], fp [minvol, maxVol])

volBar np.interp(length, xp [50, 300], fp: [400, 150])

volper np.interp(length, xp [50, 300], p: [0, 100])

volume.SetMaster VolumeLevel(vol, None)

if length < 58:

cv2.circle(img, center: (cx, cy), radius 15, color (0, 255, 0), cv2.FILLED)

cv2.rectangle(img, (50, 150), (85, 400), (8, 255, 0), 3)

cv2.rectangle(img, (50, int(volBar)), (85, 400), (8, 255, 8), cv2.FILLED)

cv2.putText(img, text: f'{int(volPer)} %, org (48, 458), cv2.FONT_HERSHEY_COMPLEX, fontScale 1, color (8, 255, 8), thickness: 3)

cv2.imshow(winname: "Img", img)

if cv2.waitKey(1) & 0xFF == ord("q"):

break

cap.release()

cv2.destroyAllWindows()

programmer-designer
visit shbcf.ru