File: pyopencv_flann.hpp

package info (click to toggle)
opencv 4.5.1%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 268,248 kB
  • sloc: cpp: 969,170; xml: 682,525; python: 36,732; lisp: 30,170; java: 25,155; ansic: 7,927; javascript: 5,643; objc: 2,041; sh: 935; cs: 601; perl: 494; makefile: 145
file content (87 lines) | stat: -rw-r--r-- 2,210 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifdef HAVE_OPENCV_FLANN
typedef cvflann::flann_distance_t cvflann_flann_distance_t;
typedef cvflann::flann_algorithm_t cvflann_flann_algorithm_t;

template<>
PyObject* pyopencv_from(const cvflann_flann_algorithm_t& value)
{
    return PyInt_FromLong(int(value));
}

template<>
PyObject* pyopencv_from(const cvflann_flann_distance_t& value)
{
    return PyInt_FromLong(int(value));
}

template<>
bool pyopencv_to(PyObject *o, cv::flann::IndexParams& p, const ArgInfo& info)
{
    CV_UNUSED(info);
    bool ok = true;
    PyObject* key = NULL;
    PyObject* item = NULL;
    Py_ssize_t pos = 0;

    if (!o || o == Py_None)
        return true;

    if(PyDict_Check(o)) {
        while(PyDict_Next(o, &pos, &key, &item))
        {
            // get key
            std::string k;
            if (!getUnicodeString(key, k))
            {
                ok = false;
                break;
            }
            // get value
            if( !!PyBool_Check(item) )
            {
                p.setBool(k, item == Py_True);
            }
            else if( PyInt_Check(item) )
            {
                int value = (int)PyInt_AsLong(item);
                if( strcmp(k.c_str(), "algorithm") == 0 )
                    p.setAlgorithm(value);
                else
                    p.setInt(k, value);
            }
            else if( PyFloat_Check(item) )
            {
                double value = PyFloat_AsDouble(item);
                p.setDouble(k, value);
            }
            else
            {
                std::string val_str;
                if (!getUnicodeString(item, val_str))
                {
                    ok = false;
                    break;
                }
                p.setString(k, val_str);
            }
        }
    }

    return ok && !PyErr_Occurred();
}

template<>
bool pyopencv_to(PyObject* obj, cv::flann::SearchParams & value, const ArgInfo& info)
{
    return pyopencv_to<cv::flann::IndexParams>(obj, value, info);
}

template<>
bool pyopencv_to(PyObject *o, cvflann::flann_distance_t& dist, const ArgInfo& info)
{
    int d = (int)dist;
    bool ok = pyopencv_to(o, d, info);
    dist = (cvflann::flann_distance_t)d;
    return ok;
}
#endif