BackgroundSubtractorCNT with python

the impossible code - python background subtraction with BackgroundSubtractorCNT

OpenCV is very popular among python developers and we deserve being able to use BackgroundSubtractorCNT with python just like any other OpenCV python feature. This post explains how to enable, build and use BackgroundSubtractorCNT with python.

UPDATE: This project now has it’s own site.

BackgroundSubtractorCNT with python

(If you missed the previous post about BackgroundSubtractorCNT and how fast it is then see here.)

As I would normally use OpenCV from python,  I wanted to be able to create a BackgroundSubtractorCNT python object in my python applications. I also wanted use it interchangeably with any other background subtraction implementation. So I started working on a python branch for the project. After looking into several options and looking at what OpenCV is doing I started focusing on swig  for wrapping the C++ code. On one hand BackgroundSubtractorCNT is not a part of OpenCV and cannot be part of it’s build system and code generation solutions. On the other hand swig can supply wrappers for use in many other programming languages, so it’s worth the time of learning to use it.

It turns out that swig is very friendly once you get to know it. After I started playing with it the decision became final.

The only main challenges left were these:

  • Having to convert numpy arrays from/to cv::Mat.
    • Challenge: I need to do it the same way OpenCV does it for interoperability.
  • Incorporate the “python module building” into a cmake build.
    • Challenge: I need it to work both on Linux and Windows – build & install/package.

Solving these challenges sent me seeking inspiration on the internet. I’m not going to delve into tedious details of how I solved it. Instead I’ll share the links to how other people solved similar problems at the end of this post.

Building and install BackgroundSubtractorCNT with python

It is best keeping such details on the GitHub project page.

There are also 2 video tutorials for building  BackgroundSubtractor with python for Windows & Pi on the video channel of the impossible code.

 

Using BackgroundSubtractorCNT with python

After installation, these are the 2 only things you would do differently:

  1. New import: import bgsubcnt
  2. Create your background subtractor with: bgsubcnt.createBackgroundSubtractor()

Here is an example usage:

Final notes

As promised here are links for further reading about facing python module building with OpenCV and cmake. I did things somewhat differently but based it on ideas from these:

  • Bloerg: Cmake and distutils – using setup.py from within cmake. I’m using that idea but changed some things for windows path compatibility and altered the install option for packaging support. If this is something which interest you, see the solution I came up with here.
  • pyboostcvconverter – manually enable conversion between cv::Mat and numpy arrays for “Boost.Python”. I’m using the same idea of “utilize by copy-paste” the original OpenCV  conversion code, but I made the conversion transparent for my swig usage (like OpenCV does it). If this is something which interest you, see the solution I came up with here.

This has been a fun project and a good learning experience. I hope you’ll also find it useful.

See you next time.

Sagi Zeevi

A software developer brain surgeon ... if software only had a brain. An electronics hobbyist heart surgeon ... if electronics only had a heart.

You may also like...

16 Responses

  1. markusand says:

    Hi! I’m trying to install BackgroundSubstractorCNT for python in RPi3 to give it a try, but I’m not able to install. I’ve followed the instructions in github and youtube channel and I am stuck in the cmake step, I guess because I have my OpenCV install in a virtual environment and some path may not match. This is my error log:
    OpenCV_LIB_DIR_NATIVE is:
    OpenCV_INCLUDE_DIRS_NATIVE is: ‘/usr/local/include’,’/usr/local/include/opencv’
    CMAKE_CURRENT_SOURCE_DIR_NATIVE is: /home/pi/BackgroundSubtractorCNT/python
    BGSUBCNT_INCLUDE_DIR_NATIVE is: /home/pi/BackgroundSubtractorCNT
    Traceback (most recent call last):
    File “”, line 1, in
    AttributeError: ‘module’ object has no attribute ‘getsitepackages’
    PYTHON_PREFIX is /home/pi/.virtualenvs/cv
    PYTHON_SITE_DIR is
    CMake Error at python/CMakeLists.txt:115 (INSTALL):
    install DIRECTORY given no DESTINATION!

    Could you please tell me what parameter do I have to change. Thanks.

  2. Charlie says:

    Hi~I’m trying to install this for python3.5.2 in ubuntu-16.04.3. I’ve followed youtube.
    But not install bgsubcnt in python3.5.2.

    ImportError : No module named ‘bgsubcnt’

    • Sagi Zeevi says:

      Hi,
      It is using the default ‘python’ in your path:
      1. I just opened and fixed 2 related issues thanks to your comment
      – fixed a build error for python3
      – fixed missing documentation about using a different python version (see also below).
      2. Please take again the source (the latest sources from the master branch) there is no specific release for these fixes yet.
      3. You need to use this from the build directory:
      cmake -DPYTHON_EXTENSION=ON -DPYTHON=/usr/bin/python3.5 ..

      Regards,
      Sagi.

      • Charlie says:

        Thank you!! success in ubuntu!!
        And i’ll try to install in window. But error occurred in building the sources.

        c:\Users\jdo\BackgroundSubtractorCNT\build>msbuild ALL_BUILD.vcxproj /p:Configuration=Release
        ..1 Warning(s)
        2 Error(s)

        The contents of the error are as follows

        “c:\Users\jdo\BackgroundSubtractorCNT\build\ALL_BUILD.vcxproj”(기본 대상)(1)->
        “C:\Users\jdo\BackgroundSubtractorCNT\build\python\bgsubcnt_python.vcxproj”(기본 대상)(4)->
        (CustomBuild 대상) ->
        LINK : fatal error LNK1181: cannot open input file ‘opencv_world330.lib’ [C:\Users\jdo\BackgroundSubtractorCNT\build\
        python\bgsubcnt_python.vcxproj]
        CUSTOMBUILD : error : command ‘C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin\\x86_amd64\\link.exe’ f
        ailed with exit status 1181 [C:\Users\jdo\BackgroundSubtractorCNT\build\python\bgsubcnt_python.vcxproj]

        • Charlie says:

          And i want to use it in python

          • Sagi Zeevi says:

            – Fixed another 2 issues for this.
            – It works now, as you can see:
            c:\Users\sagiz\BackgroundSubtractorCNT>set PYTHONPATH=C:\Program Files\bgsubcnt 1.1.3\lib\site-packages

            c:\Users\sagiz\BackgroundSubtractorCNT>c:\Python35\python.exe -c “import cv2, bgsubcnt; fgbg = bgsubcnt.createBackgroundSubtractor(); print(fgbg)”

          • Charlie says:

            umm…I did not understand, but I did what you asked me to do.
            But error occurred.

            c:\Users\jdo\BackgroundSubtractorCNT>set PYTHONPATH=C:\Program Files\bgsubcnt 1.1.3\lib\site-packages

            c:\Users\jdo\BackgroundSubtractorCNT>C:\Users\jdo\AppData\Local\Programs\Python\Python35\python.exe -c “import cv2, bgsubcnt; fgbg = bgsubcnt.createBackgroundSubtractor(); print(fgbg)”
            Traceback (most recent call last):
            File “”, line 1, in
            ImportError: No module named ‘bgsubcnt’

            I don’t know what it means…

          • Sagi Zeevi says:

            As I said, I fixed 2 issues for this.
            Please take the sources again and rebuild them before using the command line above.

          • Sagi Zeevi says:

            FYI, currently the code in opencv-contrib-3.3.0 is the same as in my github repository.

            So another option for you, if you don’t mind using opencv-contrib, is to install opencv-contrib-3.3.0 for windows (you need to build opencv yourself or use pip anyway for python3).

            Then this should work without the ‘bgsubcnt’:
            c:\python35\python -c “import cv2; fgbg = cv2.bgsegm.createBackgroundSubtractorCNT(); print(fgbg)”

            Note that I’m not directly responsible for the code inserted into opencv_contrib, nor for the pip package of it.

          • Charlie says:

            Thank you very much. Resolved !!
            But there was one problem.
            In python
            ImportError: No module named ‘bgsubcnt’
            Has occurred.

            Cmake -DPYTHON_EXTENSION = ON -DPYTHON = / usr / bin / python3.5 ..
            Make

            Will this work?

          • Sagi Zeevi says:

            You’re most welcome 🙂
            I don’t understand if there is currently still a problem, or all is well on both Ubuntu and Windows?

          • Charlie says:

            In ubuntu, it worked as well as it did. However, bgsubcnt 1.1.3 version is also installed on windows, and Importerror occurred in python IDLE.

            The error occurred in the way that ubuntu fixes it (cmake -DPYTHON_EXTENSION = ON -DPYTHON = / usr / bin / python3.5 ..)

          • Sagi Zeevi says:

            Please inspect the steps below carefully and modify to match your python installation path. This worked for me yesterday on Window10:
            cd c:\users\sagiz
            git clone https://github.com/sagi-z/BackgroundSubtractorCNT.git –single-branch
            cd BackgroundSubtractorCNT
            mkdir build
            cd build
            cmake -G “Visual Studio 14 2015 Win64” -DBUILD_TEST=ON -DPYTHON_EXTENSION=ON -DPYTHON=c:\python35\python.exe ..
            msbuild ALL_BUILD.vcxproj /p:Configuration=Release
            msbuild PACKAGE.vcxproj
            .\bgsubcnt-1.1.3-win64.exe /p:Configuration=Release
            set PYTHONPATH=C:\Program Files\bgsubcnt 1.1.3\lib\site-packages
            c:\Python35\python.exe -c “import cv2, bgsubcnt; fgbg = bgsubcnt.createBackgroundSubtractor(); print(fgbg)”

Leave a Reply

Scroll Up