Skip to content

Instantly share code, notes, and snippets.

@0x54
Created October 11, 2016 17:57
Show Gist options
  • Save 0x54/9664f12d61918d027136c403e018d1c3 to your computer and use it in GitHub Desktop.
Save 0x54/9664f12d61918d027136c403e018d1c3 to your computer and use it in GitHub Desktop.
pylibfreenect2
from pylibfreenect2 import Freenect2, SyncMultiFrameListener
from pylibfreenect2 import FrameType, Registration, Frame
import sys
import numpy as np
import cv2
from multiprocessing import Process
try:
from pylibfreenect2 import OpenGLPacketPipeline
pipeline = OpenGLPacketPipeline()
except:
from pylibfreenect2 import CpuPacketPipeline
pipeline = CpuPacketPipeline()
class Processor(Process):
def __init__(self):
super(Processor, self).__init__()
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:
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.start()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment