Skip to content

Commit

Permalink
Add Python API.
Browse files Browse the repository at this point in the history
  • Loading branch information
kyu-sz committed Apr 7, 2017
1 parent ff5ed40 commit 96b217c
Show file tree
Hide file tree
Showing 25 changed files with 417 additions and 17 deletions.
14 changes: 13 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,16 @@ tmp/
*.suo
*.sdf
*.pdb
*.opensdf
*.opensdf
*.so
*.cbp
*.pyc

.idea/
CMakeCache.txt
CMakeFiles/
Makefile
cmake-build-debug/
cmake_install.cmake
simple_vehicle_counting
pbcvt.cbp
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "python/pyboostcvconverter"]
path = python/pyboostcvconverter
url = https://github.com/kyu-sz/pyboostcvconverter.git
40 changes: 27 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ project(simple_vehicle_counting)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")

IF(UNIX)
# add some standard warnings
ADD_DEFINITIONS(-Wno-variadic-macros -Wno-long-long -Wall -Wextra -Winit-self -Woverloaded-virtual -Wsign-promo -Wno-unused-parameter -pedantic -Woverloaded-virtual -Wno-unknown-pragmas)
IF (UNIX)
# add some standard warnings
ADD_DEFINITIONS(-Wno-variadic-macros -Wno-long-long -Wall -Wextra -Winit-self -Woverloaded-virtual -Wsign-promo -Wno-unused-parameter -pedantic -Woverloaded-virtual -Wno-unknown-pragmas)

# -ansi does not compile with sjn module
#ADD_DEFINITIONS(-ansi)
# -ansi does not compile with sjn module
#ADD_DEFINITIONS(-ansi)

# if you like to have warinings about conversions, e.g. double->int or double->float etc., or float compare
#ADD_DEFINITIONS(-Wconversion -Wfloat-equal)
endif(UNIX)
# if you like to have warinings about conversions, e.g. double->int or double->float etc., or float compare
#ADD_DEFINITIONS(-Wconversion -Wfloat-equal)
endif (UNIX)

find_package(OpenCV REQUIRED)

if(${OpenCV_VERSION} VERSION_LESS 2.3.1)
message (FATAL_ERROR "OpenCV version is not compatible: ${OpenCV_VERSION}")
endif()
if (${OpenCV_VERSION} VERSION_LESS 2.3.1)
message(FATAL_ERROR "OpenCV version is not compatible: ${OpenCV_VERSION}")
endif ()

file(GLOB demo Demo.cpp)

