Commit b56933d9 authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

Merge pull request #3131 from znah:python_namespaces

parents 1efc3cff c23d6b67
......@@ -14,16 +14,10 @@ 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_face$")
ocv_list_filterout(candidate_deps "^opencv_matlab$")
ocv_list_filterout(candidate_deps "^opencv_tracking$")
ocv_list_filterout(candidate_deps "^opencv_optflow$")
ocv_list_filterout(candidate_deps "^opencv_bgsegm$")
ocv_list_filterout(candidate_deps "^opencv_xfeatures2d$")
ocv_list_filterout(candidate_deps "^opencv_ximgproc$")
ocv_list_filterout(candidate_deps "^opencv_xphoto$")
ocv_list_filterout(candidate_deps "^opencv_ts$")
ocv_list_filterout(candidate_deps "^opencv_adas$")
ocv_list_filterout(candidate_deps "^opencv_tracking$")
ocv_add_module(${MODULE_NAME} BINDINGS OPTIONAL ${candidate_deps})
......@@ -43,15 +37,14 @@ ocv_list_filterout(opencv_hdrs ".h$")
ocv_list_filterout(opencv_hdrs "cuda")
ocv_list_filterout(opencv_hdrs "cudev")
ocv_list_filterout(opencv_hdrs "opencv2/objdetect/detection_based_tracker.hpp")
ocv_list_filterout(opencv_hdrs "opencv2/optim.hpp")
ocv_list_filterout(opencv_hdrs "opencv2/ximgproc/structured_edge_detection.hpp")
set(cv2_generated_hdrs
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_include.h"
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_funcs.h"
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_func_tab.h"
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_types.h"
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_type_reg.h"
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_const_reg.h")
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_ns_reg.h")
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/headers.txt" "${opencv_hdrs}")
add_custom_command(
......
......@@ -83,8 +83,6 @@ catch (const cv::Exception &e) \
}
using namespace cv;
using cv::flann::IndexParams;
using cv::flann::SearchParams;
typedef std::vector<uchar> vector_uchar;
typedef std::vector<char> vector_char;
......@@ -1194,9 +1192,7 @@ static int convert_to_char(PyObject *o, char *dst, const char *name = "no_name")
#include "pyopencv_generated_types.h"
#include "pyopencv_generated_funcs.h"
static PyMethodDef methods[] = {
#include "pyopencv_generated_func_tab.h"
static PyMethodDef special_methods[] = {
{"createTrackbar", pycvCreateTrackbar, METH_VARARGS, "createTrackbar(trackbarName, windowName, value, count, onChange) -> None"},
{"setMouseCallback", (PyCFunction)pycvSetMouseCallback, METH_VARARGS | METH_KEYWORDS, "setMouseCallback(windowName, onMouse [, param]) -> None"},
{NULL, NULL},
......@@ -1205,6 +1201,53 @@ static PyMethodDef methods[] = {
/************************************************************************/
/* Module init */
struct ConstDef
{
const char * name;
long val;
};
static void init_submodule(PyObject * root, const char * name, PyMethodDef * methods, ConstDef * consts)
{
// traverse and create nested submodules
std::string s = name;
size_t i = s.find('.');
while (i < s.length() && i != std::string::npos)
{
size_t j = s.find('.', i);
if (j == std::string::npos)
j = s.length();
std::string short_name = s.substr(i, j-i);
std::string full_name = s.substr(0, j);
i = j+1;
PyObject * d = PyModule_GetDict(root);
PyObject * submod = PyDict_GetItemString(d, short_name.c_str());
if (submod == NULL)
{
submod = PyImport_AddModule(full_name.c_str());
PyDict_SetItemString(d, short_name.c_str(), submod);
}
root = submod;
}
// populate module's dict
PyObject * d = PyModule_GetDict(root);
for (PyMethodDef * m = methods; m->ml_name != NULL; ++m)
{
PyObject * method_obj = PyCFunction_NewEx(m, NULL, NULL);
PyDict_SetItemString(d, m->ml_name, method_obj);
Py_DECREF(method_obj);
}
for (ConstDef * c = consts; c->name != NULL; ++c)
{
PyDict_SetItemString(d, c->name, PyInt_FromLong(c->val));
}
}
#include "pyopencv_generated_ns_reg.h"
static int to_ok(PyTypeObject *to)
{
to->tp_alloc = PyType_GenericAlloc;
......@@ -1223,7 +1266,7 @@ static struct PyModuleDef cv2_moduledef =
"Python wrapper for OpenCV.",
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
methods
special_methods
};
PyObject* PyInit_cv2()
......@@ -1240,8 +1283,10 @@ void initcv2()
#if PY_MAJOR_VERSION >= 3
PyObject* m = PyModule_Create(&cv2_moduledef);
#else
PyObject* m = Py_InitModule(MODULESTR, methods);
PyObject* m = Py_InitModule(MODULESTR, special_methods);
#endif
init_submodules(m); // from "pyopencv_generated_ns_reg.h"
PyObject* d = PyModule_GetDict(m);
PyDict_SetItemString(d, "__version__", PyString_FromString(CV_VERSION));
......@@ -1289,7 +1334,6 @@ void initcv2()
PUBLISH(CV_64FC3);
PUBLISH(CV_64FC4);
#include "pyopencv_generated_const_reg.h"
#if PY_MAJOR_VERSION >= 3
return m;
#endif
......
This diff is collapsed.
......@@ -38,6 +38,8 @@ class CppHeaderParser(object):
self.PUBLIC_SECTION = 3
self.CLASS_DECL = 4
self.namespaces = set()
def batch_replace(self, s, pairs):
for before, after in pairs:
s = s.replace(before, after)
......@@ -833,6 +835,9 @@ class CppHeaderParser(object):
decls.append(d)
else:
decls.append(decl)
if stmt_type == "namespace":
chunks = [block[1] for block in self.block_stack if block[0] == 'namespace'] + [name]
self.namespaces.add('.'.join(chunks))
else:
stmt_type, name, parse_flag = "block", "", False
......@@ -877,3 +882,4 @@ if __name__ == '__main__':
#decls += parser.parse(hname, wmode=False)
parser.print_decls(decls)
print(len(decls))
print("namespaces:", " ".join(sorted(parser.namespaces)))
......@@ -4,7 +4,7 @@
Feature-based image matching sample.
USAGE
find_obj.py [--feature=<sift|surf|orb|brisk>[-flann]] [ <image1> <image2> ]
find_obj.py [--feature=<sift|surf|orb|akaze|brisk>[-flann]] [ <image1> <image2> ]
--feature - Feature to use. Can be sift, surf, orb or brisk. Append '-flann'
to feature name to use Flann-based matcher instead bruteforce.
......@@ -23,14 +23,17 @@ FLANN_INDEX_LSH = 6
def init_feature(name):
chunks = name.split('-')
if chunks[0] == 'sift':
detector = cv2.SIFT()
detector = cv2.xfeatures2d.SIFT()
norm = cv2.NORM_L2
elif chunks[0] == 'surf':
detector = cv2.SURF(800)
detector = cv2.xfeatures2d.SURF(800)
norm = cv2.NORM_L2
elif chunks[0] == 'orb':
detector = cv2.ORB(400)
norm = cv2.NORM_HAMMING
elif chunks[0] == 'akaze':
detector = cv2.AKAZE()
norm = cv2.NORM_HAMMING
elif chunks[0] == 'brisk':
detector = cv2.BRISK()
norm = cv2.NORM_HAMMING
......@@ -136,8 +139,8 @@ if __name__ == '__main__':
try:
fn1, fn2 = args
except:
fn1 = '../c/box.png'
fn2 = '../c/box_in_scene.png'
fn1 = '../cpp/box.png'
fn2 = '../cpp/box_in_scene.png'
img1 = cv2.imread(fn1, 0)
img2 = cv2.imread(fn2, 0)
......
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