From 1d1bfd3d7d305339342bf2b0c752c18aae9694d8 Mon Sep 17 00:00:00 2001 From: Alexander Mordvintsev Date: Tue, 22 Jul 2014 12:36:36 +0400 Subject: [PATCH 1/7] automatic search for headers for python wrapper generation --- modules/python/CMakeLists.txt | 41 +++++++++++++++-------------------- modules/python/src2/gen2.py | 4 +++- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/modules/python/CMakeLists.txt b/modules/python/CMakeLists.txt index af062cebc7..4eaa885254 100644 --- a/modules/python/CMakeLists.txt +++ b/modules/python/CMakeLists.txt @@ -19,31 +19,24 @@ ocv_module_include_directories( "${CMAKE_CURRENT_SOURCE_DIR}/src2" ) -set(opencv_hdrs - "${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core.hpp" - "${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core/base.hpp" - "${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core/types.hpp" - "${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core/persistence.hpp" - "${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core/utility.hpp" - "${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core/ocl.hpp" - "${OPENCV_MODULE_opencv_flann_LOCATION}/include/opencv2/flann/miniflann.hpp" - "${OPENCV_MODULE_opencv_imgproc_LOCATION}/include/opencv2/imgproc.hpp" - "${OPENCV_MODULE_opencv_video_LOCATION}/include/opencv2/video/background_segm.hpp" - "${OPENCV_MODULE_opencv_video_LOCATION}/include/opencv2/video/tracking.hpp" - "${OPENCV_MODULE_opencv_photo_LOCATION}/include/opencv2/photo.hpp" - "${OPENCV_MODULE_opencv_imgcodecs_LOCATION}/include/opencv2/imgcodecs.hpp" - "${OPENCV_MODULE_opencv_videoio_LOCATION}/include/opencv2/videoio.hpp" - "${OPENCV_MODULE_opencv_highgui_LOCATION}/include/opencv2/highgui.hpp" - "${OPENCV_MODULE_opencv_ml_LOCATION}/include/opencv2/ml.hpp" - "${OPENCV_MODULE_opencv_features2d_LOCATION}/include/opencv2/features2d.hpp" - "${OPENCV_MODULE_opencv_calib3d_LOCATION}/include/opencv2/calib3d.hpp" - "${OPENCV_MODULE_opencv_objdetect_LOCATION}/include/opencv2/objdetect.hpp" - ) -if(HAVE_opencv_nonfree) - list(APPEND opencv_hdrs "${OPENCV_MODULE_opencv_nonfree_LOCATION}/include/opencv2/nonfree/features2d.hpp" - "${OPENCV_MODULE_opencv_nonfree_LOCATION}/include/opencv2/nonfree.hpp") -endif() +set(opencv_hdrs_raw) +foreach(mod_name IN LISTS OPENCV_MODULE_opencv_python_DEPS) + list(APPEND opencv_hdrs_raw "${OPENCV_MODULE_${mod_name}_HEADERS}") +endforeach(mod_name) + +# remove problematic headers +set(opencv_hdrs) +foreach(hdr IN LISTS opencv_hdrs_raw) + if(NOT ${hdr} MATCHES ".h$") + if(NOT ${hdr} MATCHES "opencv2/core/cuda") + if(NOT ${hdr} MATCHES "opencv2/objdetect/detection_based_tracker.hpp") + list(APPEND opencv_hdrs ${hdr}) + endif() + endif() + endif() +endforeach(hdr) + set(cv2_generated_hdrs "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_include.h" diff --git a/modules/python/src2/gen2.py b/modules/python/src2/gen2.py index b613ccd4ac..5666355bb6 100755 --- a/modules/python/src2/gen2.py +++ b/modules/python/src2/gen2.py @@ -831,8 +831,10 @@ class PythonWrapperGenerator(object): # step 1: scan the headers and build more descriptive maps of classes, consts, functions for hdr in srcfiles: - self.code_include.write( '#include "{}"\n'.format(hdr[hdr.rindex('opencv2/'):]) ) decls = parser.parse(hdr) + if len(decls) == 0: + continue + self.code_include.write( '#include "{}"\n'.format(hdr[hdr.rindex('opencv2/'):]) ) for decl in decls: name = decl[0] if name.startswith("struct") or name.startswith("class"): From 964657a155899f40ad5fedd7c3afd248566d860d Mon Sep 17 00:00:00 2001 From: Alexander Mordvintsev Date: Tue, 22 Jul 2014 14:22:46 +0400 Subject: [PATCH 2/7] glob for python module dependences --- modules/python/CMakeLists.txt | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/modules/python/CMakeLists.txt b/modules/python/CMakeLists.txt index 4eaa885254..3da937fccc 100644 --- a/modules/python/CMakeLists.txt +++ b/modules/python/CMakeLists.txt @@ -11,7 +11,26 @@ if(ANDROID OR IOS OR NOT PYTHONLIBS_FOUND OR NOT PYTHON_NUMPY_INCLUDE_DIRS) endif() set(the_description "The python bindings") -ocv_add_module(python BINDINGS opencv_core opencv_flann opencv_imgproc opencv_video opencv_ml opencv_features2d opencv_imgcodecs opencv_videoio opencv_highgui opencv_calib3d opencv_photo opencv_objdetect OPTIONAL opencv_nonfree) + +set(candidate_deps_raw) +foreach(mp ${OPENCV_MODULES_PATH} ${OPENCV_EXTRA_MODULES_PATH}) + file(GLOB names "${mp}/*") + list(APPEND candidate_deps_raw ${names}) +endforeach(mp) + +set(candidate_deps) +foreach(m IN LISTS candidate_deps_raw) + if(IS_DIRECTORY ${m}) + get_filename_component(m ${m} NAME) + if (NOT ${m} MATCHES "^cud(a|ev)") + if (NOT ${m} MATCHES "optim") # unless we handle namespaces + list(APPEND candidate_deps "opencv_${m}") + endif() + endif() + endif() +endforeach(m) + +ocv_add_module(python BINDINGS OPTIONAL ${candidate_deps}) ocv_module_include_directories( "${PYTHON_INCLUDE_PATH}" @@ -21,9 +40,9 @@ ocv_module_include_directories( set(opencv_hdrs_raw) -foreach(mod_name IN LISTS OPENCV_MODULE_opencv_python_DEPS) - list(APPEND opencv_hdrs_raw "${OPENCV_MODULE_${mod_name}_HEADERS}") -endforeach(mod_name) +foreach(m IN LISTS OPENCV_MODULE_opencv_python_DEPS) + list(APPEND opencv_hdrs_raw "${OPENCV_MODULE_${m}_HEADERS}") +endforeach(m) # remove problematic headers set(opencv_hdrs) From 42ecfc5538b3da27f3ec4c0d5cd3c214547f8a6b Mon Sep 17 00:00:00 2001 From: Alexander Mordvintsev Date: Wed, 23 Jul 2014 14:54:04 +0400 Subject: [PATCH 3/7] fixed parsing enums with tailing comma gen2.py reports headers it processes cmake file restructured --- modules/python/CMakeLists.txt | 56 +++++++++++++++---------------- modules/python/src2/gen2.py | 1 + modules/python/src2/hdr_parser.py | 2 ++ 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/modules/python/CMakeLists.txt b/modules/python/CMakeLists.txt index 3da937fccc..cccf0c3915 100644 --- a/modules/python/CMakeLists.txt +++ b/modules/python/CMakeLists.txt @@ -12,23 +12,18 @@ endif() set(the_description "The python bindings") -set(candidate_deps_raw) +set(candidate_deps) foreach(mp ${OPENCV_MODULES_PATH} ${OPENCV_EXTRA_MODULES_PATH}) file(GLOB names "${mp}/*") - list(APPEND candidate_deps_raw ${names}) -endforeach(mp) - -set(candidate_deps) -foreach(m IN LISTS candidate_deps_raw) - if(IS_DIRECTORY ${m}) - get_filename_component(m ${m} NAME) - if (NOT ${m} MATCHES "^cud(a|ev)") - if (NOT ${m} MATCHES "optim") # unless we handle namespaces - list(APPEND candidate_deps "opencv_${m}") + foreach(m IN LISTS names) + if(IS_DIRECTORY ${m}) + get_filename_component(m ${m} NAME) + if (NOT ${m} MATCHES "^cud(a|ev)") + list(APPEND candidate_deps "opencv_${m}") + endif() endif() - endif() - endif() -endforeach(m) + endforeach(m) +endforeach(mp) ocv_add_module(python BINDINGS OPTIONAL ${candidate_deps}) @@ -39,23 +34,26 @@ ocv_module_include_directories( ) -set(opencv_hdrs_raw) -foreach(m IN LISTS OPENCV_MODULE_opencv_python_DEPS) - list(APPEND opencv_hdrs_raw "${OPENCV_MODULE_${m}_HEADERS}") -endforeach(m) +set(opencv_hdrs_blacklist + ".h$" + "opencv2/core/cuda" + "opencv2/objdetect/detection_based_tracker.hpp" + "opencv2/optim.hpp") -# remove problematic headers set(opencv_hdrs) -foreach(hdr IN LISTS opencv_hdrs_raw) - if(NOT ${hdr} MATCHES ".h$") - if(NOT ${hdr} MATCHES "opencv2/core/cuda") - if(NOT ${hdr} MATCHES "opencv2/objdetect/detection_based_tracker.hpp") - list(APPEND opencv_hdrs ${hdr}) - endif() - endif() - endif() -endforeach(hdr) - +foreach(m IN LISTS OPENCV_MODULE_opencv_python_DEPS) + foreach(hdr IN LISTS OPENCV_MODULE_${m}_HEADERS) + set(good TRUE) + foreach(s IN LISTS opencv_hdrs_blacklist) + if (${hdr} MATCHES ${s}) + set(good FALSE) + endif() + endforeach(s) + if(${good}) + list(APPEND opencv_hdrs ${hdr}) + endif() + endforeach(hdr) +endforeach(m) set(cv2_generated_hdrs "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_include.h" diff --git a/modules/python/src2/gen2.py b/modules/python/src2/gen2.py index 5666355bb6..9488107715 100755 --- a/modules/python/src2/gen2.py +++ b/modules/python/src2/gen2.py @@ -831,6 +831,7 @@ class PythonWrapperGenerator(object): # step 1: scan the headers and build more descriptive maps of classes, consts, functions for hdr in srcfiles: + print(hdr) decls = parser.parse(hdr) if len(decls) == 0: continue diff --git a/modules/python/src2/hdr_parser.py b/modules/python/src2/hdr_parser.py index 92f1b7347c..eb9100928f 100755 --- a/modules/python/src2/hdr_parser.py +++ b/modules/python/src2/hdr_parser.py @@ -206,6 +206,8 @@ class CppHeaderParser(object): def parse_enum(self, decl_str): l = decl_str ll = l.split(",") + if ll[-1].strip() == "": + ll = ll[:-1] prev_val = "" prev_val_delta = -1 decl = [] From 9ab3b89c53aa0153111398b08f880f68bb7e497d Mon Sep 17 00:00:00 2001 From: Alexander Mordvintsev Date: Sun, 27 Jul 2014 16:35:35 +0400 Subject: [PATCH 4/7] pass header list to gen2.py by a text file (avoid command line length limit on windows) --- modules/python/CMakeLists.txt | 36 +++++++++++++---------------------- modules/python/src2/gen2.py | 3 +-- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/modules/python/CMakeLists.txt b/modules/python/CMakeLists.txt index cccf0c3915..57a8ed7b4f 100644 --- a/modules/python/CMakeLists.txt +++ b/modules/python/CMakeLists.txt @@ -12,18 +12,17 @@ endif() set(the_description "The python bindings") -set(candidate_deps) +set(candidate_deps "") foreach(mp ${OPENCV_MODULES_PATH} ${OPENCV_EXTRA_MODULES_PATH}) file(GLOB names "${mp}/*") foreach(m IN LISTS names) if(IS_DIRECTORY ${m}) get_filename_component(m ${m} NAME) - if (NOT ${m} MATCHES "^cud(a|ev)") - list(APPEND candidate_deps "opencv_${m}") - endif() + list(APPEND candidate_deps "opencv_${m}") endif() endforeach(m) endforeach(mp) +ocv_list_filterout(candidate_deps "^opencv_cud(a|ev)") ocv_add_module(python BINDINGS OPTIONAL ${candidate_deps}) @@ -34,27 +33,16 @@ ocv_module_include_directories( ) -set(opencv_hdrs_blacklist - ".h$" - "opencv2/core/cuda" - "opencv2/objdetect/detection_based_tracker.hpp" - "opencv2/optim.hpp") - -set(opencv_hdrs) +set(opencv_hdrs "") foreach(m IN LISTS OPENCV_MODULE_opencv_python_DEPS) - foreach(hdr IN LISTS OPENCV_MODULE_${m}_HEADERS) - set(good TRUE) - foreach(s IN LISTS opencv_hdrs_blacklist) - if (${hdr} MATCHES ${s}) - set(good FALSE) - endif() - endforeach(s) - if(${good}) - list(APPEND opencv_hdrs ${hdr}) - endif() - endforeach(hdr) + list(APPEND opencv_hdrs ${OPENCV_MODULE_${m}_HEADERS}) endforeach(m) +ocv_list_filterout(opencv_hdrs ".h$") +ocv_list_filterout(opencv_hdrs "opencv2/core/cuda") +ocv_list_filterout(opencv_hdrs "opencv2/objdetect/detection_based_tracker.hpp") +ocv_list_filterout(opencv_hdrs "opencv2/optim.hpp") + set(cv2_generated_hdrs "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_include.h" "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_funcs.h" @@ -63,11 +51,13 @@ set(cv2_generated_hdrs "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_type_reg.h" "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_const_reg.h") +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/headers.txt" "${opencv_hdrs}") add_custom_command( OUTPUT ${cv2_generated_hdrs} - COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/src2/gen2.py" ${CMAKE_CURRENT_BINARY_DIR} ${opencv_hdrs} + COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/src2/gen2.py" ${CMAKE_CURRENT_BINARY_DIR} "${CMAKE_CURRENT_BINARY_DIR}/headers.txt" DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/gen2.py DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/hdr_parser.py + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/headers.txt DEPENDS ${opencv_hdrs}) add_library(${the_module} SHARED src2/cv2.cpp ${cv2_generated_hdrs}) diff --git a/modules/python/src2/gen2.py b/modules/python/src2/gen2.py index 9488107715..684b80f4e8 100755 --- a/modules/python/src2/gen2.py +++ b/modules/python/src2/gen2.py @@ -831,7 +831,6 @@ class PythonWrapperGenerator(object): # step 1: scan the headers and build more descriptive maps of classes, consts, functions for hdr in srcfiles: - print(hdr) decls = parser.parse(hdr) if len(decls) == 0: continue @@ -904,6 +903,6 @@ if __name__ == "__main__": if len(sys.argv) > 1: dstdir = sys.argv[1] if len(sys.argv) > 2: - srcfiles = sys.argv[2:] + srcfiles = open(sys.argv[2], 'r').read().split(';') generator = PythonWrapperGenerator() generator.gen(srcfiles, dstdir) From a2ae1db31c8a831c254952550786b3c589319285 Mon Sep 17 00:00:00 2001 From: Alexander Mordvintsev Date: Sun, 27 Jul 2014 17:41:12 +0400 Subject: [PATCH 5/7] 'adas' extra module doesn't produce .lib on windows (causes link error) --- modules/python/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/python/CMakeLists.txt b/modules/python/CMakeLists.txt index 57a8ed7b4f..c41d19a3f9 100644 --- a/modules/python/CMakeLists.txt +++ b/modules/python/CMakeLists.txt @@ -23,6 +23,8 @@ foreach(mp ${OPENCV_MODULES_PATH} ${OPENCV_EXTRA_MODULES_PATH}) endforeach(m) endforeach(mp) ocv_list_filterout(candidate_deps "^opencv_cud(a|ev)") +ocv_list_filterout(candidate_deps "^opencv_adas$") + ocv_add_module(python BINDINGS OPTIONAL ${candidate_deps}) From 5f47f70183612d6a1228e5dd3d97529230fa4b23 Mon Sep 17 00:00:00 2001 From: Alexander Mordvintsev Date: Sun, 27 Jul 2014 18:03:27 +0400 Subject: [PATCH 6/7] module blacklist --- modules/python/CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/python/CMakeLists.txt b/modules/python/CMakeLists.txt index c41d19a3f9..66ac1addd6 100644 --- a/modules/python/CMakeLists.txt +++ b/modules/python/CMakeLists.txt @@ -22,8 +22,11 @@ foreach(mp ${OPENCV_MODULES_PATH} ${OPENCV_EXTRA_MODULES_PATH}) endif() endforeach(m) endforeach(mp) + +# module blacklist ocv_list_filterout(candidate_deps "^opencv_cud(a|ev)") ocv_list_filterout(candidate_deps "^opencv_adas$") +ocv_list_filterout(candidate_deps "^opencv_tracking$") ocv_add_module(python BINDINGS OPTIONAL ${candidate_deps}) @@ -40,6 +43,7 @@ foreach(m IN LISTS OPENCV_MODULE_opencv_python_DEPS) list(APPEND opencv_hdrs ${OPENCV_MODULE_${m}_HEADERS}) endforeach(m) +# header blacklist ocv_list_filterout(opencv_hdrs ".h$") ocv_list_filterout(opencv_hdrs "opencv2/core/cuda") ocv_list_filterout(opencv_hdrs "opencv2/objdetect/detection_based_tracker.hpp") From 586ace1873fbf2f588b2edce29e6917c73c06e67 Mon Sep 17 00:00:00 2001 From: Alexander Mordvintsev Date: Sun, 27 Jul 2014 18:25:42 +0400 Subject: [PATCH 7/7] double to float cast warning fix --- modules/shape/include/opencv2/shape/hist_cost.hpp | 8 ++++---- modules/shape/include/opencv2/shape/shape_distance.hpp | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/shape/include/opencv2/shape/hist_cost.hpp b/modules/shape/include/opencv2/shape/hist_cost.hpp index 9ca3825fda..0ff3573eea 100644 --- a/modules/shape/include/opencv2/shape/hist_cost.hpp +++ b/modules/shape/include/opencv2/shape/hist_cost.hpp @@ -73,7 +73,7 @@ public: }; CV_EXPORTS_W Ptr - createNormHistogramCostExtractor(int flag=DIST_L2, int nDummies=25, float defaultCost=0.2); + createNormHistogramCostExtractor(int flag=DIST_L2, int nDummies=25, float defaultCost=0.2f); /*! */ class CV_EXPORTS_W EMDHistogramCostExtractor : public HistogramCostExtractor @@ -84,20 +84,20 @@ public: }; CV_EXPORTS_W Ptr - createEMDHistogramCostExtractor(int flag=DIST_L2, int nDummies=25, float defaultCost=0.2); + createEMDHistogramCostExtractor(int flag=DIST_L2, int nDummies=25, float defaultCost=0.2f); /*! */ class CV_EXPORTS_W ChiHistogramCostExtractor : public HistogramCostExtractor {}; -CV_EXPORTS_W Ptr createChiHistogramCostExtractor(int nDummies=25, float defaultCost=0.2); +CV_EXPORTS_W Ptr createChiHistogramCostExtractor(int nDummies=25, float defaultCost=0.2f); /*! */ class CV_EXPORTS_W EMDL1HistogramCostExtractor : public HistogramCostExtractor {}; CV_EXPORTS_W Ptr - createEMDL1HistogramCostExtractor(int nDummies=25, float defaultCost=0.2); + createEMDL1HistogramCostExtractor(int nDummies=25, float defaultCost=0.2f); } // cv #endif diff --git a/modules/shape/include/opencv2/shape/shape_distance.hpp b/modules/shape/include/opencv2/shape/shape_distance.hpp index 55e21aaa4a..acdb6e5f6e 100644 --- a/modules/shape/include/opencv2/shape/shape_distance.hpp +++ b/modules/shape/include/opencv2/shape/shape_distance.hpp @@ -116,7 +116,7 @@ public: /* Complete constructor */ CV_EXPORTS_W Ptr createShapeContextDistanceExtractor(int nAngularBins=12, int nRadialBins=4, - float innerRadius=0.2, float outerRadius=2, int iterations=3, + float innerRadius=0.2f, float outerRadius=2, int iterations=3, const Ptr &comparer = createChiHistogramCostExtractor(), const Ptr &transformer = createThinPlateSplineShapeTransformer()); @@ -137,7 +137,7 @@ public: }; /* Constructor */ -CV_EXPORTS_W Ptr createHausdorffDistanceExtractor(int distanceFlag=cv::NORM_L2, float rankProp=0.6); +CV_EXPORTS_W Ptr createHausdorffDistanceExtractor(int distanceFlag=cv::NORM_L2, float rankProp=0.6f); } // cv #endif