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 depth(dev, depth, timestamp): global IS_DEPTH_FORMAT_SET, KEEP_RUNNING, SAVE_VIDEO_CLOSE, SAVE_VIDEO_FAR, NP_VIDEO_PACKETS if not IS_DEPTH_FORMAT_SET: freenect.set_depth_mode(dev, freenect.RESOLUTION_HIGH, freenect.DEPTH_REGISTERED) IS_DEPTH_FORMAT_SET = True print("Depth:") print(np.unique(depth)) print(np.unique(depth).shape) return # this probably was the first frame -> continue to the next frame depth_close = depth_transforms.accurate_depth_image(depth, 545) depth_far = depth_transforms.accurate_depth_image(depth, 800) if SAVE_VIDEO_CLOSE: NP_VIDEO_PACKETS.append(depth_close) elif SAVE_VIDEO_FAR: NP_VIDEO_PACKETS.append(depth_far) utils.cv2_window_freeratio(depth_close, "54.5-80cm") utils.cv2_window_freeratio(depth_far, "80-105.5cm") # HANDLING KEYBOARD EVENTS # - "q", "esc" -> quit program # - "c" -> start video close # - "f" -> start video far pressed_k = cv2.waitKey(10) if pressed_k in (27, ord("q")): # esc or "q" for quit KEEP_RUNNING = False elif pressed_k == ord("c"): # "c" start video close if SAVE_VIDEO_CLOSE and not SAVE_VIDEO_FAR: SAVE_VIDEO_CLOSE = False stop_recording(NP_VIDEO_PACKETS, "close") NP_VIDEO_PACKETS = [] else: SAVE_VIDEO_CLOSE = True if SAVE_VIDEO_FAR: print( "You are currently recording a long-range video. Stop it before trying..." ) else: print("Close-range recording has started...") elif pressed_k == ord("f"): # "f" start video far if SAVE_VIDEO_FAR and not SAVE_VIDEO_CLOSE: SAVE_VIDEO_FAR = False stop_recording(NP_VIDEO_PACKETS, "far") NP_VIDEO_PACKETS = [] else: SAVE_VIDEO_FAR = True if SAVE_VIDEO_CLOSE: print( "You are currently recording a close-range video. Stop it before trying..." ) else: print("Long-range recording has started...")
def display_depth(dev, data, timestamp): global keep_running freenect.set_depth_mode(dev, freenect.RESOLUTION_HIGH, freenect.DEPTH_REGISTERED) #print(np.min(data)) p.WrapMessage(str(np.min(data))) if (np.min(data) < 350 or np.min(data) == 2047): p.Backlight(0) else: p.Backlight(1) if cv.WaitKey(10) == 27: keep_running = False
def __init__(self, dev, device_num, stream_type, streamer): self.dev = dev self.device_num = device_num self.stream_type = stream_type self.streamer = streamer self.consumers = set() # set([KinectConsumer]) self.lock = threading.Lock() self.frame = 0 # frame counter self.producing = False # true if freenect is sending us data assert stream_type in ('depth', 'video') if stream_type == 'depth': freenect.set_depth_mode(dev, freenect.RESOLUTION_MEDIUM, freenect.DEPTH_11BIT) freenect.set_depth_callback(dev, self._data_cb) elif stream_type == 'video': freenect.set_video_mode(dev, freenect.RESOLUTION_MEDIUM, freenect.VIDEO_RGB) freenect.set_video_callback(dev, self._data_cb)
def __init__(self, kinectid=1): self.kinectid = kinectid self.rgb = None self.depth = None self.keep_running = True # init Kenect self.context = freenect.init() self.device = freenect.open_device(self.context, self.kinectid) self.init = freenect.set_depth_mode(self.device, freenect.RESOLUTION_MEDIUM, freenect.DEPTH_REGISTERED)
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 depth(dev, depth, timestamp): global IS_DEPTH_FORMAT_SET, KEEP_RUNNING, SAVE_VIDEO_CLOSE, SAVE_VIDEO_FAR, NP_VIDEO_PACKETS, DEPTH_CALIBRATED, DEPTH_CALIBRATED_MM if not IS_DEPTH_FORMAT_SET: freenect.set_depth_mode(dev, freenect.RESOLUTION_HIGH, freenect.DEPTH_REGISTERED) IS_DEPTH_FORMAT_SET = True #print("Depth:") #print(np.unique(depth)) #print(np.unique(depth).shape) return # this probably was the first frame -> continue to the next frame # manual set of the calibrated_MM #depth_close = depth_transforms.accurate_depth_image(depth, 545) depth_close = depth_transforms.accurate_depth_image( depth, DEPTH_CALIBRATED_MM) if SAVE_VIDEO_CLOSE: NP_VIDEO_PACKETS.append(depth_close) utils.cv2_window_freeratio(depth_close, "kinect depth map") if not DEPTH_CALIBRATED: if calibrate_depth(depth_close, value=170, epsilon=2, mode="median"): DEPTH_CALIBRATED = True print( "Calibration finished! Feel free to make actions on the canvas interface." ) #cv2.destroyWindow("calibrate MM") else: DEPTH_CALIBRATED_MM += 1 return else: depth_close_detected_actions = process_depth_frame(depth_close) window_name_cm = f"{DEPTH_CALIBRATED_MM/10:.1f}-{DEPTH_CALIBRATED_MM/10 + 25.5:.1f}cm" utils.cv2_window_freeratio(depth_close_detected_actions, f"{window_name_cm} detected action") # HANDLING KEYBOARD EVENTS # - "q", "esc" -> quit program # - "v" -> start video recording # - "c" -> start calibration pressed_k = cv2.waitKey(10) if pressed_k in (27, ord("q")): # esc or "q" for quit KEEP_RUNNING = False elif pressed_k == ord("v"): # "v" start video recording if SAVE_VIDEO_CLOSE: SAVE_VIDEO_CLOSE = False stop_recording(NP_VIDEO_PACKETS, "close") NP_VIDEO_PACKETS = [] else: SAVE_VIDEO_CLOSE = True print("Close-range recording has started...") elif pressed_k == ord("c"): # "c" start calibration cv2.destroyWindow(window_name_cm) cv2.destroyWindow(f"{window_name_cm} detected action") DEPTH_CALIBRATED = False print("Calibration started...")
#!/usr/bin/env python import freenect import cv2 import frame_convert2 import numpy as np cv2.namedWindow('Depth') cv2.namedWindow('RGB') keep_running = True mdev = freenect.open_device(freenect.init(),0) freenect.set_depth_mode(mdev,freenect.RESOLUTION_MEDIUM,freenect.DEPTH_REGISTERED) lower = 0 upper = 100 def display_depth(dev, data, timestamp): global keep_running,lower,upper data = 200 * np.logical_and(data > lower, data < upper) data = data.astype(np.uint8) canny = cv2.Canny(data, 100, 255) cv2.imshow('Depth',data) cv2.imshow('RGB',canny) lower+=10 upper+=10 if upper>1900: lower = 0 upper = 100 if cv2.waitKey(10) == 27: keep_running = False
print(".") global i, working if not working: working = True depths[i] = depth filtered_depth = np.median(depths, axis=0) pointclouds.append(point_cloud(filtered_depth)) timestamps.append(timestamp) i += 1 i = i % n working = False if __name__ == "__main__": parser = OptionParser() parser.add_option("-o", "--output", dest="output", help="Output data in .npz format to OUTPUTFILE", metavar="OUTPUTFILE") (options, args) = parser.parse_args() context = freenect.init() device = freenect.open_device(context, 0) freenect.set_depth_mode(device, freenect.RESOLUTION_MEDIUM, freenect.DEPTH_MM) try: print("Capturing...") freenect.runloop(dev=device, depth=depth_callback) except KeyboardInterrupt: if options.output: print("Dumping...") np.savez_compressed(options.output, tangoPointclouds=pointclouds, tangoPointcloudTimestamps=timestamps)
#!/usr/bin/env python import freenect import rospy import cv2 import matplotlib.pyplot as plt import numpy as np mdev = freenect.open_device(freenect.init(), 0) freenect.set_depth_mode(mdev, freenect.RESOLUTION_HIGH, freenect.DEPTH_REGISTERED) # freenect.runloop(dev=mdev) def display_depth(dev, data, timestamp): d = np.array(data.copy()) # with open('depth_'+str(timestamp)+'.npy', 'w+') as f: # np.save(f, d, allow_pickle=True) d[d > 2000] = np.mean(d[d < 2000].flatten()) plt.imshow(d) plt.show() # if cv2.WaitKey(10) == 27: # keep_running = False # d = freenect.sync_get_depth()[0] freenect.runloop(dev=mdev, depth=display_depth) # plt.imshow(d, interpolation='nearest') # plt.show()
def set_depth_mode(self): if self.depth_mode: self.log.debug('Setting depth mode') fn.set_depth_mode(self.device, self.depth_mode.resolution, self.depth_mode.fmt)
def depth_mode(self, mode): self._depth_mode = mode freenect.set_depth_mode(self._dev, self._resolution, mode)
def background(): mdev = freenect.open_device(freenect.init(), 0) freenect.set_depth_mode(mdev, freenect.RESOLUTION_MEDIUM, freenect.DEPTH_MM) freenect.runloop(dev=mdev, depth=depth_func, video=video_func)
import freenect import numpy as np import time import cv2 int=freenect.init() mdev = freenect.open_device(int, 0) freenect.set_depth_mode(mdev, freenect.RESOLUTION_MEDIUM, freenect.DEPTH_REGISTERED) <<<<<<< HEAD #mdev2 = freenect.open_device(freenect.init(), 1) #freenect.set_depth_mode(mdev2, freenect.RESOLUTION_MEDIUM, freenect.DEPTH_REGISTERED) ======= mdev2 = freenect.open_device(int, 1) freenect.set_depth_mode(mdev2, freenect.RESOLUTION_MEDIUM, freenect.DEPTH_REGISTERED) >>>>>>> 851f7d8a2a40870019350bc52db5afdf02b61809 count=0 count2=0 def get_depth(dev,depth,time_stamp): print("display iage ") array=depth global count numpy.save(str(count),array) count+=1 time.sleep(60) #print(len(array),len(array[0]))