Skip to content

Instantly share code, notes, and snippets.

@joinAero
Created May 19, 2017 07:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joinAero/2bf53de4031ffcf8ed72208f486ed659 to your computer and use it in GitHub Desktop.
Save joinAero/2bf53de4031ffcf8ed72208f486ed659 to your computer and use it in GitHub Desktop.
Install OpenCV 3 with Python 2.7 (macOS) + OpenNI2 for Kinect

Install OpenCV 3 with Python 2.7 (macOS)

$ brew install python
$ python --version
Python 2.7.13

$ pip install numpy
$ 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
$ 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__))')

$ 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/ \
\
-DBUILD_DOCS=OFF \
-DBUILD_EXAMPLES=OFF \
-DBUILD_TESTS=OFF \
-DBUILD_PERF_TESTS=OFF \
..

$ make -j$(python -c 'import multiprocessing as mp; print(mp.cpu_count())')
$ make install

Other options:

-DCMAKE_C_COMPILER="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc" \
-DCMAKE_CXX_COMPILER="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++" \

-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \

cmake with OpenNI2


$ brew tap homebrew/science

$ brew install openni2
$ brew install libfreenect

$ brew info openni2
# plz see issue firstly
$ brew info libfreenect

$ export OPENNI2_DIR=/usr/local/Cellar/openni2/2.2.0-debian
$ export LIBFREENECT_DIR=/usr/local/Cellar/libfreenect/0.5.5
# Or,
$ 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])')
$ export LIBFREENECT_DIR=$(python -c 'import subprocess, glob; \
prefix = subprocess.check_output("brew --prefix", shell=True); \
print(glob.glob("%s/Cellar/libfreenect/*" % prefix[:-1])[0])')

$ cd $LIBFREENECT_DIR/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/libFreenectDriver.dylib

$ cd `brew --prefix`/share/openni2/tools
$ ./NiViewer
$ export OPENNI2_INCLUDE=/usr/local/include/ni2
$ export OPENNI2_REDIST=/usr/local/lib/ni2

$ cmake ... \
-DWITH_OPENNI2=ON \
...

Issues

OpenNI2-FreenectDriver: (ERROR) Unexpected size for XN_STREAM_PROPERTY_ZERO_PLANE_DISTANCE

OpenNI2-FreenectDriver: (ERROR) Unexpected size for XN_STREAM_PROPERTY_ZERO_PLANE_DISTANCE
OpenCV Error: Unspecified error (CvCapture_OpenNI2::readCamerasParams : Could not read virtual plane distance!

Solution:

$ brew uninstall libfreenect

# Note:
#   libfreenect  for Kinect Xbox 360
#   libfreenect2 for Kinect Xbox One
$ 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/

# fix issue in 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;

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

# 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/libFreenectDriver.dylib
@joinAero
Copy link
Author

How to use Kinect with OpenCV: C++, Python.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment