Skip to content

Commit

Permalink
Added support for pybind11 (removed Boost.Python -- no need to instal…
Browse files Browse the repository at this point in the history
…l Boost C++ Libraries or Boost.Python anymore) (#137)

* Added support for pybind11 and removed Boost.Python

* Update README

* Added support for pip install, fixed small issues for macosx

* renamed libbgs to bgs, updated cmakefile
  • Loading branch information
andrewssobral authored Aug 26, 2018
1 parent efd3690 commit f1e770e
Show file tree
Hide file tree
Showing 19 changed files with 1,001 additions and 1,287 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ fet/etc/
*.pdb
*.suo
*.dll
*.pyd
*.so
bgs.egg-info
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "pybind11"]
path = pybind11
url = https://github.com/pybind/pybind11.git
162 changes: 102 additions & 60 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,25 @@ elseif()
endif()
message(STATUS "BGSLIBRARY WITH PYTHON SUPPORT: ${BGS_PYTHON_SUPPORT}")

# cmake -D BGS_PYTHON_SUPPORT=ON -D BGS_PYTHON_VERSION=2 ..
# cmake -D BGS_PYTHON_SUPPORT=ON -D BGS_PYTHON_VERSION=3 ..
if(NOT DEFINED BGS_PYTHON_VERSION)
set(BGS_PYTHON_VERSION 2)
set(BGS_PYTHON_VERSION 3)
endif()
if(BGS_PYTHON_SUPPORT)
message(STATUS "PYTHON VERSION: ${BGS_PYTHON_VERSION}")
endif()
message(STATUS "PYTHON VERSION: ${BGS_PYTHON_VERSION}")

