Skip to content

Wrapper: Python

Andrews Sobral edited this page Jul 13, 2017 · 34 revisions
  • Dependencies:

    • Boost library
    • Python library

Building BGSLibrary with Python support on Windows

  • Tested on:

    • Windows 10 Pro
    • Python 2.7.13 (Anaconda Python)
    • Visual Studio 2013 and 2015
    • OpenCV 2.4.10 and 3.2.0
    • Boost 1.64.0

Compiling Boost with Python support on Windows

:: Setup
.\bootstrap.bat

:: Edit project-config.jam by adding the configuration below at the end of the file.
using python
    : 2.7                            # Version
    : C:\\Anaconda2\\python.exe      # Python Path
    : C:\\Anaconda2\\include         # include path
    : C:\\Anaconda2\\libs            # lib path(s)
    : <define>BOOST_ALL_NO_LIB=1
    ;

:: For Visual Studio 2013 users
.\b2 -j1 toolset=msvc-12.0 variant=release threading=multi link=static,shared runtime-link=static,shared --build-type=complete address-model=64 --stagedir=lib64-pyconda-msvc-12.0 --with-python stage

:: For Visual Studio 2015 users
.\b2 -j1 toolset=msvc-14.0 variant=release threading=multi link=static,shared runtime-link=static,shared --build-type=complete address-model=64 --stagedir=lib64-pyconda-msvc-14.0 --with-python stage
  • At the end of compilation, all *.lib and *.dll files are available at:
:: For Visual Studio 2013
C:\boost_1_64_0\lib64-pyconda-msvc-12.0\lib

:: For Visual Studio 2015
C:\boost_1_64_0\lib64-pyconda-msvc-14.0\lib

Compiling BGSLibrary with Python support on Windows

  • Step-by-step Instructions
git clone https://github.com/andrewssobral/bgslibrary.git
cd bgslibrary\build

::---------------------------------------
:: For OpenCV 2.4.10 + Visual Studio 2013
::---------------------------------------
set OpenCV_DIR=C:\OpenCV2.4.10\build
set PATH=%PATH%;%OpenCV_DIR%\x64\vc12\bin
set BOOST_ROOT=C:\boost_1_64_0
set BOOST_LIBRARYDIR=%BOOST_ROOT%\lib64-pyconda-msvc-12.0\lib

cmake -D OpenCV_DIR=%OpenCV_DIR% -D BOOST_INCLUDEDIR=%BOOST_ROOT% -D BOOST_LIBRARYDIR=%BOOST_LIBRARYDIR% -D BGS_PYTHON_SUPPORT=ON -G "Visual Studio 12 Win64" ..

::----------------------------------
:: For OpenCV 3 + Visual Studio 2015
::----------------------------------
set OpenCV_DIR=C:\OpenCV3.2.0\build
set PATH=%PATH%;%OpenCV_DIR%\x64\vc14\bin
set BOOST_ROOT=C:\boost_1_64_0
set BOOST_LIBRARYDIR=%BOOST_ROOT%\lib64-pyconda-msvc-14.0\lib

cmake -D OpenCV_DIR=%OpenCV_DIR% -D BOOST_INCLUDEDIR=%BOOST_ROOT% -D BOOST_LIBRARYDIR=%BOOST_LIBRARYDIR% -D BGS_PYTHON_SUPPORT=ON -G "Visual Studio 14 Win64" ..
  • You will see something similar to:
-- The C compiler identification is MSVC 19.0.24215.1
-- The CXX compiler identification is MSVC 19.0.24215.1
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- BGSLIBRARY WITH PYTHON SUPPORT: ON
-- OpenCV ARCH: x64
-- OpenCV RUNTIME: vc14
-- OpenCV STATIC: OFF
-- Found OpenCV: C:/OpenCV3.2.0/build (found version "3.2.0")
-- Found OpenCV 3.2.0 in C:/OpenCV3.2.0/build/x64/vc14/lib
-- You might need to add C:\OpenCV3.2.0\build\x64\vc14\bin to your PATH to be able to run your applications.
-- OpenCV library status:
--     version: 3.2.0
--     libraries: opencv_calib3d;opencv_core;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab;opencv_world
--     include path: C:/OpenCV3.2.0/build/include;C:/OpenCV3.2.0/build/include/opencv
-- Boost version: 1.64.0
-- Found the following Boost libraries:
--   python
-- Found PythonInterp: C:/Anaconda2/python.exe (found version "2.7.13")
-- Found PythonLibs: C:/Anaconda2/libs/python27.lib (found version "2.7.13")
-- Boost library status:
--     version: 106400
--     libraries: C:/boost_1_64_0/lib64-pyconda-msvc-14.0/lib/libboost_python-vc140-mt-1_64.lib
--     include path: C:/boost_1_64_0
-- Python library status:
--     executable: C:/Anaconda2/python.exe
--     version: 2.7.13
--     library: C:/Anaconda2/libs/python27.lib
--     include path: C:/Anaconda2/include
-- NUMPY_INCLUDE_DIR: C:\Anaconda2\lib\site-packages\numpy\core\include
-- Configuring done
-- Generating done
-- Build files have been written to: C:/bgslibrary/build
  • Next, open bgslibrary.sln in your Visual Studio 2015 (or 2013), check [RELEASE] mode and click on [Build].

  • At the end, you will see libbgs.pyd file in your C:\bgslibrary\build path.

  • Copy C:\bgslibrary\build\libbgs.pyd to C:\bgslibrary.

  • Now, copy all *.dll files from C:\boost_1_64_0\lib64-pyconda-msvc-14.0\lib (or from C:\boost_1_64_0\lib64-pyconda-msvc-12.0\lib for 2013 users) to C:\bgslibrary.

  • Next, go to windows console and run:

python Demo.py
  • Complete!

Building BGSLibrary with Python support on Linux

  • Tested on:

    • Ubuntu 14.04 and 16.04
    • Python 2.7.6 and Python 3.6.1 (Anaconda Python)
    • GCC 4.8.4 and 5.4.0
    • OpenCV 2.x and OpenCV 3.2
  • Step-by-step Instructions

git clone https://github.com/andrewssobral/bgslibrary.git

cd build
cmake -DBGS_PYTHON_SUPPORT=ON ..
make

(add 'libbgs.so' to your Python path)

cd ..
python Demo.py

Building with Anaconda Python

conda install boost

git clone https://github.com/andrewssobral/bgslibrary.git
cd bgslibrary/build

cmake -D BGS_PYTHON_SUPPORT=ON ..

:: Or (if you need to specify some paths)
cmake -D BGS_PYTHON_SUPPORT=ON \ 
  -D PYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
  -D PYTHON_LIBRARY=$(python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))") \
  -D BOOST_INCLUDEDIR=$HOME/anaconda/include \
  -D BOOST_LIBRARYDIR=$HOME/anaconda/lib ..

make -j8

:: Add 'libbgs.so' to your Python path
export PYTHONPATH="${PYTHONPATH}:/root/bgslibrary/build"

cd ..
python Demo.py

Known Issues

  • error: variable type 'NumpyAllocator' is an abstract class or error: no member named 'refcount' in 'cv::Mat'

    • Unsolved issue (Not compatible with OpenCV 3)
  • fatal error: 'numpy/ndarrayobject.h' file not found

    • Solution 1:
Default Python:
export CFLAGS="-I /usr/local/lib/python2.7/site-packages/numpy/core/include $CFLAGS"

Anaconda Python:
export CFLAGS="-I $HOME/anaconda/lib/python2.7/site-packages/numpy/core/include $CFLAGS"
    • Solution 2:
Default Python:
cd /usr/local/include
ln -s /usr/local/lib/python2.7/site-packages/numpy/core/include/numpy numpy

Anaconda Python:
cd $HOME/anaconda/include
ln -s $HOME/anaconda/lib/python2.7/site-packages/numpy/core/include/numpy numpy