Expand All @@ -40,5 +40,19 @@ set_property(TARGET simple_vehicle_counting PROPERTY PUBLIC_HEADER ${bgs_include
add_executable(simple_vehicle_counting_bin ${demo})
target_link_libraries(simple_vehicle_counting_bin ${OpenCV_LIBS} simple_vehicle_counting)
set_target_properties(simple_vehicle_counting_bin
PROPERTIES OUTPUT_NAME simple_vehicle_counting)

PROPERTIES OUTPUT_NAME simple_vehicle_counting)

set(PYTHON_DESIRED_VERSION "2.X" CACHE STRING "Use Python 2.")
add_subdirectory(python/pyboostcvconverter)

find_package(Boost COMPONENTS python REQUIRED)
message("Include dirs of boost: " ${Boost_INCLUDE_DIRS})
message("Libs of boost: " ${Boost_LIBRARIES})
find_package(PythonLibs 2.7 REQUIRED)
message("Include dirs of Python: " ${PYTHON_INCLUDE_DIRS})
message("Libs of Python: " ${PYTHON_LIBRARIES})

add_subdirectory(python/tracking)
add_subdirectory(python/cvb)
add_subdirectory(python/bgs)
add_subdirectory(python/analysis)
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ For Linux users
* * Requirements: OpenCV 2.4.x (it only works with this version).
* * Check out the latest project source code and compile it:
```
~/git clone https://github.com/andrewssobral/simple_vehicle_counting.git
~/git clone --recursive https://github.com/andrewssobral/simple_vehicle_counting.git
~/cd simple_vehicle_counting
~/simple_vehicle_counting/cd build
~/simple_vehicle_counting/build/ cmake ..
Expand Down Expand Up @@ -131,6 +131,24 @@ int main(int argc, char **argv)
}
```
Python API
----------------------------------------
A [python demo](python/demo.py) shows how to call the Python API.
It is similar as the [C++ demo](Demo.cpp).
To use the Python API, you should copy ["python" directory](python) to overwrite the generated one.
```
~/simple_vehicle_counting/cd build
~/simple_vehicle_counting/build/cmake ..
~/simple_vehicle_counting/build/make -j 8
~/simple_vehicle_counting/build/cp -r ../python/* python/
~/simple_vehicle_counting/build/../run_python_demo.sh
```
If you have previously built the project at the project root,
make sure there are no previously generated libraries in the ["python" directory](python) by ```make clean```.
Release Notes:
--------------
* Version 1.0.0:
Expand Down
4 changes: 2 additions & 2 deletions config/PixelBasedAdaptiveSegmenter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
<beta>1.</beta>
<N>20</N>
<Raute_min>2</Raute_min>
<R_incdec>5.0000000745058060e-002</R_incdec>
<R_incdec>5.0000000745058060e-02</R_incdec>
<R_lower>18</R_lower>
<R_scale>5</R_scale>
<T_dec>5.0000000745058060e-002</T_dec>
<T_dec>5.0000000745058060e-02</T_dec>
<T_inc>1</T_inc>
<T_init>18</T_init>
<T_lower>2</T_lower>
Expand Down
15 changes: 15 additions & 0 deletions python/analysis/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
project(analysis)

file(GLOB_RECURSE SOURCE_FILES *.cpp)
add_library(_${PROJECT_NAME} SHARED ${SOURCE_FILES})
target_include_directories(_${PROJECT_NAME} PUBLIC
${PYTHON_INCLUDE_DIR}
${Boost_INCLUDE_DIR}
${CMAKE_SOURCE_DIR}/python/pyboostcvconverter/include)
target_link_libraries(_${PROJECT_NAME}
${Boost_LIBRARIES}
${PYTHON_LIBRARIES}
${OpenCV_LIBS}
simple_vehicle_counting
pbcvt)
set_target_properties(_${PROJECT_NAME} PROPERTIES PREFIX "")
24 changes: 24 additions & 0 deletions python/analysis/_VehicleCounting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Created by ken.yu on 17-4-6.
//

#include <boost/python.hpp>

#include "package_analysis/VehicleCouting.h"
#include "pyboostcvconverter/pyboostcvconverter.hpp"

namespace bp = boost::python;

class PyVehicleCouting : public VehicleCouting {
public:
void setInput(const bp::object &i) {
VehicleCouting::setInput(pbcvt::fromNDArrayToMat(i.ptr()));
}
};

BOOST_PYTHON_MODULE (_analysis) {
bp::class_<PyVehicleCouting>("VehicleCouting")
.def("setInput", &PyVehicleCouting::setInput)
.def("setTracks", &PyVehicleCouting::setTracks)
.def("process", &PyVehicleCouting::process);
}
6 changes: 6 additions & 0 deletions python/analysis/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from _analysis import *

# noinspection PyUnresolvedReferences
import cvb
# noinspection PyUnresolvedReferences
import pyboostcvconverter as pbcvt
17 changes: 17 additions & 0 deletions python/bgs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
project(bgs)

file(GLOB_RECURSE SOURCE_FILES *.cpp)
add_library(_${PROJECT_NAME} SHARED ${SOURCE_FILES})
target_include_directories(_${PROJECT_NAME} PUBLIC
${PYTHON_INCLUDE_DIR}
${Boost_INCLUDE_DIR}
${CMAKE_SOURCE_DIR}/python/pyboostcvconverter/include)
target_link_libraries(_${PROJECT_NAME}
${Boost_LIBRARIES}
${PYTHON_LIBRARIES}
${OpenCV_LIBS}
simple_vehicle_counting
pbcvt)
set_target_properties(_${PROJECT_NAME} PROPERTIES PREFIX "")

add_subdirectory(pbas)
19 changes: 19 additions & 0 deletions python/bgs/_IBGS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Created by ken.yu on 17-4-6.
//

#include "_IBGS.hpp"

namespace bp = boost::python;

class PyIBGSWrap : public PyIBGS, public bp::wrapper<PyIBGS> {
public:
PyObject *process(PyObject *) {
return this->get_override("process")();
}
};

BOOST_PYTHON_MODULE (_bgs) {
bp::class_<PyIBGSWrap, boost::noncopyable>("IBGS")
.def("process", bp::pure_virtual(&PyIBGS::process));
}
17 changes: 17 additions & 0 deletions python/bgs/_IBGS.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Created by ken.yu on 17-4-6.
//

#ifndef SIMPLE_VEHICLE_COUNTING_IBGS_HPP
#define SIMPLE_VEHICLE_COUNTING_IBGS_HPP

#include "package_bgs/IBGS.h"

#include <boost/python.hpp>

class PyIBGS {
public:
virtual PyObject *process(PyObject *) = 0;
};

#endif //SIMPLE_VEHICLE_COUNTING_IBGS_HPP
4 changes: 4 additions & 0 deletions python/bgs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from _bgs import *

# noinspection PyUnresolvedReferences
import pyboostcvconverter as pbcvt
15 changes: 15 additions & 0 deletions python/bgs/pbas/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
project(pbas)

file(GLOB_RECURSE SOURCE_FILES *.cpp)
add_library(_${PROJECT_NAME} SHARED ${SOURCE_FILES})
target_include_directories(_${PROJECT_NAME} PUBLIC
${PYTHON_INCLUDE_DIR}
${Boost_INCLUDE_DIR}
${CMAKE_SOURCE_DIR}/python/pyboostcvconverter/include)
target_link_libraries(_${PROJECT_NAME}
${Boost_LIBRARIES}
${PYTHON_LIBRARIES}
${OpenCV_LIBS}
simple_vehicle_counting
pbcvt)
set_target_properties(_${PROJECT_NAME} PROPERTIES PREFIX "")
30 changes: 30 additions & 0 deletions python/bgs/pbas/_PixelBasedAdaptiveSegmenter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Created by ken.yu on 17-4-6.
//

#include <cstdio>

#include "../_IBGS.hpp"

#include "package_bgs/PBAS/PixelBasedAdaptiveSegmenter.h"
#include "pyboostcvconverter/pyboostcvconverter.hpp"

namespace bp = boost::python;

class PyPixelBasedAdaptiveSegmenter : public PyIBGS, public PixelBasedAdaptiveSegmenter {
public:
PyObject *process(PyObject *py_img_input) {
cv::Mat output;
cv::Mat cv_img_input = pbcvt::fromNDArrayToMat(py_img_input);
PixelBasedAdaptiveSegmenter::process(cv_img_input, output);
if (output.empty())
return NULL;
else
return pbcvt::fromMatToNDArray(output);
}
};

BOOST_PYTHON_MODULE (_pbas) {
bp::class_<PyPixelBasedAdaptiveSegmenter>("PixelBasedAdaptiveSegmenter")
.def("process", &PyPixelBasedAdaptiveSegmenter::process);
}
4 changes: 4 additions & 0 deletions python/bgs/pbas/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from _pbas import *

# noinspection PyUnresolvedReferences
import pyboostcvconverter as pbcvt
15 changes: 15 additions & 0 deletions python/cvb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
project(cvb)

file(GLOB_RECURSE SOURCE_FILES *.cpp)
add_library(_${PROJECT_NAME} SHARED ${SOURCE_FILES})
target_include_directories(_${PROJECT_NAME} PUBLIC
${PYTHON_INCLUDE_DIR}
${Boost_INCLUDE_DIR}
${CMAKE_SOURCE_DIR}/python/pyboostcvconverter/include)
target_link_libraries(_${PROJECT_NAME}
${Boost_LIBRARIES}
${PYTHON_LIBRARIES}
${OpenCV_LIBS}
simple_vehicle_counting
pbcvt)
set_target_properties(_${PROJECT_NAME} PROPERTIES PREFIX "")
1 change: 1 addition & 0 deletions python/cvb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from _cvb import *
38 changes: 38 additions & 0 deletions python/cvb/_cvblob.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Created by ken.yu on 17-4-6.")
//

#include <boost/python.hpp>

#include "package_tracking/cvblob/cvblob.h"

namespace bp = boost::python;

BOOST_PYTHON_MODULE (_cvb) {
bp::class_<CvPoint2D64f>("CvPoint2D64f")
.def_readwrite("x", &CvPoint2D64f::x)
.def_readwrite("y", &CvPoint2D64f::y);

bp::class_<cvb::CvTrack>("CvTrack")
.def_readwrite("id", &cvb::CvTrack::id,
"Track identification number.")
.def_readwrite("label", &cvb::CvTrack::label,
"Label assigned to the blob related to this track.")
.def_readwrite("minx", &cvb::CvTrack::minx,
"X min.")
.def_readwrite("maxx", &cvb::CvTrack::maxx,
"X max.")
.def_readwrite("miny", &cvb::CvTrack::miny,
"Y min.")
.def_readwrite("maxy", &cvb::CvTrack::maxy,
"y max.")
.def_readwrite("centroid", &cvb::CvTrack::centroid,
"Centroid.")
.def_readwrite("lifetime", &cvb::CvTrack::lifetime,
"Indicates how much frames the object has been in scene.")
.def_readwrite("active", &cvb::CvTrack::active,
"Indicates number of frames that has been active from last inactive period.")
.def_readwrite("inactive", &cvb::CvTrack::inactive,
"Indicates number of frames that has been missing.");
bp::class_<cvb::CvTracks>("CvTracks");
}
Loading

0 comments on commit 96b217c

Please sign in to comment.