if(UNIX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x")
set(CMAKE_MACOSX_RPATH 1)
endif(UNIX)

# Avoid cmake warnings about changes in behavior of some Mac OS X path
# variable we don't care about.
if (POLICY CMP0042)
cmake_policy(SET CMP0042 NEW)
endif()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
#set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules)

Expand Down Expand Up @@ -80,32 +88,44 @@ if(${OpenCV_VERSION} VERSION_LESS 2.3.1)
endif()

if(BGS_PYTHON_SUPPORT)
if(WIN32)
set(Boost_USE_STATIC_LIBS ON)
else()
set(Boost_USE_STATIC_LIBS OFF)
endif()
#if(WIN32)
# set(Boost_USE_STATIC_LIBS ON)
#else()
# set(Boost_USE_STATIC_LIBS OFF)
#endif()

set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
#set(Boost_USE_MULTITHREADED ON)
#set(Boost_USE_STATIC_RUNTIME OFF)

message(STATUS "SEARCHING FOR BOOST COMPONENT FOR PYTHON ${BGS_PYTHON_VERSION}")
if(BGS_PYTHON_VERSION EQUAL 2)
find_package(Boost REQUIRED COMPONENTS python)
else()
find_package(Boost REQUIRED COMPONENTS python3)
endif()
find_package(PythonInterp ${BGS_PYTHON_VERSION} REQUIRED)
find_package(PythonLibs ${BGS_PYTHON_VERSION} REQUIRED)

message(STATUS "Boost library status:")
message(STATUS " version: ${Boost_VERSION}")
message(STATUS " libraries: ${Boost_LIBRARIES}")
message(STATUS " include path: ${Boost_INCLUDE_DIRS}")
#message(STATUS "SEARCHING FOR BOOST COMPONENT FOR PYTHON ${BGS_PYTHON_VERSION}")
#if(BGS_PYTHON_VERSION EQUAL 2)
# find_package(Boost REQUIRED COMPONENTS python)
#else()
# find_package(Boost REQUIRED COMPONENTS python3)
#endif()

# Pybind11's cmake scripts enable link time optimization by default. However,
# it makes linking take a really long time and doesn't seem to substantively
# improve runtime performance. So we disable LTO here to make building bgslibrary
# faster.
set(PYBIND11_LTO_CXX_FLAGS "")

#set(PYBIND11_PYTHON_VERSION 2.7 3.5 3.6)
set(PYBIND11_PYTHON_VERSION ${BGS_PYTHON_VERSION})
#find_package(pybind11 REQUIRED)
add_subdirectory(pybind11)

#find_package(PythonInterp ${BGS_PYTHON_VERSION} REQUIRED)
#find_package(PythonLibs ${BGS_PYTHON_VERSION} REQUIRED)

#message(STATUS "Boost library status:")
#message(STATUS " version: ${Boost_VERSION}")
#message(STATUS " libraries: ${Boost_LIBRARIES}")
#message(STATUS " include path: ${Boost_INCLUDE_DIRS}")

message(STATUS "Python library status:")
message(STATUS " executable: ${PYTHON_EXECUTABLE}")
message(STATUS " version: ${PYTHON_VERSION_STRING}")
#message(STATUS " version: ${PYTHON_VERSION_STRING}")
#message(STATUS " libraries: ${PYTHON_LIBRARIES}")
message(STATUS " library: ${PYTHON_LIBRARY}")
message(STATUS " include path: ${PYTHON_INCLUDE_DIRS}")
Expand All @@ -131,11 +151,17 @@ file(GLOB_RECURSE analysis_src package_analysis/*.cpp)
if(BGS_PYTHON_SUPPORT)
file(GLOB_RECURSE bgs_src package_bgs/*.cpp package_bgs/*.c wrapper_python/*.cpp)
file(GLOB_RECURSE bgs_include package_bgs/*.h wrapper_python/*.h)
include_directories(${CMAKE_SOURCE_DIR} ${OpenCV_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS} ${NUMPY_INCLUDE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/pybind11/include)
include_directories(${OpenCV_INCLUDE_DIRS})
#include_directories(${Boost_INCLUDE_DIRS})
include_directories(${PYTHON_INCLUDE_DIRS})
include_directories(${NUMPY_INCLUDE_DIR})
else()
file(GLOB_RECURSE bgs_src package_bgs/*.cpp package_bgs/*.c)
file(GLOB_RECURSE bgs_include package_bgs/*.h)
include_directories(${CMAKE_SOURCE_DIR} ${OpenCV_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${OpenCV_INCLUDE_DIRS})
endif()

# GMG is not available in older OpenCV versions
Expand All @@ -145,43 +171,59 @@ if(${OpenCV_VERSION} VERSION_LESS 2.4.3)
endif()

if(BGS_PYTHON_SUPPORT)
#add_library(libbgs SHARED ${sources} ${bgs_src} ${analysis_src})
add_library(libbgs SHARED ${bgs_src} ${analysis_src})
target_link_libraries(libbgs ${OpenCV_LIBS} ${Boost_LIBRARIES} ${PYTHON_LIBRARY})
target_compile_definitions(libbgs PRIVATE BGS_PYTHON_SUPPORT=1)
#add_library(bgs_python SHARED ${sources} ${bgs_src} ${analysis_src})
#add_library(bgs_python SHARED ${bgs_src} ${analysis_src})
pybind11_add_module(bgs_python ${bgs_src} ${analysis_src})
target_link_libraries(bgs_python PRIVATE ${OpenCV_LIBS} ${PYTHON_LIBRARY} pybind11::module)
#target_link_libraries(bgs_python ${OpenCV_LIBS} ${Boost_LIBRARIES} ${PYTHON_LIBRARY})
#target_link_libraries(bgs_python ${OpenCV_LIBS} ${PYTHON_LIBRARY} pybind11::module)
#target_link_libraries(bgs_python PRIVATE ${OpenCV_LIBS} ${PYTHON_LIBRARY} pybind11::embed)
#set_target_properties(bgs_python PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}" SUFFIX "${PYTHON_MODULE_EXTENSION}")
set_target_properties(bgs_python PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
target_compile_definitions(bgs_python PRIVATE BGS_PYTHON_SUPPORT=1)
install(TARGETS bgs_python DESTINATION ${CMAKE_CURRENT_SOURCE_DIR})
# Set the output library name to bgslibrary because that's what setup.py and distutils expects.
set_property(TARGET bgs_python PROPERTY OUTPUT_NAME "bgs")
else()
#add_library(libbgs STATIC ${sources} ${bgs_src} ${analysis_src})
add_library(libbgs STATIC ${bgs_src} ${analysis_src})
target_link_libraries(libbgs ${OpenCV_LIBS})
#add_library(bgslibrary_core STATIC ${sources} ${bgs_src} ${analysis_src})
add_library(bgslibrary_core STATIC ${bgs_src} ${analysis_src})
target_link_libraries(bgslibrary_core ${OpenCV_LIBS})
set_property(TARGET bgslibrary_core PROPERTY PUBLIC_HEADER ${bgs_include})
endif()
set_property(TARGET libbgs PROPERTY PUBLIC_HEADER ${bgs_include})

if(WIN32)
# set_property(TARGET libbgs PROPERTY SUFFIX ".lib")
if(BGS_PYTHON_SUPPORT)
set_property(TARGET libbgs PROPERTY SUFFIX ".pyd")
endif()
else()
set_property(TARGET libbgs PROPERTY OUTPUT_NAME "bgs")
endif()
#if(WIN32)
# # set_property(TARGET bgslibrary_core PROPERTY SUFFIX ".lib")
# #if(BGS_PYTHON_SUPPORT)
# # set_property(TARGET bgslibrary_core PROPERTY SUFFIX ".pyd")
# #endif()
#else()
# set_property(TARGET bgslibrary_core PROPERTY OUTPUT_NAME "bgs")
#endif()

#if(APPLE)
# if(BGS_PYTHON_SUPPORT)
# set_property(TARGET bgslibrary_core PROPERTY SUFFIX ".so")
# set_target_properties(bgslibrary_core PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
# endif()
#endif()

if(NOT BGS_PYTHON_SUPPORT)
add_executable(bgslibrary ${main})
target_link_libraries(bgslibrary ${OpenCV_LIBS} libbgs)
# set_target_properties(bgslibrary PROPERTIES OUTPUT_NAME bgs)

add_executable(bgs_demo ${demo})
target_link_libraries(bgs_demo ${OpenCV_LIBS} libbgs)

add_executable(bgs_demo2 ${demo2})
target_link_libraries(bgs_demo2 ${OpenCV_LIBS} libbgs)

install(TARGETS libbgs
bgslibrary
RUNTIME DESTINATION bin COMPONENT app
LIBRARY DESTINATION lib COMPONENT runtime
ARCHIVE DESTINATION lib COMPONENT runtime
PUBLIC_HEADER DESTINATION include/package_bgs COMPONENT dev
FRAMEWORK DESTINATION "/Library/Frameworks"
)
add_executable(bgslibrary ${main})
target_link_libraries(bgslibrary ${OpenCV_LIBS} bgslibrary_core)
# set_target_properties(bgslibrary PROPERTIES OUTPUT_NAME bgs)

add_executable(bgs_demo ${demo})
target_link_libraries(bgs_demo ${OpenCV_LIBS} bgslibrary_core)

add_executable(bgs_demo2 ${demo2})
target_link_libraries(bgs_demo2 ${OpenCV_LIBS} bgslibrary_core)

install(TARGETS bgslibrary_core
bgslibrary
RUNTIME DESTINATION bin COMPONENT app
LIBRARY DESTINATION lib COMPONENT runtime
ARCHIVE DESTINATION lib COMPONENT runtime
PUBLIC_HEADER DESTINATION include/package_bgs COMPONENT dev
FRAMEWORK DESTINATION "/Library/Frameworks"
)
endif()
52 changes: 4 additions & 48 deletions Demo.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,8 @@
import numpy as np
import cv2
import libbgs

## BGS Library algorithms
bgs = libbgs.FrameDifference()
#bgs = libbgs.StaticFrameDifference()
#bgs = libbgs.AdaptiveBackgroundLearning()
#bgs = libbgs.AdaptiveSelectiveBackgroundLearning()
#bgs = libbgs.DPAdaptiveMedian()
#bgs = libbgs.DPEigenbackground()
#bgs = libbgs.DPGrimsonGMM()
#bgs = libbgs.DPMean()
#bgs = libbgs.DPPratiMediod()
#bgs = libbgs.DPTexture()
#bgs = libbgs.DPWrenGA()
#bgs = libbgs.DPZivkovicAGMM()
#bgs = libbgs.FuzzyChoquetIntegral()
#bgs = libbgs.FuzzySugenoIntegral()
#bgs = libbgs.GMG() # if opencv 2.x
#bgs = libbgs.IndependentMultimodal()
#bgs = libbgs.KDE()
#bgs = libbgs.KNN() # if opencv 3.x
#bgs = libbgs.LBAdaptiveSOM()
#bgs = libbgs.LBFuzzyAdaptiveSOM()
#bgs = libbgs.LBFuzzyGaussian()
#bgs = libbgs.LBMixtureOfGaussians()
#bgs = libbgs.LBSimpleGaussian()
#bgs = libbgs.LBP_MRF()
#bgs = libbgs.LOBSTER()
#bgs = libbgs.MixtureOfGaussianV1() # if opencv 2.x
#bgs = libbgs.MixtureOfGaussianV2()
#bgs = libbgs.MultiCue()
#bgs = libbgs.MultiLayer()
#bgs = libbgs.PAWCS()
#bgs = libbgs.PixelBasedAdaptiveSegmenter()
#bgs = libbgs.SigmaDelta()
#bgs = libbgs.SuBSENSE()
#bgs = libbgs.T2FGMM_UM()
#bgs = libbgs.T2FGMM_UV()
#bgs = libbgs.T2FMRF_UM()
#bgs = libbgs.T2FMRF_UV()
#bgs = libbgs.VuMeter()
#bgs = libbgs.WeightedMovingMean()
#bgs = libbgs.WeightedMovingVariance()
#bgs = libbgs.TwoPoints()
#bgs = libbgs.ViBe()
#bgs = libbgs.CodeBook()
import bgs

algorithm = bgs.FrameDifference()
video_file = "dataset/video.avi"

capture = cv2.VideoCapture(video_file)
Expand All @@ -68,8 +24,8 @@
pos_frame = capture.get(1)
#print str(pos_frame)+" frames"

img_output = bgs.apply(frame)
img_bgmodel = bgs.getBackgroundModel();
img_output = algorithm.apply(frame)
img_bgmodel = algorithm.getBackgroundModel()

cv2.imshow('img_output', img_output)
cv2.imshow('img_bgmodel', img_bgmodel)
Expand Down
Loading

0 comments on commit f1e770e

Please sign in to comment.