├── README └── kinect_launcher.py /README: -------------------------------------------------------------------------------- 1 | Kinect USB Rocket launcher hack thing 2 | 3 | See it in action here: 4 | http://www.youtube.com/watch?v=Hz5OiQAmUpc 5 | -------------------------------------------------------------------------------- /kinect_launcher.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from freenect import sync_get_depth as get_depth, sync_get_video as get_video 3 | import cv 4 | import time 5 | import numpy as np 6 | from numpy import array 7 | 8 | import rocket_backend 9 | from time import sleep 10 | 11 | FAR = 700 12 | from time import time 13 | start = time() 14 | 15 | class Controller(object): 16 | def __init__(self): 17 | manager = rocket_backend.RocketManager() 18 | manager.acquire_devices() 19 | self.launcher = manager.launchers[0] 20 | self.armed = False 21 | 22 | def fire(self): 23 | if self.armed: 24 | self.launcher.issue_command(4) 25 | self.armed = False 26 | 27 | def left(self): 28 | self.launcher.issue_command(2) 29 | 30 | def right(self): 31 | self.launcher.issue_command(3) 32 | 33 | def up(self): 34 | self.launcher.issue_command(1) 35 | 36 | def down(self): 37 | self.launcher.issue_command(0) 38 | 39 | def stop(self): 40 | self.launcher.issue_command(5) 41 | 42 | def tick(self): 43 | self.launcher.check_limits() 44 | 45 | controller = Controller() 46 | 47 | def extract_blob(depth, field, x, y): 48 | stack = [(x, y)] 49 | left = right = x 50 | top = bottom = y 51 | width = len(field[0]) 52 | height = len(field) 53 | field[y][x] = 1 54 | while stack: 55 | x, y = stack.pop() 56 | if x < left: 57 | left = x 58 | elif x > right: 59 | right = x 60 | if y < top: 61 | top = y 62 | elif y > bottom: 63 | bottom = y 64 | for x, y in ((x-1, y), (x+1, y), (x, y-1), (x, y+1)): 65 | if 0 < x < width and 0 < y < height and depth[y][x] <= FAR and not field[y][x]: 66 | field[y][x] = 1 67 | stack.append((x, y)) 68 | return (left, right, top, bottom) 69 | 70 | def process(depth): 71 | height, width = depth.shape 72 | field = np.zeros_like(depth).tolist() 73 | depth = depth.tolist() 74 | r = np.zeros_like(depth).tolist() 75 | g = np.zeros_like(depth).tolist() 76 | b = np.zeros_like(depth).tolist() 77 | blobs = [] 78 | for y in xrange(0,height): 79 | row = depth[y] 80 | for x in xrange(0, width): 81 | z = row[x] 82 | if z > FAR: 83 | b[y][x] = 255 84 | continue 85 | if field[y][x]: 86 | r[y][x] = 255 87 | continue 88 | blobs.append(extract_blob(depth, field, x, y)) 89 | g[y][x] = 255 90 | DEAD = 100 91 | for blob in blobs: 92 | if time()-start < 10: 93 | break 94 | if blob[1]-blob[0] > 10 and blob[3]-blob[2] > 10: 95 | controller.armed = True 96 | print blob 97 | if blob[0] < DEAD: 98 | controller.left() 99 | elif blob[1] > width-DEAD: 100 | controller.right() 101 | elif blob[2] < DEAD*0.5: 102 | controller.up() 103 | elif blob[3] > height-DEAD*0.5: 104 | controller.down() 105 | else: 106 | controller.stop() 107 | break 108 | else: 109 | controller.fire() 110 | controller.tick() 111 | return r,g,b 112 | 113 | def doloop(): 114 | while True: 115 | # Get a fresh frame 116 | (depth,_), (rgb,_) = get_depth(), get_video() 117 | 118 | depth = depth[::2, ::2] 119 | r,g,b = process(depth) 120 | 121 | # Build a two panel color image 122 | d3 = np.dstack((r,g,depth/20)).astype(np.uint8) 123 | da = np.hstack((d3,rgb[::2, ::2])) 124 | 125 | # Simple Downsample 126 | cv.ShowImage('both',np.array(da[:,:,::-1])) 127 | cv.WaitKey(5) 128 | 129 | doloop() 130 | 131 | """ 132 | IPython usage: 133 | ipython 134 | [1]: run -i demo_freenect 135 | # (to interrupt the loop) 136 | [2]: %timeit -n100 get_depth(), get_rgb() # profile the kinect capture 137 | 138 | """ 139 | 140 | --------------------------------------------------------------------------------