def run(self): try: self.ctx = freenect.init() self.dev = freenect.open_device(self.ctx, 0) freenect.set_depth_mode(self.dev, freenect.RESOLUTION_MEDIUM, freenect.DEPTH_11BIT) freenect.set_depth_callback(self.dev, self._depth_cb) freenect.set_video_mode(self.dev, freenect.RESOLUTION_MEDIUM, freenect.VIDEO_RGB) freenect.set_video_callback(self.dev, self._video_cb) self.video_started = False self.depth_started = False while self.keep_running: with self.lock: if self.led_update is not None: freenect.set_led(self.dev, self.led_update) self.led_update = None self.update_streams() if not self.video_started and not self.depth_started: self.update_cond.wait() continue self.update.clear() if not self.keep_running: break freenect.base_runloop(self.ctx, self._body) finally: with self.lock: for k in self.depth_consumers.keys() + self.video_consumers.keys(): k.put(StreamerDied("The Kinect streamer died")) self.depth_consumers = {} self.video_consumers = {} self.update_streams() freenect.close_device(self.dev) freenect.shutdown(self.ctx)
def body(dev, ctx): global tilt global light global increment global last_time if not keep_running: raise freenect.Kill if time.time() - last_time < 3: return last_time = time.time() if tilt >= 30: increment = False if tilt <= 10: increment = True led = light freenect.set_led(dev, led) if search: if increment: tilt = tilt + 5 else: tilt = tilt - 5 freenect.set_tilt_degs(dev, tilt)
def body(dev,ctx): if sys.argv < 2: exit(0); global keep_running global myAngle global k global image char_set = string.ascii_uppercase + string.digits k = (cv.WaitKey(10) & 255) if k == 27: print "Exiting..." keep_running = False elif chr(k) == 'w' or chr(k) == 'W': myAngle = myAngle + 5 elif chr(k) == 's' or chr(k) == 'S': myAngle = myAngle - 5 elif chr(k) == 'p' or chr(k) == 'P': cv.SaveImage('positive/pos-'+ rand_string(10) + '.jpg',image) print "Positive image saved" elif chr(k) == 'n' or chr(k) == 'N': cv.SaveImage('negative/neg-'+ rand_string(10) + '.jpg',image) print "Negative image saved" freenect.set_led(dev,1) freenect.set_tilt_degs(dev,myAngle) if not keep_running: freenect.set_led(dev,0) raise freenect.Kill
def rotate(): led = 0 for i in range(31): freenect.set_tilt_degs(new_dev, i) print(f"iterataion number {i}") time.sleep(0.1) if i % 5 == 0: freenect.set_led(new_dev, led) led += 1
def body(dev, ctx): global last_time if time.time() - last_time < 3: return last_time = time.time() led = random.randint(0, 6) tilt = random.randint(0, 30) freenect.set_led(dev, led) freenect.set_tilt_degs(dev, tilt) print('led[%d] tilt[%d] accel[%s]' % (led, tilt, freenect.get_accel(dev)))
def body(dev, ctx): global last_time global led_color if not keep_running: raise freenect.Kill if time.time() - last_time < 1: return last_time = time.time() freenect.set_led(dev, led_color%3+1) print led_color led_color+=1
def body(dev, ctx): global last_time if not keep_running: raise freenect.Kill if time.time() - last_time < 3: return last_time = time.time() led = random.randint(0, 6) tilt = random.randint(0, 30) freenect.set_led(dev, led) freenect.set_tilt_degs(dev, tilt)
def body(dev, ctx): #global last_time if not keep_running: raise freenect.Kill #last_time = time.time() led = 1 tilt = int(input("Enter a degree 0 - 30: ")) #tilt = random.randint(0, 30) freenect.set_led(dev, led) freenect.set_tilt_degs(dev, tilt) print('led[%d] tilt[%d] accel[%s]' % (led, tilt, freenect.get_accel(dev)))
def _body(self, ctx): with self.lock: if self.update.isSet(): self.update_streams() if not self.video_started and not self.depth_started: raise freenect.Kill() self.update.clear() if not self.keep_running: raise freenect.Kill() if self.led_update is not None: freenect.set_led(self.dev, self.led_update) self.led_update = None
def tilt_and_sense(): global dev while not dev: time.sleep(1) while 1: time.sleep(3) led = random.randint(0, 6) tilt = random.randint(0, 30) print('Led[%d] Tilt[%d]' % (led, tilt)) freenect.set_led(dev, led) freenect.set_tilt_degs(dev, tilt) print(freenect.raw_accel(dev)) print(freenect.mks_accel(dev))
def body(dev, ctx): global last_time print "Doing something" if not keep_running: raise freenect.Kill if time.time() - last_time < 3: return last_time = time.time() led = random.randint(0, 6) tilt = 30 #random.randint(0, 30) freenect.set_led(dev, led) freenect.set_tilt_degs(dev, tilt) print('led[%d] tilt[%d] accel[%s]' % (led, tilt, freenect.get_accel(dev)))
def __init__(self, cam=-1): # Initialize freenect and get the context print 'Initalize kinect' context = freenect.init() # Open the device and get the device print 'Open device' self.kinect = freenect.open_device(context, 0) # Turn the led off print 'Turning the led off' freenect.set_led(self.kinect, freenect.LED_OFF) # Close the device print 'Closing the device' freenect.close_device(self.kinect)
def body(dev, ctx): global last_time if time.time() - last_time > 3 and not has_face: last_time = time.time() print 'NO FACE FOUND - Randomly tilting to see if I can find one..' led = random.randint(0, 6) freenect.set_led(dev, led) tilt = random.randint(0, 30) freenect.set_tilt_degs(dev, tilt) if not keep_running: raise freenect.Kill
def mybody(*args): dev, ctx = args global last_time print(last_time) if not keep_running: raise freenect.Kill if time.time() - last_time < 3: return last_time = time.time() led = random.randint(0, 6) tilt = random.randint(0, 30) freenect.set_led(dev, led) freenect.set_tilt_degs(dev, tilt) print('led[%d] tilt[%d] accel[%s]' % (led, tilt, freenect.get_accel(dev)))
def body(dev, ctx): global last_time global led global tilt if not keep_running: raise freenect.Kill if led > 6 and tilt > 30: return freenect.set_led(dev, led) freenect.set_tilt_degs(dev, tilt) print('led[%d] tilt[%d] accel[%s]' % (led, tilt, freenect.get_accel(dev))) time.sleep(1) led = led + 1 tilt = tilt + 5
def body(dev, ctx): global last_time if not keep_running: raise freenect.Kill if time.time() - last_time < 3: return print "boop" last_time = time.time() led = random.randint(0, 6) tilt = random.randint(0, 30) print "set_led(%s)" % led freenect.set_led(dev, led) print "set_tilt_degs(%s)" % tilt freenect.set_tilt_degs(dev, tilt) print('led[%d] tilt[%d] accel[%s]' % (led, tilt, freenect.get_accel(dev)))
def rand_led_tilt(dev, ctx): """Randomly changes the led and tilt position every 3 seconds""" global last_time if time.time() - last_time < 3: return last_time = time.time() led = random.randint(0, 6) tilt = random.randint(0, 30) freenect.set_led(dev, led) freenect.set_tilt_degs(dev, tilt) print('led[%d] tilt[%d] accel[%s]' % (led, tilt, freenect.get_accel(dev)))
def body(dev): global last_time if not keep_running: raise freenect.Kill if time.time() - last_time < 3: return last_time = time.time() for i in range(0, 6): led = i tilt = i * 5 time.sleep(3) freenect.set_led(dev, led) freenect.set_tilt_degs(dev, tilt) print('led[%d] tilt[%d] accel[%s]' % (led, tilt, freenect.get_accel(dev)))
def body_callback(self, dev, ctx): # _set_led hackery is required because for some reason calling set_led # from update loop hangs the process if self._set_led: freenect.set_led(dev, self._set_led) self._set_led = None if self._set_video is not None: freenect.stop_video(dev) freenect.set_video_mode(dev, freenect.RESOLUTION_MEDIUM, self._set_video) freenect.start_video(dev) self._set_video = None if self._last_setting_check + self.settings_delay < time.time(): self.load_settings() self._last_setting_check = time.time()
def set_tilt(): TILT_MAX = 30 TILT_STEP = 10 TILT_START = 0 ctx = freenect.init() dev = freenect.open_device(ctx, freenect.num_devices(ctx) - 1) if not dev: freenect.error_open_device() print "Starting TILT Cycle" for tilt in xrange(TILT_START, TILT_MAX + TILT_STEP, TILT_STEP): print "Setting TILT: ", tilt freenect.set_led(dev, 6) freenect.set_tilt_degs(dev, tilt) time.sleep(3) freenect.set_tilt_degs(dev, 0)
def body(dev, ctx): global last_time global tilt global light if not keep_running: raise freenect.Kill if tilt == 30: return if time.time() - last_time < 3: return last_time = time.time() led = light freenect.set_led(dev, led) freenect.set_tilt_degs(dev, tilt) print('led[%d] tilt[%d] accel[%s]' % (led, tilt, freenect.get_accel(dev)))
def testkinect(): # test leds and tilts [ctx, dev] = initkinect() for i in range(1, 6): freenect.set_led(dev, i) cv2.waitKey(2000) freenect.set_tilt_degs(dev, 0) print('complete led') cv2.waitKey(3000) #test tilt freenect.set_tilt_degs(dev, -50) cv2.waitKey(3000) freenect.set_tilt_degs(dev, 50) cv2.waitKey(3000) freenect.set_tilt_degs(dev, 0) cv2.waitKey(1) print('complete tilt') freenect.shutdown(ctx)
def body(dev, ctx): global last_time, tilt0 if not keep_running: raise freenect.Kill if time.time() - last_time < 1: return last_time = time.time() led = 2 if faceFound and abs(tilt - tilt0) > 0: led = 3 if faceFound and abs(tilt - tilt0) == 0: led = 4 tilt0 = tilt freenect.set_led(dev, led) freenect.set_tilt_degs(dev, tilt) print('led[%d] tilt[%d] accel[%s]' % (led, tilt, freenect.get_accel(dev))) if not keep_running: raise freenect.Kill
def run(self): try: self.ctx = freenect.init() self.dev = freenect.open_device(self.ctx, 0) freenect.set_depth_mode(self.dev, freenect.RESOLUTION_MEDIUM, freenect.DEPTH_11BIT) freenect.set_depth_callback(self.dev, self._depth_cb) freenect.set_video_mode(self.dev, freenect.RESOLUTION_MEDIUM, freenect.VIDEO_RGB) freenect.set_video_callback(self.dev, self._video_cb) self.video_started = False self.depth_started = False while self.keep_running: with self.lock: if self.led_update is not None: freenect.set_led(self.dev, self.led_update) self.led_update = None self.update_streams() if not self.video_started and not self.depth_started: self.update_cond.wait() continue self.update.clear() if not self.keep_running: break freenect.base_runloop(self.ctx, self._body) finally: with self.lock: for k in self.depth_consumers.keys( ) + self.video_consumers.keys(): k.put(StreamerDied("The Kinect streamer died")) self.depth_consumers = {} self.video_consumers = {} self.update_streams() freenect.close_device(self.dev) freenect.shutdown(self.ctx)
def body(dev, ctx): global last_time if not keep_running: raise freenect.Kill if time.time() - last_time < 3: return last_time = time.time() # rotate through the different colors if led > 6: led = 0 else: led = led + 1 freenect.set_led(dev, led) # tilt back and forth if (tilt > 30 or tilt < 0): tilt = 0 if (time.time() % 30 < 15): tilt = tile + 5 else: tilt = tilt - 5 freenect.set_tilt_degs(dev, tilt) print('led[%d] tilt[%d] accel[%s]' % (led, tilt, freenect.get_accel(dev)))
def set_led(self, leds): return freenect.set_led(self._dev, leds)
def body(dev,ctx): global hold_it_start, action_less_start, setting_center_start global hold_it, center_set, active_area, exclusion_zone, trying_to_set_center global old_ts, old_xy, center_xy, center_z, exclusion_xy global action_set, action_num if ts<>old_ts: #pygame and freenect use different indexing and origin depth=np.flipud(depth_raw.transpose()) #eliminate low objects #depth[:,240:]=2047 if active_area: mask=(x_grid-old_xy[0])**2+(y_grid-old_xy[1])**2>active_area_limit**2 depth[mask]=2047 depth_flat=depth.ravel() depth_flat=np.sort(depth_flat) itemindex=np.where(depth<=depth_flat[num_to_track]) final_xy=[int(itemindex[0][:num_to_track].mean()),int(itemindex[1][:num_to_track].mean())] final_z=int(depth_flat[0:num_to_track].mean()) dist_xy=rect_to_polar([final_xy[0]-old_xy[0],-(final_xy[1]-old_xy[1])]) rel_xy=rect_to_polar([final_xy[0]-center_xy[0],-(final_xy[1]-center_xy[1])]) rel_z=center_z-final_z rightnow=datetime.now() if dist_xy[0]>huge_motion_limit: hold_it=False center_set=False freenect.set_led(dev,freenect.LED_OFF) #Turn led off active_area=False elif center_set: if hold_it: if action_is_digital[action_set][action_num]: if rightnow-hold_it_start>hold_it_limit: if rel_xy[0]<r_medium: hold_it=False freenect.set_led(dev,leds[action_set][1]) action_less_start=rightnow elif rel_xy[0]>r_medium and rel_xy[0]<r_large: hold_it=False action_less_start=rightnow else: hold_it=False center_set=False freenect.set_led(dev,freenect.LED_OFF) #Turn led off active_area=False else: if rel_xy[0]<r_medium: hold_it=False freenect.set_led(dev,leds[action_set][1]) action_less_start=rightnow elif rel_xy[0]>r_medium and rel_xy[0]<r_large: if action_num==bisect(limits[action_set],rel_xy[1]) and rightnow-hold_it_start>hold_it_limit: perform(action_set,actions[action_set][action_num]) else: hold_it=False action_less_start=rightnow else: hold_it=False center_set=False freenect.set_led(dev,freenect.LED_OFF) #Turn led off active_area=False # elif rel_z<-z_switch_controller and dist_xy[0]<r_central_button: # action_num=-1 # action_set=action_set+1 # if action_set==len(actions): # action_set=0 # freenect.set_led(dev,leds[action_set][1]) # hold_it=True # hold_it_start=rightnow elif rel_z>z_main_action and dist_xy[0]<r_central_button: action_num=-2 perform(action_set,actions[action_set][action_num]) hold_it=True hold_it_start=rightnow elif rel_xy[0]>r_medium: action_num=bisect(limits[action_set],rel_xy[1]) freenect.set_led(dev,leds[action_set][2]) perform(action_set,actions[action_set][action_num]) hold_it=True hold_it_start=rightnow elif rightnow-action_less_start>action_less_limit: center_set=False freenect.set_led(dev,freenect.LED_OFF) #Turn led off exclusion_zone=True exclusion_xy=final_xy active_area=False elif trying_to_set_center: if rightnow-setting_center_start>center_setting_limit: trying_to_set_center=False center_set=True freenect.set_led(dev,leds[action_set][1]) #Turn led solid active_area=True center_xy=final_xy center_z=final_z action_less_start=rightnow elif dist_xy[0]>r_small: trying_to_set_center=False freenect.set_led(dev,freenect.LED_OFF) #Turn led off elif dist_xy[0]<r_small: if (not exclusion_zone) or (rect_to_polar([final_xy[0]-exclusion_xy[0],final_xy[1]-exclusion_xy[1]])[0]>exclusion_zone_limit): trying_to_set_center=True freenect.set_led(dev,leds[action_set][0]) #Turn led blinking setting_center_start=rightnow exclusion_zone=False old_xy=final_xy old_ts=ts #PAINTING screen.fill(BLACK) #Make the window black pygame.draw.circle(screen,(255,min(255,max(0,255*(1000.0-final_z)/500)),0),(final_xy[0],final_xy[1]),10) if center_set: paint_controller(center_xy) if active_area: pygame.draw.circle(screen,YELLOW,(old_xy[0],old_xy[1]),active_area_limit,2) if exclusion_zone: pygame.draw.circle(screen,RED,(exclusion_xy[0],exclusion_xy[1]),exclusion_zone_limit,2) font = pygame.font.Font(None, 48) if hold_it: ftext=actions[action_set][action_num]+"! Holding for "+str((rightnow-hold_it_start).seconds)+" / "+str(hold_it_limit.seconds) elif center_set: ftext="Set for "+str((rightnow-action_less_start).seconds)+" / "+str(action_less_limit.seconds)+" "+str(int(rel_xy[0]))+" / "+str(r_medium) elif trying_to_set_center: ftext="Trying for "+str((rightnow-setting_center_start).seconds)+" / "+str(center_setting_limit.seconds) else: ftext="Searching..." text = font.render(ftext,1,YELLOW) screen.blit(text, (20,20)) screen.blit(screen,(0,0)) #Updates the main screen --> screen pygame.display.flip() #Updates everything on the window for e in pygame.event.get(): #Iterates through current events if e.type is pygame.QUIT: #If the close button is pressed, the while loop ends raise freenect.Kill
YELLOW = (255,255,0) pygame.init() #Initiates pygame xSize,ySize = 640,480 #Sets size of window screen = pygame.display.set_mode((xSize,ySize),pygame.RESIZABLE) #creates main surface #KINECT depth_raw=None num_to_track=1000 #Number of points to use to determine where the closest 'thing' is x_grid,y_grid=np.ogrid[0:xSize,0:ySize] ctx=freenect.init() #Start up the kinect dev=freenect.open_device(ctx,freenect.LED_OFF) #Pointer to the device itself used for led handling freenect.set_led(dev,0) #Turn led off freenect.close_device(dev) #Release the kinect #CONTROLLER r_small=15 r_medium=75 r_large=125 r_central_button=50 z_main_action=50 z_switch_controller=50 huge_motion_limit=40 active_area_limit=50 exclusion_zone_limit=50
def set_led_and_tilt(dev, ctx): freenect.set_led(dev, 2) freenect.set_tilt_degs(dev, -10) time.sleep(3) raise freenect.Kill
def body(dev, ctx): freenect.set_led(dev, led) freenect.set_tilt_degs(dev, tilt)
def set_led(self, led_state): if not self.dev: print "no device set!" return freenect.set_led(self.dev, led_state)
def set_led(self, ledstate): self.add_command(lambda : [freenect.set_led(dev, ledstate) for dev in self.devs])
def doloop(): #Series of commands to do pointer operations on the kinect (motor, led, accelerometer) ctx = init() #Initiates device mdev = open_device(ctx, 0) #Opens the device for commands set_led(mdev, 1) #Sets LED to green close_device( mdev) #Closes device. Device must be closed immediately after usage #Mean filter caches yList = [0, 0, 0, 0, 0, 0] xList = [0, 0, 0, 0, 0, 0] #Sets color tuples RED = (255, 0, 0) BLUE = (0, 0, 255) TEAL = (0, 200, 100) BLACK = (0, 0, 0) #Sets the size of the screen xSize = 640 ySize = 480 done = False #Main while loop bool counter pygame.init() #Initiates pygame screen = pygame.display.set_mode( (xSize, ySize), pygame.RESIZABLE) #Creates the pygame window screen.fill(BLACK) #Fills the window black #Initiates the xTempPos and yTempPos values so that the point will remain stationary #if the minimum value is larger than 600 xTempPos = xSize / 2 yTempPos = ySize / 2 global depth, rgb #Makes the depth and rgb variables global while not done: screen.fill(BLACK) #Makes the pygame window black after each iteration # Get a fresh frame (depth, _) = get_depth() (rgb, _) = get_video() minVal = np.min(depth) #This is the minimum value from the depth image minPos = np.argmin( depth) #This is the raw index of the minimum value above xPos = np.mod(minPos, xSize) #This is the x component of the raw index yPos = minPos // xSize #This is the y component of the raw index #This is the mean filter process """ A mean filter works by collecting values in a cache list and taking the mean of them to determine the final value. It works in this case to decrease the amount of volatility the minimum position experiences to get a smoother display with a more consistent value. My computer works smoothly with a 5 bit cache where as a faster computer may need a larger cache and a slower computer may need a smaller cache """ xList.append(xPos) del xList[0] xPos = int(mean(xList)) yList.append(yPos) del yList[0] yPos = int(mean(yList)) """ This if statement says that if the minimum value is below 600 to store the minimum positions in xTempPos and yTempPos and to make the dot color red. Also if the minimum value is larger than 600, xPos and yPos become the last stored minimum and maximum positions. It also changes the color to purple """ if minVal < 600: xTempPos = xPos yTempPos = yPos COLOR = cv.RGB(255, 0, 0) else: xPos = xTempPos yPos = yTempPos COLOR = cv.RGB(100, 0, 100) #cv.Circle(rgb, (xPos, yPos), 2, COLOR, 40) #draws a circle of a certain color at minimum position #cv.ShowImage('Image',rgb) #Shows the image cv.WaitKey(5) #Keyboard interupt """ The if statement below sets up the virtual joystick by basically breaking the pygame window into four parts. A dot representing the minimum position is drawn on the window and the corresponding button based on the position is "pressed". The quarter of the window in which the button "pressed" corresponds to turns teal after being "pressed" Top Right : A Bottom Right: B Bottom Left : Y Top Right : X """ if xPos <= xSize / 2 and yPos <= ySize / 2: command = 'A' rect1 = pygame.Rect((xSize / 2, 0), (xSize / 2, ySize / 2)) pygame.draw.rect(screen, TEAL, rect1) elif xPos <= xSize / 2 and yPos > ySize / 2: command = 'B' rect1 = pygame.Rect((xSize / 2, ySize / 2), (xSize / 2, ySize / 2)) pygame.draw.rect(screen, TEAL, rect1) elif xPos > xSize / 2 and yPos <= ySize / 2: command = 'X' rect1 = pygame.Rect((0, 0), (xSize / 2, ySize / 2)) pygame.draw.rect(screen, TEAL, rect1) else: command = 'Y' rect1 = pygame.Rect((0, ySize / 2), (xSize / 2, ySize / 2)) pygame.draw.rect(screen, TEAL, rect1) pygame.draw.line( screen, BLUE, (xSize / 2, ySize / 2), (xSize - xPos, yPos)) #Draws a line from the middle to the minimum position pygame.draw.circle(screen, RED, (xSize - xPos, yPos), 10) #Draws the circle on pygame window pygame.display.flip() #Displays the processed pygame window print command, minVal #Prints the "pressed" button and the minimum value for e in pygame.event.get(): #Itertates through current events if e.type is pygame.QUIT: #If the close button is pressed, the while loop ends done = True
def main(): grip_dir = os.environ['GRIP_DATA'] global g_model g_model = IO.load(os.path.join(grip_dir, 'aam.new.io'))[1] global g_predictor, reference_3d, geo_vs, geo_vts, rect rect = None pred_fn = os.path.join(grip_dir, 'pred.new.io') g_predictor = Face.load_predictor(pred_fn) #, cutOff=15) reference_shape = g_predictor['ref_shape'] size = reference_shape.shape[0] geo_vs = np.zeros((size, 3), dtype=np.float32) geo_vs[:size, :2] = reference_shape geo_vts = np.zeros((size, 2), dtype=np.float32) geo_vts[:size] = reference_shape + 0.5 geo_ts = np.array([[1, 0, 0, 0], [0, 1, 0, 1000], [0, 0, 1, 0]], dtype=np.float32) geo_fs = Face.triangulate_2D(reference_shape) geo_bs = [] for p0, p1, p2 in geo_fs: geo_bs.append((p0, p1)) geo_bs.append((p1, p2)) geo_bs.append((p2, p0)) reference_3d = np.zeros((reference_shape.shape[0], 3), dtype=np.float32) reference_3d[:, :2] = reference_shape * [100, 100] img_vs = np.array([[0, 0, 0], [640, 0, 0], [640, 480, 0], [0, 480, 0]], dtype=np.float32) img_vts = np.array([[0, 1], [1, 1], [1, 0], [0, 0]], dtype=np.float32) img_fs = np.array([[0, 1, 2, 3]], dtype=np.int32) img_ts = np.array([[1, 0, 0, 0], [0, 1, 0, 1000], [0, 0, 1, 0]], dtype=np.float32) geo_mesh = GLMeshes(names=['geo_mesh'], verts=[geo_vs], faces=[geo_fs], transforms=[geo_ts], bones=[geo_bs], vts=[geo_vts]) img_mesh = GLMeshes(names=['img_mesh'], verts=[img_vs], faces=[img_fs], transforms=[img_ts], bones=[None], vts=[img_vts]) kinect = freenect.init() tilt, roll = 0, 0 if 1: kdev = freenect.open_device(kinect, 0) freenect.set_led(kdev, 0) # turn off LED freenect.set_tilt_degs(kdev, 25) kstate = freenect.get_tilt_state(kdev) freenect.update_tilt_state(kdev) tilt_angle, tilt_status = kstate.tilt_angle, kstate.tilt_status ax, ay, az = kstate.accelerometer_x, kstate.accelerometer_y, kstate.accelerometer_z #bottom facing down: (85, 743, 369, 52, 0) #right side down: (916, 71, 96, 112, 0) #front side down: (52, 63, -863, -128, 0) freenect.close_device(kdev) y_axis = np.array((ax, ay, az), dtype=np.float32) y_axis = y_axis / np.linalg.norm(y_axis) roll = np.degrees(np.arctan2(ax, ay)) tilt = -np.degrees(np.arctan2(az, (ax**2 + ay**2)**0.5)) fovX = 62.0 pan_tilt_roll = (0, tilt, roll) tx_ty_tz = (0, 1000, 6000) P = Calibrate.composeP_fromData((fovX, ), (pan_tilt_roll), (tx_ty_tz), 0) global g_camera_rays, g_camera_mat h, w = 480 // 2, 640 // 2 coord, pix_coord = make_coords(h, w) #P = np.eye(3,4,dtype=np.float32) #P[0,0] = P[1,1] = 2.0 k1, k2 = 0, 0 g_camera_mat = Calibrate.makeMat(P, (k1, k2), [w, h]) K, RT, P, ks, T, wh = g_camera_mat coord_undist = coord.copy() Calibrate.undistort_points_mat(coord.reshape(-1, 2), g_camera_mat, coord_undist.reshape(-1, 2)) g_camera_rays = np.dot(coord_undist, RT[:2, :3]) # ray directions (unnormalized) g_camera_rays -= np.dot([-K[0, 2], -K[1, 2], K[0, 0]], RT[:3, :3]) g_camera_rays /= (np.sum(g_camera_rays**2, axis=-1)**0.5).reshape( h, w, 1) # normalized ray directions names = ['kinect'] vs = [np.zeros((h * w, 3), dtype=np.float32)] ts = [np.eye(3, 4, dtype=np.float32)] vts = [pix_coord * (1.0 / w, 1.0 / h)] faces = [make_faces(h, w)] mats = None geom_mesh = GLMeshes(names=names, verts=vs, faces=faces, transforms=ts, vts=vts) layers = { 'geom_mesh': geom_mesh, 'geo_mesh': geo_mesh, 'img_mesh': img_mesh } QGLViewer.makeViewer(layers=layers, mats=mats, callback=cb, timeRange=(0, 10000))