Commit 10d61a2b authored by Maksim Shabunin's avatar Maksim Shabunin

Limited API support for Python3

parent 878af7ad
...@@ -21,15 +21,18 @@ bool pyopencv_to(PyObject *o, dnn::DictValue &dv, const char *name) ...@@ -21,15 +21,18 @@ bool pyopencv_to(PyObject *o, dnn::DictValue &dv, const char *name)
} }
else if (PyFloat_Check(o)) else if (PyFloat_Check(o))
{ {
dv = dnn::DictValue(PyFloat_AS_DOUBLE(o)); dv = dnn::DictValue(PyFloat_AsDouble(o));
return true; return true;
} }
else if (PyString_Check(o)) else
{
std::string str;
if (getUnicodeString(o, str))
{ {
dv = dnn::DictValue(String(PyString_AsString(o))); dv = dnn::DictValue(str);
return true; return true;
} }
else }
return false; return false;
} }
...@@ -134,7 +137,7 @@ public: ...@@ -134,7 +137,7 @@ public:
PyObject* args = PyList_New(inputs.size()); PyObject* args = PyList_New(inputs.size());
for(size_t i = 0; i < inputs.size(); ++i) for(size_t i = 0; i < inputs.size(); ++i)
PyList_SET_ITEM(args, i, pyopencv_from_generic_vec(inputs[i])); PyList_SetItem(args, i, pyopencv_from_generic_vec(inputs[i]));
PyObject* res = PyObject_CallMethodObjArgs(o, PyString_FromString("getMemoryShapes"), args, NULL); PyObject* res = PyObject_CallMethodObjArgs(o, PyString_FromString("getMemoryShapes"), args, NULL);
Py_DECREF(args); Py_DECREF(args);
......
...@@ -27,20 +27,20 @@ bool pyopencv_to(PyObject *o, cv::flann::IndexParams& p, const char *name) ...@@ -27,20 +27,20 @@ bool pyopencv_to(PyObject *o, cv::flann::IndexParams& p, const char *name)
return true; return true;
if(PyDict_Check(o)) { if(PyDict_Check(o)) {
while(PyDict_Next(o, &pos, &key, &item)) { while(PyDict_Next(o, &pos, &key, &item))
if( !PyString_Check(key) ) { {
// get key
std::string k;
if (!getUnicodeString(key, k))
{
ok = false; ok = false;
break; break;
} }
// get value
String k = PyString_AsString(key); if( !!PyBool_Check(item) )
if( PyString_Check(item) )
{ {
const char* value = PyString_AsString(item);
p.setString(k, value);
}
else if( !!PyBool_Check(item) )
p.setBool(k, item == Py_True); p.setBool(k, item == Py_True);
}
else if( PyInt_Check(item) ) else if( PyInt_Check(item) )
{ {
int value = (int)PyInt_AsLong(item); int value = (int)PyInt_AsLong(item);
...@@ -55,10 +55,15 @@ bool pyopencv_to(PyObject *o, cv::flann::IndexParams& p, const char *name) ...@@ -55,10 +55,15 @@ bool pyopencv_to(PyObject *o, cv::flann::IndexParams& p, const char *name)
p.setDouble(k, value); p.setDouble(k, value);
} }
else else
{
std::string val_str;
if (!getUnicodeString(item, val_str))
{ {
ok = false; ok = false;
break; break;
} }
p.setString(k, val_str);
}
} }
} }
......
...@@ -51,11 +51,13 @@ if(NOT HAVE_CUDA) ...@@ -51,11 +51,13 @@ if(NOT HAVE_CUDA)
endif() endif()
set(cv2_generated_files set(cv2_generated_files
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_include.h" "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_enums.h"
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_funcs.h" "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_funcs.h"
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_include.h"
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_modules.h"
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_modules_content.h"
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_types.h" "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_types.h"
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_type_reg.h" "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_types_content.h"
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_ns_reg.h"
"${OPENCV_PYTHON_SIGNATURES_FILE}" "${OPENCV_PYTHON_SIGNATURES_FILE}"
) )
......
...@@ -24,6 +24,22 @@ if(TARGET gen_opencv_python_source) ...@@ -24,6 +24,22 @@ if(TARGET gen_opencv_python_source)
add_dependencies(${the_module} gen_opencv_python_source) add_dependencies(${the_module} gen_opencv_python_source)
endif() endif()
ocv_assert(${PYTHON}_VERSION_MAJOR)
ocv_assert(${PYTHON}_VERSION_MINOR)
if(${PYTHON}_LIMITED_API)
# support only python3.3+
ocv_assert(${PYTHON}_VERSION_MAJOR EQUAL 3 AND ${PYTHON}_VERSION_MINOR GREATER 2)
target_compile_definitions(${the_module} PRIVATE CVPY_DYNAMIC_INIT)
if(WIN32)
string(REPLACE
"python${${PYTHON}_VERSION_MAJOR}${${PYTHON}_VERSION_MINOR}.lib"
"python${${PYTHON}_VERSION_MAJOR}.lib"
${PYTHON}_LIBRARIES
"${${PYTHON}_LIBRARIES}")
endif()
endif()
if(APPLE) if(APPLE)
set_target_properties(${the_module} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup") set_target_properties(${the_module} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
elseif(WIN32 OR OPENCV_FORCE_PYTHON_LIBS) elseif(WIN32 OR OPENCV_FORCE_PYTHON_LIBS)
...@@ -54,6 +70,13 @@ else() ...@@ -54,6 +70,13 @@ else()
if(NOT PYTHON_CVPY_PROCESS EQUAL 0) if(NOT PYTHON_CVPY_PROCESS EQUAL 0)
set(CVPY_SUFFIX ".so") set(CVPY_SUFFIX ".so")
endif() endif()
if(${PYTHON}_LIMITED_API)
if(WIN32)
string(REGEX REPLACE "\\.[^\\.]*\\." "." CVPY_SUFFIX "${CVPY_SUFFIX}")
else()
string(REGEX REPLACE "\\.[^\\.]*\\." ".abi${${PYTHON}_VERSION_MAJOR}." CVPY_SUFFIX "${CVPY_SUFFIX}")
endif()
endif()
endif() endif()
ocv_update(OPENCV_PYTHON_EXTENSION_BUILD_PATH "${LIBRARY_OUTPUT_PATH}/${MODULE_INSTALL_SUBDIR}") ocv_update(OPENCV_PYTHON_EXTENSION_BUILD_PATH "${LIBRARY_OUTPUT_PATH}/${MODULE_INSTALL_SUBDIR}")
...@@ -111,9 +134,6 @@ else() ...@@ -111,9 +134,6 @@ else()
set(PYTHON_INSTALL_ARCHIVE ARCHIVE DESTINATION ${${PYTHON}_PACKAGES_PATH} COMPONENT python) set(PYTHON_INSTALL_ARCHIVE ARCHIVE DESTINATION ${${PYTHON}_PACKAGES_PATH} COMPONENT python)
endif() endif()
ocv_assert(${PYTHON}_VERSION_MAJOR)
ocv_assert(${PYTHON}_VERSION_MINOR)
set(__python_loader_subdir "") set(__python_loader_subdir "")
if(NOT OPENCV_SKIP_PYTHON_LOADER) if(NOT OPENCV_SKIP_PYTHON_LOADER)
set(__python_loader_subdir "cv2/") set(__python_loader_subdir "cv2/")
......
...@@ -2,6 +2,14 @@ if(NOT PYTHON3_INCLUDE_PATH OR NOT PYTHON3_NUMPY_INCLUDE_DIRS) ...@@ -2,6 +2,14 @@ if(NOT PYTHON3_INCLUDE_PATH OR NOT PYTHON3_NUMPY_INCLUDE_DIRS)
ocv_module_disable(python3) ocv_module_disable(python3)
endif() endif()
# Problem in numpy >=1.15 <1.17
if(PYTHON3_LIMITED_API
AND NOT PYTHON3_NUMPY_VERSION VERSION_LESS "1.15"
AND PYTHON3_NUMPY_VERSION VERSION_LESS "1.17"
)
set(PYTHON3_LIMITED_API OFF)
endif()
set(the_description "The python3 bindings") set(the_description "The python3 bindings")
set(MODULE_NAME python3) set(MODULE_NAME python3)
set(MODULE_INSTALL_SUBDIR python3) set(MODULE_INSTALL_SUBDIR python3)
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment