-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
184 lines (170 loc) · 5.76 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import cv2
import mediapipe as mp
import time
import math
import alsaaudio
import pyautogui
def dist(p1, p2):
return math.sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2+(p1[2]-p2[2])**2)
def changevol(change):
newvol = int(m.getvolume()[0]+change)
if newvol>100:
newvol=100
elif newvol<0:
newvol=0
m.setvolume(newvol)
return newvol
m = alsaaudio.Mixer()
cap = cv2.VideoCapture(0)
mpHands = mp.solutions.hands
hands = mpHands.Hands(static_image_mode=False,
max_num_hands=2,
min_detection_confidence=0.5,
min_tracking_confidence=0.5)
mpDraw = mp.solutions.drawing_utils
ptime = 0
ctime = 0
pTime = 0
cTime = 0
handlist=[[1,1,0,0,1,{},[],0,0], [1,1,0,0,1,{},[],0,0]]
success, img = cap.read()
h, w, c = img.shape
d = (h+w)/2
workspace = [0, 0]
pageup = [False, False]
pagedown = [False, False]
handopen = [0, 0]
while True:
#print("begin====================")
success, img = cap.read()
img = cv2.flip(img, 1)
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = hands.process(imgRGB)
ctime = time.time()
frametime = (ctime-ptime)
#print(frametime)
ptime = ctime
#print(results.multi_hand_landmarks)
#print("\n\n\n\n")
i = 0
o = 0
for lis in handlist:
lis[4] += 1
#print(handlist)
if results.multi_hand_landmarks:
handdet = len(results.multi_hand_landmarks)
if results.multi_handedness[i].classification[0].label == "Right":
o = 1
else:
o = 0
#print(results.multi_handedness[0].classification[0].label)
for handLms in results.multi_hand_landmarks:
#print(i)
#handlist[i][4] += 1
#good code
if i<len(handlist):
pos = [0, 0, 0, 0, handlist[o][4],{},[],0,0]
else:
break
for id, lm in enumerate(handLms.landmark):
#print(id,lm)
cx, cy, cz = int(lm.x*w), int(lm.y*h), int(lm.z*d)
pos[0] += cx
pos[1] += cy
if id == 0 or 4 or 5 or 8 or 12 or 13 or 16 or 20:
pos[5][id] = (cx, cy, cz)
#print(str(cx) + " " + str(cy))
#if id ==0:
cv2.circle(img, (cx,cy), 3, (0,0,0), cv2.FILLED)
mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
pos[0] = pos[0]/21
pos[1] = pos[1]/21
#print("i: "+str(i)+" o: "+str(o))
#print(pos[4])
try:
pos[2] = (handlist[o][0]-pos[0])/pos[4]
pos[3] = (handlist[o][1]-pos[1])/pos[4]
except:
pos[2] = handlist[o][0]-pos[0]
pos[3] = handlist[o][1]-pos[1]
pos[4] = 0
dis = dist(pos[5][0], pos[5][13])
pos[6].append(dist(pos[5][5], pos[5][4]) < dist(pos[5][5], pos[5][13]) or dis*1.4 > dist(pos[5][0], pos[5][4]))
for idx in (8,12,16,20):
pos[6].append(dis*1.3 > dist(pos[5][0], pos[5][idx]))
#print(pos[6])
#nice code
try:
rel = (pos[5][0][1]-pos[5][4][1])/(pos[5][0][0]-pos[5][4][0])
#rel = ((pos[5][0][1]-pos[5][4][1])/dist(pos[5][0], pos[5][4]))
except:
rel = 999
#print(rel)
prev = handlist[o][7]
pos[7] = math.degrees(math.atan(rel))
#print(pos[5][0][1]>pos[5][4][1])
if pos[5][0][0]<pos[5][4][0]:
if pos[5][0][1]>pos[5][4][1]:
pos[7] = 90+pos[7]+90
else:
pos[7] = -(90-pos[7])-90
try:
pos[8] = (pos[7]-prev)/handlist[o][4]
except:
pos[8] = pos[7]-prev
#print(pos[8])
#print(pos[7])
#if len(handlist)>i:
handlist[o] = pos
#templist.append(pos)
#handlist[i] = pos
i += 1
#good job
#handlist = templist
#print(handlist)
#print(handlist)
#print()
i = 1
hand = handlist[1]
if hand[4] < 4:
if hand[6] == [False, True, True, True, True]:
#print("audio")
if 10 > abs(hand[8]) > 1:
#print(hand[8])
print(changevol(hand[8]))
if hand[6] == [False, False, True, True, True]:
if 16 > hand[8] > 1 or -1 > hand[8] > -16:
workspace[i] += hand[8]
#print()
#print(i)
#print(str(hand[8])+" "+str(workspace[i]))
#print(str(pageup[i])+" "+str(pagedown[i]))
if workspace[i] > 35 and not pageup[i]:
pyautogui.hotkey('win', 'pageup')
#print("up==============================================")
workspace[i] = 0
pageup[i] = True
if workspace[i] < -35 and not pagedown[i]:
pyautogui.hotkey('win', 'pagedown')
#print("down============================================")
workspace[i] = 0
pagedown[i] = True
if hand[6] == [False, False, False, False, False]:
handopen[i] += 1
#print("open")
if handopen[i] > 5:
#print("REsertttttttttttttttttttttttttttttttttt")
workspace[i] = 0
handopen[i] = 0
pagedown[i] = False
pageup[i] = False
else:
#print("resresresresresres"+str(i))
#pagedown[i] = False
#pageup[i] = False
workspace[i] = 0
fps = (1/frametime)
cv2.putText(img,str(int(fps)), (10,70), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 1)
#print(fps)
cv2.imshow("Image", img)
cv2.waitKey(1)