-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkinect_raw1.py
123 lines (95 loc) · 2.84 KB
/
kinect_raw1.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
import SimpleCV as scv
import numpy as np
cam = scv.Kinect()
idxmap=["All", "Left", "Center", "Right"]
def avg(depth=None):
if not depth:
depth = cam.getDepth()
mat = depth.getNumpy()
mean = np.sum(mat)/mat.size
return mean
def show():
depth = cam.getDepth()
depth.show()
return depth
#avg(show())
"""
Does average using 11 bit matrix
"""
def avg2():
dm=cam.getDepthMatrix()
#Get depth and height
h,w = dm.shape #h=480, w=640
left = dm[:, 0:w/2]
center = dm[:, w/4:3*w/4]
right = dm[:, w/2:]
leftMean = np.sum(left)/left.size
centerMean = np.sum(center)/center.size
rightMean = np.sum(right)/right.size
mean = np.sum(dm)/dm.size
#return (mean, leftMean, centerMean, rightMean)
return np.array([mean, leftMean, centerMean, rightMean])
def getmax():
oldval = None
val = None
while True:
val = idxmap[avg2().argmax()]
if oldval != val:
print val
oldval = val
"""
import kinect_raw as kr
kr.getmax2()
"""
def getmax2():
mlen = 5
mptr = 0
mem = [avg2() for i in range(mlen)]
maxArray = []
oldidx = None
idx = None
while True:
mem[mptr] = avg2()
print mem
maxArray = map(lambda arr: arr.argmax(), mem) #contains index of max element
return maxArray
bins = np.bincount(maxArray)
print idxmap[bins.argmax()]
"""
if oldval != val:
print val
oldval = val
"""
mptr = (mptr + 1)%mlen
def diff():
mlen = 5
midx = 0
mem = [None]*mlen
for i in range(mlen):
mem[i] = avg2() #Initialize
olddiff = mem[4] - mem[0]
oldidx = olddiff.argmax()
while True:
mem[midx] = avg2()
newdiff = mem[midx] - mem[(midx + 1)%mlen] #newest - oldest
newidx = newdiff.argmax() #where distance has increased the most
if np.fabs(newdiff[newidx] - olddiff[oldidx]) > 30:
print "{}: NEW:{} OLD:{}".format(idxmap[newidx], newdiff, olddiff)
olddiff = newdiff
oldidx = newidx
midx = (midx+1)%mlen #increment midx
def diff2():
idxmap=["All", "Left", "Center", "Right"]
mlen = 5
midx = 0
mem = [avg2() for i in range(mlen)] #Initialize
oldidx = -1
while True:
mem[midx] = avg2() #Store value
delta = mem[midx] - mem[(midx-1)%mlen] #get diff between newest and next-to-newest
deltaAbs = np.fabs(delta)
maxidx = deltaAbs.argmax() #idx where distance has changed the most
if deltaAbs[maxidx] > 30 and oldidx != maxidx:
print "{}".format(idxmap[maxidx])
oldidx = maxidx
midx = (midx+1)%mlen #increment midx