コード例 #1
0
ファイル: cv2.cpp プロジェクト: coapp-packages/opencv
static bool pyopencv_to(PyObject *o, cv::flann::IndexParams& p, const char *name="<unknown>")
{
    bool ok = false;
    PyObject* keys = PyMapping_Keys(o);
    PyObject* values = PyMapping_Values(o);
    
    if( keys && values )
    {
        int i, n = (int)PyList_GET_SIZE(keys);
        for( i = 0; i < n; i++ )
        {
            PyObject* key = PyList_GET_ITEM(keys, i);
            PyObject* item = PyList_GET_ITEM(values, i);
            if( !PyString_Check(key) )
                break;
            std::string k = PyString_AsString(key);
            if( PyString_Check(item) )
                p.setString(k, PyString_AsString(item));
            else if( PyInt_Check(item) )
                p.setInt(k, PyInt_AsLong(item));
            else if( PyFloat_Check(item) )
                p.setDouble(k, PyFloat_AsDouble(item));
            else
                break;
        }
        ok = i == n && !PyErr_Occurred();
    }
    
    Py_XDECREF(keys);
    Py_XDECREF(values);
    return ok;
}
コード例 #2
0
ファイル: cv2.cpp プロジェクト: FelixPe/opencv
static bool pyopencv_to(PyObject *o, cv::flann::IndexParams& p, const char *name="<unknown>")
{
    (void)name;
    bool ok = false;
    PyObject* keys = PyObject_CallMethod(o,(char*)"keys",0);
    PyObject* values = PyObject_CallMethod(o,(char*)"values",0);

    if( keys && values )
    {
        int i, n = (int)PyList_GET_SIZE(keys);
        for( i = 0; i < n; i++ )
        {
            PyObject* key = PyList_GET_ITEM(keys, i);
            PyObject* item = PyList_GET_ITEM(values, i);
            if( !PyString_Check(key) )
                break;
            std::string k = PyString_AsString(key);
            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);
            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
                break;
        }
        ok = i == n && !PyErr_Occurred();
    }

    Py_XDECREF(keys);
    Py_XDECREF(values);
    return ok;
}
コード例 #3
0
ファイル: cv2.cpp プロジェクト: 666prashanth/opencv
bool pyopencv_to(PyObject *o, cv::flann::IndexParams& p, const char *name)
{
    (void)name;
    bool ok = true;
    PyObject* key = NULL;
    PyObject* item = NULL;
    Py_ssize_t pos = 0;

    if(PyDict_Check(o)) {
        while(PyDict_Next(o, &pos, &key, &item)) {
            if( !PyString_Check(key) ) {
                ok = false;
                break;
            }

            String k = PyString_AsString(key);
            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);
            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
            {
                ok = false;
                break;
            }
        }
    }

    return ok && !PyErr_Occurred();
}