Skip to content

Instantly share code, notes, and snippets.

@r9y9
Created October 6, 2016 14:17
Show Gist options
  • Save r9y9/c64d4500283f6c33aa150d1e89806af8 to your computer and use it in GitHub Desktop.
Save r9y9/c64d4500283f6c33aa150d1e89806af8 to your computer and use it in GitHub Desktop.
from pylibfreenect2 import Freenect2, SyncMultiFrameListener
from pylibfreenect2 import FrameType, Registration, Frame
import sys
import numpy as np
import cv2
try:
from pylibfreenect2 import OpenGLPacketPipeline
pipeline = OpenGLPacketPipeline()
except:
from pylibfreenect2 import CpuPacketPipeline
pipeline = CpuPacketPipeline()
class Processor(object):
def __init__(self):
# self.pipeline = pipeline
self.fn = Freenect2()
num_devices = self.fn.enumerateDevices()
if num_devices == 0:
print("No device connected!")
sys.exit(1)
self.serial = self.fn.getDeviceSerialNumber(0)
self.device = self.fn.openDevice(self.serial, pipeline=pipeline)
self.listener = SyncMultiFrameListener(FrameType.Color | FrameType.Ir | FrameType.Depth)
# Register listeners
self.device.setColorFrameListener(self.listener)
self.device.setIrAndDepthFrameListener(self.listener)
self.device.start()
self.registration = Registration(self.device.getIrCameraParams(),
self.device.getColorCameraParams())
self.undistorted = Frame(512, 424, 4)
self.registered = Frame(512, 424, 4)
# Optinal parameters for registration
# set True if you need
need_bigdepth = False
need_color_depth_map = False
self.bigdepth = Frame(1920, 1082, 4) if need_bigdepth else None
self.color_depth_map = np.zeros((424, 512), np.int32).ravel() \
if need_color_depth_map else None
def run(self):
while True:
if self.listener.hasNewFrame():
frames = self.listener.waitForNewFrame()
print(frames)
color = frames["color"]
ir = frames["ir"]
depth = frames["depth"]
self.registration.apply(color, depth, self.undistorted, self.registered,
bigdepth=self.bigdepth,
color_depth_map=self.color_depth_map)
cv2.imshow("color", cv2.resize(color.asarray(),
(int(1920 // 3), int(1080 // 3))))
self.listener.release(frames)
key = cv2.waitKey(delay=1)
if key == ord('q'):
break
self.device.stop()
self.device.close()
p = Processor()
p.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment