0
我有一个python嵌入在C++中,C++调用python中的函数,应该返回一个值。这里是我的Python代码:Boost Python Python返回值到C++程序
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.
我知道这是不是没有,因为我能够正确地访问每个值的元组。什么是提取元组的正确方法是什么?
我试图从对象中提取这样的值:'bp :: extract(results [0])'但它给了我一个'NoneType对象不是可下载的错误。 –
terminix00
以上忽略。但是,我想从这样的对象中提取值:'BP ::提取(结果[0])'但它给我的一个0.00000值,这是出乎我的Python脚本是什么打印。 –
terminix00