-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainMulti.py
231 lines (192 loc) · 7.55 KB
/
mainMulti.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import pygame
from pygame.locals import *
from sys import exit
import cv2
import time
from collections import deque
import traceback
from ModuleSound import effect1,effect2,effect3,effect4,effect5
from ModuleHand import handKeypoints
# from ModuleHandPaddle import handKeypoints
from ModuleInput import FrameProducer
pygame.init()
def yBiasCal(fringerTip,pressLineDict1):#higher than line is + else -
biasDict={}
for index,ft in fringerTip.items():
print('ft[1]-(fringerTipDown[index][1]+upRange',ft[1],pressLineDict1[index])
## ft[1]<0 fringerTipDown<0
biasDict[index]=ft[1]-(pressLineDict1[index])
return biasDict
def pressDetect(biasDictBefore,biasDictNow):
biasMinerList=[]
indexList=[]
fringerTipIndex=-1
print('biasDictBefore,biasDictNow',biasDictBefore,biasDictNow)
for index in range(5):
try:
if biasDictBefore[index]>0 and biasDictNow[index]<0:
biasMinerList.append(biasDictBefore[index]-biasDictNow[index])
indexList.append(index)
except Exception as e:
print(e)
if len(biasMinerList)>0:
mi=biasMinerList.index(max(biasMinerList))
fringerTipIndex=indexList[mi]
print('press----------',fringerTipIndex)
return fringerTipIndex
def uiProcess(image,ftDown1,ftDown2,pressLineDict1,pressLineDict2):
if len(ftDown1)==5 and len(pressLineDict1)==0:
image = drawPressArea(image, ftDown1, [])
if len(ftDown2)==5 and len(pressLineDict2)==0:
image = drawPressArea(image, ftDown2, [])
if len(pressLineDict1)==5:
image=drawPressArea(image, ftDown1,pressLineDict1)
if len(pressLineDict2)==5:
image=drawPressArea(image, ftDown2,pressLineDict2)
return image
def pressLineCal(ftDown1,ftUp1,rangeIndexList):#for position y: ftDown1<0,ftUp1<0,ftDown1<ftUp1
pressLineDict={}
for index,ftd in ftDown1.items():
print('pressLineCal',index,ftUp1[index][1],ftd[1])
print(rangeIndexList[index])
y_min=int(ftd[1]+(ftUp1[index][1]-ftd[1])*rangeIndexList[index])
pressLineDict[index]=y_min
ftUp1={}
## only reset ftUp ,keep ftDown
return pressLineDict,ftUp1
def drawPressArea(image,ftDown1,pressLineDict1):#for position y: ftDown1<0,pressLineDict1<0,ftDown1<pressLineDict1
# print('ftDown1--',ftDown1)
# print('pressLineDict1',pressLineDict1,ftDown1)
for index,ft in ftDown1.items():
try:
bias=pressLineDict1[index]
except:
bias=ft[1]-2
x_max=ft[0]+15
x_min=ft[0]-15
y_max=int(-1*ft[1])
y_min=int(-1*ft[1]+(ft[1]-bias))
cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (0, 0, 0), 4)
return image
def frameShow(frame,screen):
#
# timeStamp = cap.get(cv2.CAP_PROP_POS_MSEC)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
print('frame',frame.shape)
frame = cv2.transpose(frame)
frame = pygame.surfarray.make_surface(frame)
screen.blit(frame, (0, 0))
pygame.display.update()
# return timeStamp
def keyboardResponse(prodecer,handKeypointer,
ft1, ft2,ftDown1,ftDown2,ftUp1,ftUp2):
for event in pygame.event.get():
if event.type == pygame.QUIT:
prodecer.runFlag = False
handKeypointer.runFlag = False
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN :
print('down',ft1,ft2)
return ft1,ft2,ftUp1,ftUp2
if event.key == pygame.K_UP :
print('up',ft1,ft2)
return ftDown1,ftDown2,ft1,ft2
if event.key == pygame.K_1:
print('key1')
effect5.play()
return ftDown1,ftDown2,ftUp1,ftUp2
def loopRun(dataDeque,wSize,hSize,prodecer,handKeypointer,rangeIndexList,skipFrame):
# tip position of hand down
ftDown1={}
ftDown2={}
# tip position for now
ft1={}
ft2={}
# tip position of hand up
ftUp1={}
ftUp2={}
#
pressLineDict1={}
pressLineDict2={}
#
biasDict1={}
biasDict2={}
screen = pygame.display.set_mode((wSize,hSize))
# cap = cv2.VideoCapture(path)
num=-1
result={}
while True:
##
FPS=prodecer.fps/skipFrame
if FPS > 0:
videoFlag = True
else:
videoFlag = False
##
##
ftDown1,ftDown2,ftUp1,ftUp2=keyboardResponse(prodecer, handKeypointer,
ft1, ft2,ftDown1,ftDown2,ftUp1,ftUp2)
##
if len(ftDown1)==5 and len(ftUp1)==5:
pressLineDict1, ftUp1=pressLineCal(ftDown1, ftUp1, rangeIndexList)
if len(ftDown2)==5 and len(ftUp2)==5:
pressLineDict2, ftUp2=pressLineCal(ftDown2, ftUp2, rangeIndexList)
# print('ppp', len(dataDeque), len(result))
if len(dataDeque)==0 and len(result)==0:
time.sleep(0.1)
continue
# print('FPS',FPS)
if len(result)==0:
##
result=dataDeque.popleft()
image=result['image']
ft1=result['fringerTip1']
ft2=result['fringerTip2']
if videoFlag:
num += 1
if num == 0:
T0 = time.time()
print('T0',T0,num*(1./FPS))
try:
image = uiProcess(image,ftDown1,ftDown2,pressLineDict1,pressLineDict2)
# print('ft1,2',ft1,ft2)
if len(ft1) ==5 and len(pressLineDict1) == 5 :#to do: do not need all fringer tip to be detected at that time
bl1 = yBiasCal(ft1, pressLineDict1)
if len(biasDict1) > 0 and len(bl1) > 0:
# print('biasDict1', biasDict1, bl1)
fringerTipIndex1 = pressDetect(biasDict1, bl1)
if fringerTipIndex1 >= 0:
eval('effect' + str(fringerTipIndex1+1)).play()
cv2.putText(image, str(fringerTipIndex1+1),(int( ftDown1[fringerTipIndex1][0]),int(-1*ftDown1[fringerTipIndex1][1])), cv2.FONT_HERSHEY_COMPLEX,
2, (0, 255, 0), thickness=2, lineType=4)
print('play effect:', str(fringerTipIndex1))
biasDict1 = bl1
if len(ft2) == 5 and len(pressLineDict2) == 5 :
bl2 = yBiasCal(ft2, pressLineDict2)
##################
if len(biasDict2) > 0 and len(bl2) > 0:
# print('biasDict2', biasDict2, bl2)
fringerTipIndex2 = pressDetect(biasDict2, bl2)
if fringerTipIndex2 >= 0:
eval('effect' + str(5 - fringerTipIndex2)).play()
cv2.putText(image, str(fringerTipIndex2+1),
( int(ftDown2[fringerTipIndex2][0]),int(-1*ftDown2[fringerTipIndex2][1])), cv2.FONT_HERSHEY_COMPLEX,
2, (0, 255, 0), thickness=2, lineType=4)
biasDict2 = bl2
except Exception as e:
traceback.print_exc()
frameShow(image, screen)
#clear result
result={}
if __name__=='__main__':
from initParam import link,wSize,hSize,rangeIndexList,maxLen,skipFrmae
ft1=ft2=ftDown1=ftDown2=[]
biasDict1=biasDict2= {}
dataDeque = deque(maxlen=maxLen)
resultDeque=deque()
producer = FrameProducer(dataDeque, wSize, hSize, link,skipFrmae)
handKeypointer = handKeypoints(dataDeque, resultDeque,savePath='')
producer.start()
handKeypointer.start()
loopRun(resultDeque, wSize, hSize, producer,handKeypointer,rangeIndexList,skipFrmae)