-
Notifications
You must be signed in to change notification settings - Fork 1
/
qtest.py
427 lines (377 loc) · 15.6 KB
/
qtest.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# From Python
# It requires OpenCV installed for Python
import sys
import cv2
import os
from sys import platform
import argparse
import numpy as np
import tensorflow as tf
from multiprocessing import Process,Queue,Pipe
import OSC
import scipy as sp
c = OSC.OSCClient()
c.connect(('10.19.33.79', 57120)) # connect to SuperCollider on surface
# c.connect(('127.0.0.1', 57120)) # connect to SuperCollider IDE Local
#c.connect(('192.168.7.2', 57120)) # connect to Bela
import time
class_names = ['RR', 'guitar-knees', 'bass', 'cowbell', 'piano', 'guitar', 'goats','clap','drums','dab','stand','throatcut','bow']
#with CustomObjectScope({'GlorotUniform': glorot_uniform()}):
#model = load_model('imdb_mlp_model.h5')
#
# Import Openpose (Windows/Ubuntu/OSX)
dir_path = os.path.dirname(os.path.realpath(__file__))
try:
# Windows Import
if platform == "win32":
# Change these variables to point to the correct folder (Release/x64 etc.)
sys.path.append(dir_path + '/../../python/openpose/Release');
os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;'
import pyopenpose as op
else:
# Change these variables to point to the correct folder (Release/x64 etc.)
sys.path.append('../../python');
# If you run `make install` (default path is `/usr/local/python` for Ubuntu), you can also access the OpenPose/python module from there. This will install OpenPose and the python library at your desired installation path. Ensure that this is in your python path in order to use it.
# sys.path.append('/usr/local/python')
from openpose import pyopenpose as op
except ImportError as e:
print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?')
raise e
# Flags
parser = argparse.ArgumentParser()
parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000192.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).")
args = parser.parse_known_args()
# Custom Params (refer to include/openpose/flags.hpp for more parameters)
params = dict()
params["model_folder"] = "../../../models/"
params['net_resolution'] = '128x96'
params['camera_resolution'] = '640x480'
params['camera'] = '1'
params['render_pose'] = 0
# Add others in path?
for i in range(0, len(args[1])):
curr_item = args[1][i]
if i != len(args[1])-1: next_item = args[1][i+1]
else: next_item = "1"
if "--" in curr_item and "--" in next_item:
key = curr_item.replace('-','')
if key not in params: params[key] = "1"
elif "--" in curr_item and "--" not in next_item:
key = curr_item.replace('-','')
if key not in params: params[key] = next_item
stream = cv2.VideoCapture(1)
if (not stream.isOpened()): # check if succeeded to connect to the camera
print("Cam open failed");
else:
stream.set(3,640);
stream.set(4,480);
loop = 0
windowSize = 6 # just for reference
#list of the coords of the different people
list1 = []
list2 = []
list3 = []
motion = {1:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], 2:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], 3:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}
drumHistory = {'left':[0,0],'right':[0,0]}
#list21 = []
#list22 = []
ready = [0,0,0]
state = {'mode':'begin','players':{'guitar':0,'bass':0,'drums':0}}
#whatStateAreWeIn = {'mode':'begin','players':[]} #play, end
#players = {'guitar':[-1],'bass':-1,'drums':-1}
tempMovements = []
# list of body keypoints as coordinates
headX = 0
headY = 1
neckX = 2
neckY = 3
rShoulderX = 4
rShoulderY = 5
rElbowX = 6
rElbowY = 7
rWristX = 8
rWristY = 9
lShoulderX = 10
lShoulderY = 11
lElbowX = 12
lElbowY = 13
lWristX = 14
lWristY = 15
pelvisX = 16
pelvisY = 17
rHipX = 18
rHipY = 19
rKneeX = 20
rKneeY = 21
rAnkleX = 22
rAnkleY = 23
lHipX = 24
lHipY = 25
lKneeX = 26
lKneeY = 27
lAnkleX = 28
lAnkleY = 29
frame6 = 250
frame5 = 200
frame4 = 150
frame3 = 100
frame2 = 50
frame1 = 0
# Starting OpenPose
opWrapper = op.WrapperPython()
print(params)
opWrapper.configure(params)
opWrapper.start()
oldtime = time.time()
def most_common(lst):
return max(set(lst), key=lst.count)
#function to send data
def packageAndSend(feature):
global oldtime
global tempMovements
if (time.time() - oldtime > 3):
tempMovements.append(feature)
print('*************SENT TO MUSIC SERVER *****************')
oscmsg = OSC.OSCMessage()
oscmsg.setAddress("/bang")
#oscmsg.append(most_common(tempMovements))
oscmsg.append('bang')
c.send(oscmsg)
#c.send('bang')
oldtime = time.time()
tempMovements = []
else:
tempMovements.append(feature)
def returnNormalizedKeypoints(keypoints):
temp = []
for i in range(len(keypoints)):
if (keypoints[i,0] != 0):
temp.append(keypoints[i,0]/640) # x value normalized (640 pixels wide)
else:
temp.append(-1)
if (keypoints[i,1] != 0):
temp.append(keypoints[i,1]/480) # y value normalized (480 pixels high)
else:
temp.append(-1)
#returned the normalized keypoint
return temp
#returns the percentage that x is between a and b,
#assumes a is lower value and b is higher value
def returnPercentage(x,a,b):
if (x >=a):
return 0
if (x <=b):
return 1
else:
return (x-a)/(b-a)
def get_angle(p0, p1=np.array([0,0]), p2=None):
''' compute angle (in degrees) for p0p1p2 corner
Inputs:
p0,p1,p2 - points in the form of [x,y]
'''
if p2 is None:
p2 = p1 + np.array([1, 0])
v0 = np.array(p0) - np.array(p1)
v1 = np.array(p2) - np.array(p1)
angle = np.math.atan2(np.linalg.det([v0,v1]),np.dot(v0,v1))
return np.degrees(angle)
def checkForPose(history, person):
history = np.reshape(history, (-1, 50))
a = np.expand_dims(history, axis=0)
b = model.predict(a)
blist = []
for i in b[0]:
blist.append(i)
#print ('person ' + str(person) +':' + class_names[blist.index(max(b[0]))] + " confidence: " + str(max(b[0])))
if (max(b[0]) > .8):
if (state['mode'] == 'begin'):
if (class_names[blist.index(max(b[0]))] == 'guitar' and state['players']['guitar'] ==0):
state['players']['guitar'] = person
print('player ' + str(person) + 'selected guitar')
elif(class_names[blist.index(max(b[0]))] == 'bass' and state['players']['guitar'] ==0):
state['players']['bass'] = person;
print('player ' + str(person) + 'selected bass')
elif(class_names[blist.index(max(b[0]))] == 'drums' and state['players']['guitar'] ==0):
state['players']['drums'] = person
print('player ' + str(person) + 'selected drums')
elif(class_names[blist.index(max(b[0]))] == 'bow'):
if (state['players']['guitar'] ==person or state['players']['bass']==person or state['players']['drums']==person):
ready[person-1] = 1
print('player ' + str(person) + 'is ready')
if (ready[0]==1 and ready[1]==1 and ready[2]==1):
print('heading into play mode!')
state['mode'] = 'play'
print('in play mode')
else:
#print('waiting on other players...')
print('entering test mode')
state['mode']='test'
elif state['mode'] == 'play':
print ('person ' + str(person) +':' + class_names[blist.index(max(b[0]))] + " confidence: " + str(max(b[0])))
#print(class_names[blist.index(max(b[0]))])
#print ('person ' + str(person) +':' + class_names[blist.index(max(b[0]))] + " confidence: " + str(max(b[0])))
#packageAndSend(class_names[b[0]])
#print(type(b))
def checkDrumsDSP(side,handX,handY,history,currentFrame):
yMax = max(history[currentFrame + lShoulderY],history[currentFrame + rShoulderY])
yMin = min(history[currentFrame + lHipY],history[currentFrame + rHipY])
xMax = max(history[currentFrame + lShoulderX],history[currentFrame + lHipX])
xMin = min(history[currentFrame + rShoulderX],history[currentFrame + rHipX])
#adjust box after testing it out to avoid less false positives
yMax = yMax*.9
yMin = yMin*.9
xMax = xMax*1.1
xMin = xMin*.9
#here we check to see if their wrist is out of the safe zone(torso).
#if it is out of the box, we send a drum hit depending on the location
#start with left hand
if (history[currentFrame + handY] < yMax or history[currentFrame + handY] > yMin or history[currentFrame + handX] < xMin or history[currentFrame + handX] > xMax):
if (history[currentFrame + handY] < yMax):
if (history[currentFrame + handX] < xMin):
#print(str(side) + ' quadrant 1')
return 1
elif(history[currentFrame + handX] > xMax):
#print(str(side) + 'quadrant 3')
return 3
else:
#print(str(side) + 'quadrant 2')
return 2
else: #left hand is below waist
if (history[currentFrame + handX] < xMin):
#print(str(side) + 'quadrant 4')
return 4
elif(history[currentFrame + handX] > xMax):
#print(str(side) + 'quadrant 6')
return 6
else:
# print(str(side) + 'quadrant 5')
return 5
return 0
def checkStrums(history,person,currentFrame, previousFrame):
strums = []
if (person == state['players']['bass']):
wrist1 = [lWristX,lWristY]
wrist2 = [rShoulderX,rShoulderY]
hip = [lHipX,lHipY]
shoulder = [rShoulderX,rShoulderY]
word = 'bass'
elif (person == state['players']['guitar']):
wrist1 = [rWristX,rWristY]
wrist2 = [rShoulderX,lShoulderY]
hip = [rHipX,rHipY]
shoulder = [lShoulderX,lShoulderY]
word = 'guitar'
for i in (currentFrame,previousFrame):
x = history[i + wrist1[0]]
y = history[i + wrist1[1]]
x1 = history[i + wrist2[0]]
y1 = history[i + wrist2[1]]
x2 = history[i + hip[0]]
y2 = history[i + hip[0]]
strums.append((x-x1)*(y2-y1)-(y-y1)*(x2-x1))
if (strums[0] > 0 and strums[1] < 0) or (strums[0] < 0 and strums[1] > 0):
print('strum ' + word)
currentAngle = get_angle([history[currentFrame+wrist1[0]],history[currentFrame+wrist1[1]]],[history[currentFrame+shoulder[0]],history[currentFrame+shoulder[1]]],[0,history[currentFrame+shoulder[1]]])
previousAngle = get_angle([history[previousFrame+wrist1[0]],history[previousFrame+wrist1[1]]],[history[previousFrame+shoulder[0]],history[previousFrame+shoulder[1]]],[0,history[previousFrame+shoulder[1]]])
previousFrame2 = previousFrame-50
previousAngle2 = get_angle([history[previousFrame2+wrist1[0]],history[previousFrame2+wrist1[1]]],[history[previousFrame2+shoulder[0]],history[previousFrame2+shoulder[1]]],[0,history[previousFrame2+shoulder[1]]])
#print(currentAngle)
if (currentAngle>=90 and previousAngle <=170) and (previousAngle<=-90 or previousAngle2 <=-90):
print ('powerStrum ' + word)
def checkForDSP(history,person,currentFrame,previousFrame):
#highkick
if (history[currentFrame + lAnkleY] < history[currentFrame + rKneeY] or history[currentFrame + rAnkleY] < history[currentFrame + lKneeY]):
print ('highkick')
#allow guitar to fret
#if (person == 1):
#delta of hand movement
#print("right fret level:" + str(returnPercentage(history[currentFrame+rWristY],history[currentFrame+pelvisY],history[currentFrame+headY])))
#print("left fret level:" + str(returnPercentage(history[currentFrame+lWristY],history[currentFrame+pelvisY],history[currentFrame+headY])))
#find the jump
percentageAnkleGain = returnPercentage(history[currentFrame+ lAnkleY],history[previousFrame+lAnkleY],history[previousFrame+headY])
percentageNeckGain = returnPercentage(history[currentFrame + neckY],history[previousFrame+neckY],history[previousFrame+headY])
percentagePelvisGain = returnPercentage(history[currentFrame + pelvisY],history[previousFrame+pelvisY],history[previousFrame+headY])
if ( percentageAnkleGain > .004 and percentageNeckGain > .1 and percentagePelvisGain >.1):
print('REALLLLLJUMPPP')
#pOwer strum
#currentAngle = get_angle([history[currentFrame+rWristX],history[currentFrame+rWristY]],[history[currentFrame+rShoulderX],history[currentFrame+rShoulderY]],[0,history[currentFrame+rShoulderY]])
#previousAngle = get_angle([history[previousFrame+rWristX],history[previousFrame+rWristY]],[history[previousFrame+rShoulderX],history[previousFrame+rShoulderY]],[0,history[previousFrame+rShoulderY]])
#print(currentAngle)
#if (currentAngle>=90 and currentAngle <=170) and previousAngle<=-90:
# print ('powerStrum')
#if left hand x pos crosses center line and right arm is reaching out
#if (list1[214] < list1[216] and (abs(list1[208]-list1[204])>abs(list1[214]-list1[202]))):
# print('reaching right')
#if (list1[208] > list1[216] and (abs(list1[214]-list1[210])>abs(list1[208]-list1[202]))):
# print('reaching left')
#TODO return 0,1,2
#update randomness of motion if we got all the major keypoints
if not (-1 in history[previousFrame:previousFrame+lAnkleX] or -1 in history[currentFrame:currentFrame+lAnkleX]):
check = np.sum(abs(np.subtract(history[previousFrame:previousFrame+lAnkleX],history[currentFrame:currentFrame+lAnkleX])))
motion[person].pop(0)
motion[person].append(check)
#print("motion: " + str(np.mean(motion[person])))
if (person == state['players']['drums']):#drummer
if len(drumHistory['left']) > 5:
drumHistory['left'].pop(0)
if len(drumHistory['right']) > 5:
drumHistory['right'].pop(0)
drumHistory['left'].append(checkDrumsDSP('left',lWristX,lWristY,history,currentFrame))
drumHistory['right'].append(checkDrumsDSP('right',rWristX,rWristY,history,currentFrame))
if drumHistory['left'][-1] and not drumHistory['left'][-2]:
print('left hand ' + str(drumHistory['left'][-1]))
if drumHistory['right'][-1] and not drumHistory['right'][-2]:
print('right hand ' + str(drumHistory['right'][-1]))
#begin while loop!
datum = op.Datum()
while True:
#need this to happen a bit after the program loads otherwise it throws a memory error
if loop ==3:
model=tf.keras.models.load_model('model_size6_aug_2.h5')
ret,img = stream.read()
if (img.any()):
#print(img)
datum.cvInputData = img
opWrapper.emplaceAndPop([datum])
keypoints = datum.poseKeypoints # chop off the confidence levels
#print(keypoints.shape)
if (keypoints.shape !=()):
list1 = list1 + returnNormalizedKeypoints(keypoints[0])
#two people
if (keypoints.shape[0]>1):
list2 = list2 + returnNormalizedKeypoints(keypoints[1])
#three people
if (keypoints.shape[0]>2):
list3 = list3 + returnNormalizedKeypoints(keypoints[2])
#checking to see if we have enough data on person 1 to make a prediction
if len(list1)==250:
if (state['mode']=='test'):
checkStrums(list1,1,frame5,frame4)
if (state['mode']=='play'):
checkForDSP(list1,1,frame5,frame4)
elif len(list1) ==300: #300 is because 6 seconds of data, 25 (x,y) coords per second...aka windowSize*50
if (state['mode']=='play'):
checkForDSP(list1,1,frame6,frame5)
checkForPose(list1,1)
if (state['mode']=='test'):
checkStrums(list1,1,frame5,frame4)
list1 = list1[100:]
if len(list2)==250:
if (state['mode']=='play'):
checkForDSP(list2,2,frame5,frame4)
elif len(list2) ==300:
if (state['mode']=='play'):
checkForDSP(list2,2,frame6,frame5)
checkForPose(list2,2)
list2 = list2[100:]
if len(list3)==250:
if (state['mode']=='play'):
checkForDSP(list3,3,frame5,frame4)
elif len(list3) ==300:
if (state['mode']=='play'):
checkForDSP(list3,3,frame6,frame5)
checkForPose(list3,3)
list3 = list3[100:]
loop+=1
#if loop == 100:
# break