-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepth
executable file
·82 lines (66 loc) · 2.09 KB
/
depth
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
#!/usr/bin/env python
import freenect
import matplotlib.pyplot as mp
import numpy
import frame_convert
import signal
from multiprocessing import Process, Value
import time
import BaseHTTPServer
import json
keep_running = True
def get_depth():
depth = frame_convert.pretty_depth(freenect.sync_get_depth()[0])
#min = depth.argmin()
#where = numpy.where(depth == min, True, False)
#count = numpy.count_nonzero(where == True)
#print min, count
min = depth.argmin()
minloc = numpy.unravel_index(min, depth.shape)
y = float(minloc[0])/float(depth.shape[0])
x = float(minloc[1])/float(depth.shape[1])
return depth, x, y, min
def handler(signum, frame):
"""Sets up the kill handler, catches SIGINT"""
global keep_running
keep_running = False
def http_handler_factory(sh_x, sh_y, sh_min):
class HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("content-type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
self.wfile.write(json.dumps({
'x': sh_x.value,
'y': sh_y.value,
'min': sh_min.value,
}))
return HTTPHandler
def consumer(sh_x, sh_y, sh_min):
server_address=('0.0.0.0', 8888)
handler = http_handler_factory(sh_x, sh_y, sh_min)
httpd = BaseHTTPServer.HTTPServer(server_address, handler)
while True:
httpd.handle_request()
print sh_x.value, sh_y.value, sh_min.value
mp.ion()
mp.gray()
mp.figure(1)
image_depth = mp.imshow(get_depth()[0], interpolation='nearest', animated=True)
print('Press Ctrl-C in terminal to stop')
signal.signal(signal.SIGINT, handler)
sh_x = Value('d', 0.0)
sh_y = Value('d', 0.0)
sh_min = Value('d', 0.0)
p = Process(target=consumer, args=(sh_x, sh_y, sh_min))
p.start()
while keep_running:
mp.figure(1)
depth, x, y, min = get_depth()
image_depth.set_data(depth)
sh_x.value = x
sh_y.value = y
sh_min.value = min
mp.draw()
mp.waitforbuttonpress(0.01)