Skip to content

Commit 4109076

Browse files
authored
Add files via upload
1 parent 50994d5 commit 4109076

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

V/vol-handcontroller/HTrack.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
This file uses mediapipe - a framework by google to detect the landmarks of hand (21)
3+
in the image captured from webcam using openCV
4+
The functionality wrapped as handDetector class containing methods drawHands() and getPositions()
5+
drawHands() --> To draw land marks of detected hands
6+
getPositions() --> To return the found landmarks as a list
7+
"""
8+
import cv2
9+
import mediapipe as mp
10+
import time
11+
12+
class handDetector():
13+
def __init__(self, mode=False, maxHands=2, detectionCon=0.5, trackCon=0.5):
14+
self.mode = mode
15+
self.maxHands = maxHands
16+
self.detectionCon = detectionCon
17+
self.trackCon = trackCon
18+
self.mpDraw = mp.solutions.drawing_utils
19+
self.hands = mp.solutions.hands.Hands(static_image_mode=self.mode,max_num_hands= self.maxHands,min_detection_confidence=self.detectionCon,min_tracking_confidence= self.trackCon)
20+
def drawHands(self, img, draw=True):
21+
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
22+
self.res = self.hands.process(imgRGB)
23+
if self.res.multi_hand_landmarks:
24+
for handLms in self.res.multi_hand_landmarks:
25+
if draw:
26+
self.mpDraw.draw_landmarks(img, handLms,mp.solutions.hands.HAND_CONNECTIONS)
27+
return img
28+
29+
def getPositions(self, img, handId=0):
30+
self.xList=[]
31+
self.yList=[]
32+
self.lmList = []
33+
if self.res.multi_hand_landmarks:
34+
decHand = self.res.multi_hand_landmarks[handId]
35+
for index, lmark in enumerate(decHand.landmark):
36+
h, w, c = img.shape
37+
cx, cy = int(lmark.x * w), int(lmark.y * h)
38+
self.xList.append(cx)
39+
self.yList.append(cy)
40+
self.lmList.append([index, cx, cy])
41+
return self.lmList
42+
43+
def main():
44+
prevTime = 0
45+
currTime = 0
46+
cap = cv2.VideoCapture(0)
47+
detector = handDetector()
48+
while True:
49+
_, img = cap.read()
50+
img = detector.drawHands(img)
51+
lms= detector.getPositions(img)
52+
print(lms)
53+
currTime = time.time()
54+
fps = 1 / (cTime - pTime)
55+
prevTime = cTime
56+
cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3,(51, 0, 255), 3)
57+
cv2.imshow("Image", img)
58+
cv2.waitKey(1)
59+
60+
if __name__ == "__main__":
61+
main()

V/vol-handcontroller/VolCon.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
""" I have used pycaw by Andre Miras at https://github.com/AndreMiras/pycaw
2+
To control audio. It is an open-source module which can be installed by --> pip install pycaw
3+
"""
4+
import math
5+
import cv2
6+
import time
7+
import numpy as np
8+
import HTrack as ht
9+
from comtypes import CLSCTX_ALL
10+
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
11+
12+
height = 720 # height of webwindow
13+
width = 1080 # width of webwindow
14+
cap = cv2.VideoCapture(0) # using default system web cam 0
15+
cap.set(3, width) # 3 is a index specifying width
16+
cap.set(4, height) # 4 is a index specifying height
17+
prevTime = 0
18+
detector = ht.handDetector(detectionCon=0.7) # we need to detect hand more precisely
19+
devices = AudioUtilities.GetSpeakers() # instantiating system speakers as objects
20+
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None) # activating the interface to control volume
21+
volume = interface.QueryInterface(IAudioEndpointVolume)
22+
rangeVol=volume.GetVolumeRange() # volume range for my system is found to be -96 to 0
23+
volLeast=rangeVol[0]
24+
volHigh=rangeVol[1]
25+
vol=volLeast #initialising volume to least volume
26+
volBar=400 #initialising volume bar
27+
while True:
28+
success, img = cap.read() # reading the image
29+
img=detector.drawHands(img) # detecting the hands
30+
lmList=detector.getPositions(img) # finding landmarks of detected hands
31+
if len(lmList)!=0:
32+
# print(lmList[8],lmList[12])
33+
x1,y1=lmList[8][1],lmList[8][2]
34+
x2,y2=lmList[12][1],lmList[12][2]
35+
cx,cy=(x1+x2)//2,(y1+y2)//2
36+
cv2.circle(img,(x1,y1),10,(170,153,255),cv2.FILLED) # drawing circle around index finger
37+
cv2.circle(img,(x2,y2),10,(170,153,255),cv2.FILLED) # drawing circle around middle finger
38+
cv2.circle(img,(cx,cy),10,(170,153,255),cv2.FILLED) # drawing circle around center point of both fingers
39+
cv2.line(img,(x1,y1),(x2,y2),(25,102,180),2)
40+
distance=math.dist([x1,y1],[x2,y2])
41+
# print(distance) # range of distance found to be 25 and 150
42+
vol=np.interp(distance,[25,150],[volLeast,volHigh]) # To interpolate distance between index and middle fingers to the volume of system
43+
volBar=np.interp(distance,[25,150],[400,100]) # To interpolate distance between index and middle fingers to the volume bar length
44+
volume.SetMasterVolumeLevel(vol, None)
45+
print(int(distance),vol)
46+
if distance<25:
47+
cv2.circle(img,(cx,cy),10,(0,0,120),cv2.FILLED) # indicating volume is 0 bby changing color of center circle
48+
#drawing volume bar
49+
cv2.rectangle(img,(80,100),(60,400),(230,153,0),2) # outer rectangle
50+
cv2.rectangle(img,(80,int(volBar)),(60,400),(150,153,0),cv2.FILLED) # actual volume
51+
cv2.putText(img,"Volume",(40,420),cv2.FONT_HERSHEY_SIMPLEX,0.5,(149,100,149),2)
52+
53+
currTime = time.time()
54+
fps = 1/(currTime - prevTime) # using time to get timestamp of previous iteration and current iteration to know the frames per second
55+
prevTime = currTime
56+
cv2.putText(img, str(int(fps)), (30, 40), cv2.FONT_HERSHEY_PLAIN, 2, (255, 255, 0), 3) # writing FPS on screen
57+
cv2.imshow("Image", img)
58+
cv2.waitKey(1)
59+

0 commit comments

Comments
 (0)