我正在尝试在C ++中包装Python函数,为此,我需要将std :: vector转换为NumPy数组。到目前为止,我已经按照here给出的解决方案进行了修改:
PyArrayObject* mPythonScriptWrapper::VecToNumPy(std::vector<T>* vector){
size_t nRows = vector->size();
//size_t nCols = vec[0].size();
npy_intp dims[2] = {nRows, 1};
PyArray_Descr* descr;
PyObject* op;
op = Py_BuildValue("[(s, s), (s, s), (s, s), (s, s)]", "Lane", "i4", "Velocity", "f4", "Acceleration", "f4", "Distance", "f4");
PyArray_DescrConverter(op, &descr);
Py_DECREF(op);
return (PyArrayObject*) PyArray_NewFromDescr(&PyArray_Type, descr, vector->size(), dims, nullptr, vector->data(), 0, nullptr);
调试时,我发现在执行PyArray_DescrConverter之后,descr保留在nullptr上。这与提供无效的PyObject进行转换有关吗?我要包装的结构包含一个int32和3个float32,这将导致Converter的参数i4和3x f4。
期待您的帮助!谢谢!