Search code examples
pythonopencvopencv3.0openni

OpenNI2 with OpenCV 3


I'm on OSX 10.11.5

I have been trying to build OpenCV3 with OpenNI support. I have a Python script that is supposed to work with the XBox 360 Kinect, but OpenCV isn't detecting the Kinect. I have libfreenect installed and I can run freenect-glview which brings up the depth view.

I've done pretty extensive Google searches, but all guides seem to be for Windows or Linux. Can anyone help me? Guides for OpenNI 1 will work too.

Thanks in advance!


Solution

  • I use Kinect for Xbox 360 on macOS successfully. The steps are as follows:

    1) Install OpenNI2

    $ brew tap homebrew/science
    
    $ brew install openni2
    

    2) Install libfreenect

    Note:
      libfreenect for Kinect Xbox 360
      libfreenect2 for Kinect Xbox One

    Because of this issue: Using Microsoft Kinect with Opencv 3.0.0, we should build it by ourselves.

    2.1) Download

    $ curl -O -L https://github.com/OpenKinect/libfreenect/archive/v0.5.5.tar.gz
    $ tar zxf v0.5.5.tar.gz
    $ cd libfreenect-0.5.5/
    

    2.2) Fix issue

    # jump to line 173
    $ vi OpenNI2-FreenectDriver/src/DepthStream.hpp
    
            case XN_STREAM_PROPERTY_ZERO_PLANE_DISTANCE:    // unsigned long long or unsigned int (for OpenNI2/OpenCV)
              if (*pDataSize != sizeof(unsigned long long) && *pDataSize != sizeof(unsigned int))
              {
                LogError("Unexpected size for XN_STREAM_PROPERTY_ZERO_PLANE_DISTANCE");
                return ONI_STATUS_ERROR;
              } else {
                if (*pDataSize != sizeof(unsigned long long)) {
                  *(static_cast<unsigned long long*>(data)) = ZERO_PLANE_DISTANCE_VAL;
                } else {
                  *(static_cast<unsigned int*>(data)) = (unsigned int) ZERO_PLANE_DISTANCE_VAL;
                }
              }
              return ONI_STATUS_OK;
    

    2.3) Build

    $ mkdir build
    $ cd build
    $ cmake .. -DBUILD_OPENNI2_DRIVER=ON
    $ make
    

    2.4) Install

    $ export OPENNI2_DIR=$(python -c 'import subprocess, glob; \
    prefix = subprocess.check_output("brew --prefix", shell=True); \
    print(glob.glob("%s/Cellar/openni2/*" % prefix[:-1])[0])')
    $ cd lib/OpenNI2-FreenectDriver/
    $ export FREENECT_DRIVER_LIB=libFreenectDriver.dylib
    $ export FREENECT_DRIVER_LIB=$(python - <<'EOF'
    import os
    lib = os.environ['FREENECT_DRIVER_LIB']
    while os.path.islink(lib):
        lib = os.readlink(lib)
    print(lib)
    EOF
    )
    $ ln -sf `pwd`/$FREENECT_DRIVER_LIB $OPENNI2_DIR/lib/ni2/OpenNI2/Drivers/libFreenectDriver.dylib
    $ ln -sf `pwd`/$FREENECT_DRIVER_LIB $OPENNI2_DIR/share/openni2/tools/OpenNI2/Drivers/
    

    2.5) Preview

    $ cd `brew --prefix`/share/openni2/tools
    $ ./NiViewer
    

    3) Build OpenCV

    3.1) Python

    $ brew install python
    $ python --version
    Python 2.7.13
    
    $ pip install numpy
    

    3.2) Source

    $ git clone https://github.com/opencv/opencv.git
    $ cd opencv/
    $ git checkout 3.2.0
    
    # if use extra modules
    $ git clone https://github.com/opencv/opencv_contrib.git
    $ cd opencv_contrib/
    $ git checkout 3.2.0
    

    3.3) Build

    $ cd opencv/
    $ mkdir build
    $ cd build/
    
    $ export PY_HOME=/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7
    $ export PY_NUMPY_DIR=$(python -c 'import os.path, numpy.core; print(os.path.dirname(numpy.core.__file__))')
    
    $ export OPENNI2_INCLUDE=/usr/local/include/ni2
    $ export OPENNI2_REDIST=/usr/local/lib/ni2
    
    $ cmake -DCMAKE_BUILD_TYPE=RELEASE \
    -DCMAKE_INSTALL_PREFIX=/usr/local \
    \
    -DBUILD_opencv_python2=ON \
    -DPYTHON2_EXECUTABLE=$PY_HOME/bin/python \
    -DPYTHON2_INCLUDE_DIR=$PY_HOME/Headers \
    -DPYTHON2_LIBRARY=$PY_HOME/lib \
    -DPYTHON2_PACKAGES_PATH=/usr/local/lib/python2.7/site-packages \
    -DPYTHON2_NUMPY_INCLUDE_DIRS=$PY_NUMPY_DIR/include/ \
    \
    -DWITH_OPENNI2=ON \
    \
    -DBUILD_DOCS=OFF \
    -DBUILD_EXAMPLES=OFF \
    -DBUILD_TESTS=OFF \
    -DBUILD_PERF_TESTS=OFF \
    \
    -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
    ..
    
    $ make -j$(python -c 'import multiprocessing as mp; print(mp.cpu_count())')
    $ make install
    

    4) Finally

    I posted the details to this gist. Samples here: C++, Python.

    NOTE: Unable to retrieve correct image using Python code now. Please see this issue.