public
Created

  • Download Gist
main.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
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()
oni_kinect.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.