我在C++中嵌入了一个python,其中C++调用python中的函数,并且应该返回一个值。下面是我的python代码:
def predict(window, g_slope, g_compliance):
ps = model.predict_on_batch(
x=np.asarray([window])
)
p_slopes = ps[0]
p_compliances = ps[1]
p_slopes = np.reshape(p_slopes, (np.shape(p_slopes)[1],))
p_compliances = np.reshape(p_compliances, (np.shape(p_compliances)[1],))
p_slope = p_slopes[-1]
p_compliance = p_compliances[-1]
n_slope = standardize_data(g_slope, means['GROUND_SLOPE'], variances['GROUND_SLOPE'])
n_compliance = standardize_data(g_compliance, means['GROUND_STIFFNESS'], variances['GROUND_STIFFNESS'])
#print('p_compliance: {0}, n_compliance: {1}, p_slope: {2}, n_slope: {3} '.format(str(p_compliance), str(n_compliance), str(n_slope), str(p_slope)))
return(p_slope, n_slope, p_compliance, n_compliance)它返回我希望在C++程序中接收的元组。我的C++程序像这样调用函数:
void ContactLearningApp::PythonWorkerThread() {
bp::object f = m_interface.attr("predict_on_data");
printf("Start Python thread. \n");
while (true) {
//printf("Inside while loop and waiting. \n");
std::unique_lock<std::mutex> ul(m_mutex);
while (m_data.size() <= 0) {
m_NotEmptyCV.wait(ul);
}
Data dat = m_data.back();
m_data.pop_back();
ul.unlock();
m_pydata_ptr py_ptr(new PyData);
py_ptr->InitWithData(dat);
try {
bp::tuple results = bp::extract<bp::tuple>(f(bp::ptr(py_ptr.get())));
printf("p_slope: %f, n_slope: %f, p_compliance: %f, n_compliance %f \n", results[0], results[1], results[2], results[3]);
}
catch (const bp::error_already_set &import) {
// Parse and output the exception
std::string perror_str = parse_python_exception();
std::cout << "Error in Python: " << perror_str << std::endl;
}
}
}我试图提取从Python返回的元组,但得到以下错误消息:
Expecting an object of type tuple; got an object of type NoneType instead. 我知道它不是none,因为我能够正确地访问元组中的每个值。提取元组的正确方法是什么?
发布于 2017-05-04 19:58:16
boost::python::object支持operator[],允许按索引访问序列(列表、元组等)元素。这意味着您不需要将函数返回的object转换为tuple,您可以直接访问它的元素:
bp::object func = bp::import("my_module").attr("my_func");
bp::object foo = func();
bp::object bar = foo[0];如果返回的python对象不支持索引访问,则会抛出异常。请注意,您可能仍然需要将由索引提取的序列元素转换为各自的C++类型。
通常,您可以“按原样”将参数从C++传递给function对象,而不会干扰原始的PyObject*指针。在大多数情况下,Boost.Python都会进行必要的转换。
https://stackoverflow.com/questions/43774207
复制相似问题