-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkinect_cam3.py
79 lines (71 loc) · 2.19 KB
/
kinect_cam3.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
import SimpleCV as scv
import numpy as np
import subprocess as sp
import time
last_call = time.time()
#Arg must be string
def speak(arg):
global last_call
if isinstance(arg, str):
cmd = ["say", arg ]
elif isinstance(arg, list):
cmd=["say"] + arg
else:
cmd = None
print "Error: Unknown arg type"
if cmd:
if time.time() - last_call > 5:
sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE)
last_call = time.time()
#Get the normalized view by sampling initial view
def getNorm(cam, samples=2):
leftNorm = 0
rightNorm = 0
for i in range(samples):
dm = cam.getDepthMatrix()
h,w = dm.shape #h=480, w=640
left = dm[:, 0:w/2]
right = dm[:, w/2:]
leftNorm += np.sum(left)
rightNorm += np.sum(right)
leftNorm /= samples
rightNorm/= samples
return [leftNorm, rightNorm]
if __name__ == '__main__':
cam = scv.Kinect()
leftNorm, rightNorm = getNorm(cam)
#print "leftNorm is {}. rightNorm is {}".format(leftNorm, rightNorm)
leftMem = [leftNorm] * 5 #Memory
rightMem = [rightNorm] * 5 #Memory
while True:
depth = cam.getDepth()
depth.show()
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:]
leftWeight = np.sum(left)
centerWeight = np.sum(center)
rightWeight = np.sum(right)
"""
print leftWeight #/float(leftNorm)
print centerWeight
print rightWeight #/float(rightNorm)
print " "
"""
"""
if leftWeight > rightWeight:
print "Left"
else:
print "Right"
"""
r = centerWeight / float(rightWeight)
l = centerWeight / float(leftWeight)
if r > 1.2 or r < 0.8:
speak("Go right")
elif l > 1.2 or l < 0.8:
speak("Go left")
else:
pass #print "Straight"