Skip to content

Wrapper: Python

Andrews Cordolino Sobral edited this page May 1, 2024 · 34 revisions

Installation using PyPI repository

Downloads Downloads Downloads

pip install pybgs

or

pip3 install pybgs

Building Python wrapper from scratch

  • Requirements:

    • Python library (2.7.x or 3.x)
    • OpenCV (2.4.x, 3.4.x or 4.x)
To build the bgslibrary:
    python setup.py build
To build and install:
    python setup.py install
To install using pip:
    pip install .
To install using PyPI:
    pip install pybgs
To package the wheel (after pip installing twine and wheel):
    python setup.py bdist_wheel
To upload the binary wheel to PyPi
    twine upload dist/*.whl
To upload the source distribution to PyPi
    python setup.py sdist
    twine upload dist/pybgs-*.tar.gz

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

For Python 2.x you should add BGS_PYTHON_VERSION=2 (BGS_PYTHON_VERSION=3 by default) in your cmake command, for example:

cmake -D BGS_PYTHON_SUPPORT=ON -D BGS_PYTHON_VERSION=2 ..

Compiling BGSLibrary with Python 2.x support on Windows

  • Step-by-step Instructions
git clone --recursive 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

cmake -D OpenCV_DIR=%OpenCV_DIR% -D BGS_PYTHON_SUPPORT=ON -D BGS_PYTHON_VERSION=2 -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

cmake -D OpenCV_DIR=%OpenCV_DIR% -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
-- Found PythonInterp: C:/Anaconda2/python.exe (found version "2.7.13")
-- Found PythonLibs: C:/Anaconda2/libs/python27.lib (found version "2.7.13")
-- 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.

  • Next, go to the previous 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.4.x and OpenCV 3.2
  • Step-by-step Instructions

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

cd build

:: For Python 2.x:
cmake -D BGS_PYTHON_SUPPORT=ON -D BGS_PYTHON_VERSION=2 ..

:: For Python 3.x:
cmake -D BGS_PYTHON_SUPPORT=ON ..

make

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

cd ..
python Demo.py

Building with Anaconda Python

conda install boost

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

:: For Python 2.x:
cmake -D BGS_PYTHON_SUPPORT=ON -D BGS_PYTHON_VERSION=2 ..

:: For Python 3.x:
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'))") ..

make -j8

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

cd ..
python Demo.py

Known Issues

  • 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

Building BGSLibrary with Python support on Mac OS X

  • Similar to build process on Linux.

  • Just add the generated lib (e.g. pybgs.so for Python 2.7 or pybgs.cpython-36m-darwin.so for Python 3.6) to your Python path.

  • Tested on:

    • macOS Sierra (10.12.6)
    • Python 2.7.13 and Python 3.6.1 (Anaconda Python)
    • OpenCV 3.3.0

For Python 2.x:

cmake -D BGS_PYTHON_SUPPORT=ON -D BGS_PYTHON_VERSION=2 ..

For Python 3.x:

cmake -D BGS_PYTHON_SUPPORT=ON ..

Support for Xcode: just append -G "Xcode" at the end.

Example:

$ cmake -D BGS_PYTHON_SUPPORT=ON ..
-- The C compiler identification is AppleClang 8.1.0.8020042
-- The CXX compiler identification is AppleClang 8.1.0.8020042
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- 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
-- Found OpenCV: /usr/local (found version "3.3.0") 
-- OpenCV library status:
--     version: 3.3.0
--     libraries: opencv_calib3d;opencv_core;opencv_dnn;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_aruco;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_datasets;opencv_dnn_modern;opencv_dpm;opencv_face;opencv_fuzzy;opencv_hdf;opencv_img_hash;opencv_line_descriptor;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_reg;opencv_rgbd;opencv_saliency;opencv_stereo;opencv_structured_light;opencv_surface_matching;opencv_text;opencv_tracking;opencv_xfeatures2d;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto
--     include path: /usr/local/include;/usr/local/include/opencv
-- Found PythonInterp: /Users/andrewssobral/anaconda/bin/python (found version "2.7.13") 
-- Found PythonLibs: /Users/andrewssobral/anaconda/lib/libpython2.7.dylib (found version "2.7.13") 
-- Python library status:
--     executable: /Users/andrewssobral/anaconda/bin/python
--     version: 2.7.13
--     library: /Users/andrewssobral/anaconda/lib/libpython2.7.dylib
--     include path: /Users/andrewssobral/anaconda/include/python2.7
-- NUMPY_INCLUDE_DIR: /Users/andrewssobral/anaconda/lib/python2.7/site-packages/numpy/core/include
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/andrewssobral/bgslibrary/build