我正在尝试使用 Cython 将一些 C++ 类和函数包装到 Python 中。到目前为止,我已经包装了 2 个类,现在我想包装一个函数。
该函数的签名是
std::map<std::string, std::vector<PyObject*>> analyze(PyObject* img, LandmarkDetector::CLNF& clnf_model, LandmarkDetector::FaceModelParameters& params);
我已经成功地包装了CLNF
andFaceModelParameters
类,我在包装这个analyze
函数时遇到了麻烦。
该函数处理PyObject*
s 因为它处理 opencv,我希望能够在语言之间轻松传递它们。我正在使用这些函数来执行cv::Point
到 python 对象之间以及 python Mat 到cv::Mat
.
这是我的 pyx 文件:
from libcpp.vector cimport vector
from libcpp.map cimport map
from libcpp.string cimport string
from cpython.ref cimport PyObject
from cython.operator cimport dereference as deref
cdef extern from "LandmarkDetectorModel.h" namespace "LandmarkDetector":
cdef cppclass CLNF:
CLNF(string) except +
cdef extern from "LandmarkDetectorParameters.h" namespace "LandmarkDetector":
cdef cppclass FaceModelParameters:
FaceModelParameters(vector[string] &) except +
cdef class PyCLNF:
cdef CLNF *thisptr
def __cinit__(self, arg):
self.thisptr = new CLNF(<string> arg)
cdef class PyLandmarkDetectorParameters:
cdef FaceModelParameters *thisptr
def __cinit__(self, args):
self.thisptr = new FaceModelParameters(args)
cdef extern from "FaceLandmarkVid.h":
map[string, vector[object]] analyze(object, CLNF&, FaceModelParameters&)
cdef PyAnalyze(object img, PyCLNF clnf, PyLandmarkDetectorParameters facemodel):
return analyze(img, deref(clnf.thisptr), deref(facemodel.thisptr))
但是在尝试编译它时,我收到了错误消息
landmarks.pyx:26:23: Python object type 'Python object' cannot be used as a template argument
(指的是线map[string, vector[object]] analyze [...]
)