From 707d3e0a6240de70050587f73be02464ef6d9130 Mon Sep 17 00:00:00 2001 From: Andrey Kamaev Date: Wed, 20 Mar 2013 20:42:07 +0400 Subject: [PATCH] Python wrapper is adapted for cv::String --- modules/python/CMakeLists.txt | 2 ++ modules/python/src2/cv2.cpp | 18 ++++++++---------- modules/python/src2/hdr_parser.py | 2 +- modules/python/test/test2.py | 13 +++++++++++++ 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/modules/python/CMakeLists.txt b/modules/python/CMakeLists.txt index 5a42a7b62b..591490fe61 100644 --- a/modules/python/CMakeLists.txt +++ b/modules/python/CMakeLists.txt @@ -65,6 +65,8 @@ add_custom_command( DEPENDS ${opencv_hdrs}) add_library(${the_module} SHARED src2/cv2.cpp ${CMAKE_CURRENT_BINARY_DIR}/generated0.i ${cv2_generated_hdrs} src2/cv2.cv.hpp) +set_target_properties(${the_module} PROPERTIES COMPILE_DEFINITIONS OPENCV_NOSTL) + if(PYTHON_DEBUG_LIBRARIES AND NOT PYTHON_LIBRARIES MATCHES "optimized.*debug") target_link_libraries(${the_module} debug ${PYTHON_DEBUG_LIBRARIES} optimized ${PYTHON_LIBRARIES}) else() diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index 36aaf740ba..f0242f9314 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -100,8 +100,6 @@ catch (const cv::Exception &e) \ using namespace cv; typedef cv::softcascade::ChannelFeatureBuilder softcascade_ChannelFeatureBuilder; -typedef std::string string; - typedef std::vector vector_uchar; typedef std::vector vector_int; typedef std::vector vector_float; @@ -117,7 +115,7 @@ typedef std::vector vector_Rect; typedef std::vector vector_KeyPoint; typedef std::vector vector_Mat; typedef std::vector vector_DMatch; -typedef std::vector vector_string; +typedef std::vector vector_String; typedef std::vector > vector_vector_Point; typedef std::vector > vector_vector_Point2f; typedef std::vector > vector_vector_Point3f; @@ -550,12 +548,12 @@ static PyObject* pyopencv_from(int64 value) return PyLong_FromLongLong(value); } -static PyObject* pyopencv_from(const std::string& value) +static PyObject* pyopencv_from(const cv::String& value) { return PyString_FromString(value.empty() ? "" : value.c_str()); } -static bool pyopencv_to(PyObject* obj, std::string& value, const char* name = "") +static bool pyopencv_to(PyObject* obj, cv::String& value, const char* name = "") { (void)name; if(!obj || obj == Py_None) @@ -563,7 +561,7 @@ static bool pyopencv_to(PyObject* obj, std::string& value, const char* name = "< char* str = PyString_AsString(obj); if(!str) return false; - value = std::string(str); + value = cv::String(str); return true; } @@ -906,14 +904,14 @@ template<> struct pyopencvVecConverter } }; -template<> struct pyopencvVecConverter +template<> struct pyopencvVecConverter { - static bool to(PyObject* obj, std::vector& value, const ArgInfo info) + static bool to(PyObject* obj, std::vector& value, const ArgInfo info) { return pyopencv_to_generic_vec(obj, value, info); } - static PyObject* from(const std::vector& value) + static PyObject* from(const std::vector& value) { return pyopencv_from_generic_vec(value); } @@ -994,7 +992,7 @@ static bool pyopencv_to(PyObject *o, cv::flann::IndexParams& p, const char *name PyObject* item = PyList_GET_ITEM(values, i); if( !PyString_Check(key) ) break; - std::string k = PyString_AsString(key); + cv::String k = PyString_AsString(key); if( PyString_Check(item) ) { const char* value = PyString_AsString(item); diff --git a/modules/python/src2/hdr_parser.py b/modules/python/src2/hdr_parser.py index 536e180c2f..cf26b59ebe 100755 --- a/modules/python/src2/hdr_parser.py +++ b/modules/python/src2/hdr_parser.py @@ -335,7 +335,7 @@ class CppHeaderParser(object): if pos >= 0: aname = arg[pos+1:].strip() atype = arg[:pos+1].strip() - if aname.endswith("&") or aname.endswith("*") or (aname in ["int", "string", "Mat"]): + if aname.endswith("&") or aname.endswith("*") or (aname in ["int", "String", "Mat"]): atype = (atype + " " + aname).strip() aname = "" else: diff --git a/modules/python/test/test2.py b/modules/python/test/test2.py index a331d69277..8796e70198 100644 --- a/modules/python/test/test2.py +++ b/modules/python/test/test2.py @@ -32,6 +32,19 @@ class NewOpenCVTests(unittest.TestCase): """ Compute a hash for an image, useful for image comparisons """ return hashlib.md5(im.tostring()).digest() + if sys.version_info[:2] == (2, 6): + def assertLess(self, a, b, msg=None): + if not a < b: + self.fail('%s not less than %s' % (repr(a), repr(b))) + + def assertLessEqual(self, a, b, msg=None): + if not a <= b: + self.fail('%s not less than or equal to %s' % (repr(a), repr(b))) + + def assertGreater(self, a, b, msg=None): + if not a > b: + self.fail('%s not greater than %s' % (repr(a), repr(b))) + # Tests to run first; check the handful of basic operations that the later tests rely on class Hackathon244Tests(NewOpenCVTests):