vgel.me

- projects & stuff

permalink for Setting up PyOpenNI in Linux

Setting up PyOpenNI in Linux

I've had a Kinect for a while. I wrote a few interesting scripts with it, then forgot about it for a while. In between that time and now, I went through several OS upgrades that wiped away my changes. I decided I want to mess with it again, and had to reinstall OpenNI and the python extensions. This is a somewhat tricky process, so here's how it's done.

Step 1: Installing OpenNI, NITE, and the drivers

These are the drivers and middleware that will do all the heavy lifting for you. I grabbed them from the simple-openni project, which is a Kinect library for Processing. I don't care about processing, but they also conveniently bundle the libraries for you. Just grab the zipfile, and execute sudo ./install.sh in the OpenNI-Bin-Dev-Linux-[platform]-[version] folder, the NITE-Bin-Dev-Linux-[platform]-[version] folder, and the kinect/Sensor-Bin-Linux-[platform]-[version] folder.

Step 2: Add the library exports to your shell

The installation process generates a file with the location of the library files. You need to source this file for the installing of PyOpenNI. Simply run

source [folder you extracted openni in]/OpenNIDevEnvironment

However if it's not there, simply run

sudo updatedb && locate OpenNIDevEnvironment

Which should find the current location for your to source, though updatedb will take a few minutes to run generally.

Step 3: Build PyOpenNI

These instructions are on the Github, however I'll copy them here.

git clone https://github.com/jmendeth/PyOpenNI.git
mkdir PyOpenNI-build
cd PyOpenNI-build
cmake ../PyOpenNI
make
<copy to your python modules folder>

Step 4: Blacklist gspca_kinect

If you're using a newer Linux kernel (I'm using one of the 3.10 series), there is an existing module that only allows you to use the kinect as a generic webcam. This conflicts with OpenNI, so you need to blacklist it.

You can temporarily blacklist the module by running

sudo rmmod gspca_kinect

And permanently blacklist it by adding blacklist gspca_kinect to your blacklist file (google how to do it for your distribution).

Step 5: Not really a step

You can use OpenNI through Python now. But here's a bit of extra support code for controlling the motor and LED, which PyOpenNI doesn't do (requires pyusb):

import usb

LED_OFF              = 0
LED_GREEN            = 1
LED_RED              = 2
LED_YELLOW           = 3
LED_BLINK_YELLOW     = 4
LED_BLINK_GREEN      = 5
LED_BLINK_RED_YELLOW = 6

_dev = usb.core.find(idVendor=0x045e, idProduct=0x02B0) #The kinect motor device

def set_led_color(color):
    _dev.ctrl_transfer(0x40, 0x06, color, 0x0, [])

def set_motor_angle(degree):
    _dev.ctrl_transfer(0x40, 0x31, degree, 0x0, [])

def set_motor_up():
    set_motor_angle(60)

def set_motor_level():
    set_motor_angle(0)

def set_motor_down():
    set_motor_angle(-60)

Thanks for reading, hope this helps you!