Created
October 9, 2012 14:56
-
-
Save hansent/3859347 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy | |
from kivy.app import App | |
from kivy.lang import Builder | |
from kivy.clock import Clock | |
from kivy.uix.floatlayout import FloatLayout | |
from kivy.graphics.texture import Texture | |
from kivy.graphics import Rectangle, Color | |
from kivy.properties import ObjectProperty | |
from oni_kinect import Kinect | |
def cv_image(buf, w=640, h=480, dtype=numpy.uint8): | |
img = numpy.frombuffer(buf, dtypr) | |
img.shape = (h,w) | |
return img | |
class KinectViewer(FloatLayout): | |
depth_data = ObjectProperty(None, allownone=True) | |
depth_tex = ObjectProperty(None, allownone=True) | |
image_data = ObjectProperty(None, allownone=True) | |
image_tex = ObjectProperty(None, allownone=True) | |
def __init__(self, **kwargs): | |
super(KinectViewer, self).__init__(**kwargs) | |
self.depth_fmt = {'colorfmt': 'luminance', 'bufferfmt': 'ubyte'} | |
self.image_fmt = {'colorfmt': 'rgb', 'bufferfmt': 'ubyte'} | |
depth_tex = Texture.create(size=(640, 480), **self.depth_fmt) | |
depth_tex.flip_vertical() | |
image_tex = Texture.create(size=(640, 480), **self.image_fmt) | |
image_tex.flip_vertical() | |
self.depth_tex = depth_tex | |
self.image_tex = image_tex | |
def on_depth_data(self, *args): | |
if self.depth_data: | |
self.depth_tex.blit_buffer(self.depth_data, **self.depth_fmt) | |
self.canvas.ask_update() | |
def on_image_data(self, *args): | |
if self.image_data: | |
self.image_tex.blit_buffer(self.image_data, **self.image_fmt) | |
self.canvas.ask_update() | |
Builder.load_string(""" | |
<KinectViewer>: | |
canvas: | |
Color: | |
rgb: .3,.3,.3 | |
Rectangle: | |
pos: self.pos | |
size: self.size | |
GridLayout: | |
cols:2 | |
Image: | |
texture: root.depth_tex | |
Image: | |
texture: root.image_tex | |
""") | |
class KinectApp(App): | |
def on_start(self, *args): | |
self.kinect = Kinect() | |
self.kinect.start() | |
Clock.schedule_interval(self.update_kinect, 0) | |
def on_stop(self, *args): | |
self.kinect.stop() | |
def update_kinect(self, *args): | |
self.kinect.update() | |
self.kinect_viewer.depth_data = self.kinect.depth_data | |
self.kinect_viewer.image_data = self.kinect.image_data | |
def build(self): | |
self.kinect_viewer = KinectViewer() | |
return self.kinect_viewer | |
if __name__ == '__main__': | |
KinectApp().run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import openni | |
class Kinect(object): | |
def __init__(self): | |
self.context = None | |
self.recorder = None | |
self.depth_data = None | |
self.image_data = None | |
def start(self): | |
self.context = openni.Context() | |
self.context.init() | |
self.depth_gen = openni.DepthGenerator() | |
self.depth_gen.create(self.context) | |
self.depth_gen.set_resolution_preset(openni.RES_VGA) | |
self.depth_gen.fps = 30 | |
self.image_gen = openni.ImageGenerator() | |
self.image_gen.create(self.context) | |
self.image_gen.set_resolution_preset(openni.RES_VGA) | |
self.image_gen.fps = 30 | |
self.context.start_generating_all() | |
def stop(self): | |
self.conext.shutdown() | |
def update(self, wait=False): | |
if wait: | |
self.context.wait_any_update_all() | |
else: | |
self.context.wait_none_update_all() | |
self.depth_data = self.depth_gen.get_raw_depth_map_8() | |
self.image_data = self.image_gen.get_raw_image_map() | |
def playback(self, filename): | |
if self.context: | |
self.stop() | |
ctx = openni.Context() | |
ctx.init() | |
ctx.open_file_recording(filename) | |
ctx.start_generating_all() | |
self.context = ctx | |
self.depth_gen = ctx.find_existing_node(openni.NODE_TYPE_DEPTH) | |
self.image_gen = ctx.find_existing_node(openni.NODE_TYPE_IMAGE) | |
def start_recording(self, filename): | |
self.recorder = openni.Recorder() | |
self.recorder.create(self.context) | |
self.recorder.destination = filename | |
self.recorder.add_node_to_rec(self.depth_gen, CODEC_16Z_EMB_TABLES) | |
self.recorder.add_node_to_rec(self.image_gen, CODEC_JPEG) | |
def stop_recording(self): | |
if self.recorder: | |
self.recorder.rem_node_from_rec(self.depth_gen) | |
self.recorder.rem_node_from_rec(self.image_gen) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment