-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·146 lines (120 loc) · 5.11 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import cv2
import numpy as np
import hand as htm
import time
import pyautogui as pg
from ctypes import cast, POINTER # pip install pycaw
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
# Intialization for mouse
#####################
wCam, hCam = 640, 480
wScr, hScr = pg.size()
# print(wScr,hScr)
frameR = 100 # frame Reduction
smoothenes = 7
ptime = 0
plocX, plocY = 0, 0
clocX, clocY = 0, 0
#####################
#Volume Intialization
vol = 0
volBar = 400
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(
IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
volume.GetMute()
volume.GetMasterVolumeLevel()
volRange = volume.GetVolumeRange() # volume range (-65.25, 0.0)
minVol = volRange[0]
maxVol = volRange[1]
cap = cv2.VideoCapture(0)
cap.set(3, wCam)
cap.set(4, hCam)
detector = htm.hand(maxHands=1)
#
while True:
# Find hand Landmarks
success, img = cap.read()
# img = cv2.flip(img, 1)
img = detector.Hands(img)
lmList, bbox = detector.fingersPosition(img)
# Get the tip of the index and middle fingers
if len(lmList) != 0:
Tx, Ty = lmList[4][1:]
Ix, Iy = lmList[8][1:]
Mx, My = lmList[12][1:]
Rx, Ry = lmList[16][1:]
Px, Py = lmList[20][1:]
# print(Ix,Iy,Mx,My)
# Check which fingers are up
fingers = detector.fingersUp()
# print(fingers)
cv2.rectangle(img, (frameR, frameR), (wCam - frameR, hCam - frameR), (255, 0, 255), 2)
# Only Index Finger : Moving Mode
if fingers[0] == 0 and fingers[1] == 1 and fingers[2] == 0 and fingers[3] == 0 and fingers[4] == 0:
# Convert Coordinates
x3 = np.interp(Ix, (frameR, wCam - frameR), (0, wScr))
y3 = np.interp(Iy, (frameR, hCam - frameR), (0, hScr))
# Smoothen Values
clocX = plocX + (x3 - plocX) / smoothenes
clocY = plocY + (y3 - plocY) / smoothenes
# Move Mouse
pg.moveTo(wScr - clocX, clocY)
cv2.circle(img, (Ix, Iy), 15, (255, 0, 255), cv2.FILLED)
plocX, plocY = clocX, clocY
# Both Index and middle fingers are up : Clicking Mode
if fingers[0] == 0 and fingers[1] == 1 and fingers[2] == 1 and fingers[3] == 0 and fingers[4] == 0:
# Find distance between index finger
lengthIM, img, lineInfo = detector.findDistance(8, 12, img)
# print(lengthIM)
# Click mouse if distance short
if lengthIM < 40:
cv2.circle(img, (lineInfo[4], lineInfo[5]), 15, (0, 255, 0), cv2.FILLED)
pg.click()
# Both Thumb and index finger are up : Volume mode
if fingers[0] == 1 and fingers[1] == 1 and fingers[2] == 0 and fingers[3] == 0 and fingers[4] == 0:
# Find distance between finger and thumb
lengthTI, img, lineInfo = detector.findDistance(4, 8, img)
# print(length)
# Hand Range 50 to 300
# volume Range -65 to 0
vol = np.interp(lengthTI, [50, 250], [minVol, maxVol])
volBar = np.interp(lengthTI, [50, 250], [400, 150])
# print(int(length),vol)
volume.SetMasterVolumeLevel(vol, None)
cv2.rectangle(img, (50, 150), (85, 400), (255, 0, 0), 3)
cv2.rectangle(img, (50, int(volBar)), (85, 400), (255, 0, 0), cv2.FILLED)
if lengthTI < 50:
cv2.circle(img, (lineInfo[4], lineInfo[5]), 15, (0, 255, 0), cv2.FILLED)
# Index, middle and ring finger are up : scroll
if fingers[0] == 0 and fingers[1] == 1 and fingers[2] == 1 and fingers[3] == 1 and fingers[4] == 0:
lenghtIM, img, lineInfo = detector.findDistance(8, 12, img)
lengthMR, img, lineInfo = detector.findDistance(12, 16, img)
if lenghtIM < 40 and lengthMR < 45:
cv2.circle(img, (Mx, My), 15, (0, 255, 0), cv2.FILLED)
# print(My)
if My == 250:
pass
elif My > 250:
pg.scroll(20)
elif My < 250:
pg.scroll(-20)
# middle finger : fuck off
if fingers[0] == 0 and fingers[1] == 0 and fingers[2] == 1 and fingers[3] == 0:
cv2.putText(img,str("Fuck offf"),(70,250),cv2.FONT_HERSHEY_PLAIN, 6, (255, 0, 0), 5)
if fingers[0] == 1 and fingers[1] == 0 and fingers[2] == 1 and fingers[3] == 0:
cv2.putText(img,str("Fuck offf"),(70,250),cv2.FONT_HERSHEY_PLAIN, 6, (255, 0, 0), 5)
# system
if fingers[0] == 1 and fingers[1] == 1 and fingers[2] == 0 and fingers[3] == 0 and fingers[4] == 1:
cv2.putText(img,str("systumm"),(70,250),cv2.FONT_HERSHEY_PLAIN, 6, (255, 0, 0), 5)
# print(fingers[0],fingers[1],fingers[2],fingers[3])
# Frame Rate
ctime = time.time()
fps = 1 / (ctime - ptime)
ptime = ctime
cv2.putText(img, str(int(fps)), (20, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
# Display
cv2.imshow("Image", img)
cv2.waitKey(1)