def body(self, dev, *args): """Initialize movement variables""" acc = freenect.get_accel(dev) accX = int(acc[0]) accY = int(acc[1]) accZ = int(acc[2]) accX = accX - self.acceleration[0] accY = accY - self.acceleration[1] accZ = accZ - self.acceleration[2] self.velocity[0] += accX self.velocity[1] += accY self.velocity[2] += accZ self.acceleration[0] = int(acc[0]) self.acceleration[1] = int(acc[1]) self.acceleration[2] = int(acc[2]) if self.connected: self.send_kinect_info(self.velocity, [accX, accY, accZ]) if self.kill_kinect: freenect.shutdown(dev)
def run(self): try: ctx = freenect.init() for index in xrange(freenect.num_devices(ctx)): self.devs.append(freenect.open_device(ctx, index)) for device_num, dev in enumerate(self.devs): for stream_type in ('video', 'depth'): self.producers[device_num, stream_type] = \ KinectProducer(dev, device_num, stream_type, self) self.initialized.set() while self.keep_running: while not self.command_q.empty(): self.command_q.get()() if self._should_runloop(): freenect.base_runloop(ctx, self._body) else: self.command_q.get()() finally: with self.lock: self.keep_running = False for producer in self.producers.itervalues(): producer.stop() self.producers = {} freenect.shutdown(ctx) self.devs = [] self.initialized.set()
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 kill(): [ctx, dev] = initkinect() cv2.destroyAllWindows() freenect.sync_stop() freenect.stop_video(dev) freenect.stop_depth(dev) freenect.close_device(dev) freenect.shutdown(ctx) quit()
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 set_kinect_angle(angle, device_index=0): # Clamp angle to [-30, 30] angle = min(angle, max(angle, -30), 30) print "Setting Kinect angle to", angle # We have to stop the synchronous runloop to interact with the device. freenect.sync_stop() # Open the device ctx = freenect.init() dev = freenect.open_device(ctx, device_index) # Set angle freenect.set_tilt_degs(dev, angle) # Shutdown context, allowing synchronous runloop to start freenect.shutdown(ctx) return angle
def __init__(self, devno=0, resolution=RESOLUTION_MEDIUM, depth_mode=DEPTH_11BIT, video_mode=VIDEO_RGB): self._ctx = freenect.init() if not self._ctx: raise KinectError("Cannot connect to device.") self.devno = devno self._dev = freenect.open_device(self._ctx, self.devno) if not self._dev: freenect.shutdown(self._ctx) raise KinectError("Cannot open device.") self._depth_callback = None self._video_callback = None self._resolution = resolution self.depth_mode = depth_mode self.video_mode = video_mode
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 stop(self): freenect.close_device(self.dev) freenect.shutdown(self.ctx) print('kinect Stopped')
#!/usr/bin/python2.7 import freenect import cv2 import numpy as np import time from usb.core import find as finddev dev1 = finddev(idVendor=0x045e, idProduct=0x02ae) dev2 = finddev(idVendor=0x045e, idProduct=0x02b0) dev3 = finddev(idVendor=0x045e, idProduct=0x02ad) dev1.reset() dev2.reset() ctx = freenect.init() mdev = freenect.open_device(ctx, 0) freenect.shutdown(ctx) print(ctx) print(mdev) freenect.sync_stop() def applyCustomColorMap(im_gray): lut = np.zeros((256, 1, 3), dtype=np.uint8) lut[:, 0, 0] = [ 0, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 0, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 0, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 0, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 52, 49, 51, 0, 48, 48, 48, 46, 47, 47, 43, 45, 45, 45, 44, 44, 43, 43, 43, 0, 46, 45, 46, 45, 50, 50, 50, 51, 55, 56, 56, 56, 62, 61, 61, 0, 61, 68, 67, 67, 68, 73, 73, 73, 73, 78, 79,
def killkinect(ctx, dev): freenect.close_device(dev) freenect.shutdown(ctx)
def shutdown(self): if self._dev: freenect.close_device(self._dev) if self._ctx: freenect.shutdown(self._ctx)
cv.Resize(cv.fromarray(raw_image), cv.fromarray(image), cv.CV_INTER_AREA) cv.CvtColor(cv.fromarray(image), cv.fromarray(image), cv.CV_RGB2HSV) # Downsample the depth frame using nearest-neighbor to make sure # invalid pixels are handled properly. cv.Resize(cv.fromarray(raw_depth), cv.fromarray(depth), cv.CV_INTER_NN) # Do the object recognition color.identify(image, constants, colors) global balls, yellow_walls, green_walls balls = blobs.find_blobs(colors, depth, color=0) yellow_walls = blobs.find_blobs(colors, depth, color=1, min_size=100) green_walls = blobs.find_blobs(colors, depth, color=2, min_size=10) # Work around an initialization bug for synchronous image try: ctx = freenect.init() dev = freenect.open_device(ctx, 0) if not dev: raise Exception freenect.start_video(dev) # sync_get_video hangs if we don't do this freenect.start_depth(dev) # sync_get_depth hangs if we don't do this freenect.stop_depth(dev) freenect.stop_video(dev) freenect.close_device(dev) # close the device so that c_sync can open it freenect.shutdown(ctx) process_frame() initialized = True except: pass