X-Git-Url: http://47.100.26.94:8080/?a=blobdiff_plain;f=src%2FPredictorWrapper.cpp;h=ebe75798ccd3566f0b9a66cdb2c8901ea9f1258b;hb=70532232dd98f31467eff7baaaff6e68f803bb45;hp=20fefc3e6326ec2737d539fd2d3dc0528ea18c10;hpb=3aa517d206c44156fe86697aeadc5f75ea212329;p=trackerpp.git diff --git a/src/PredictorWrapper.cpp b/src/PredictorWrapper.cpp index 20fefc3..ebe7579 100644 --- a/src/PredictorWrapper.cpp +++ b/src/PredictorWrapper.cpp @@ -12,27 +12,26 @@ using namespace suanzi; PredictorWrapperWPtr PredictorWrapper::instance; -const static std::string PREDICTOR_PY_DIR = "./python"; const static std::string TAG = "PredictorWrapper"; static std::string parse_python_exception(); template -boost::python::list toPythonList(std::vector vector) { +boost::python::list toPythonList(const std::vector& v) { typename std::vector::iterator iter; boost::python::list list; - for (iter = vector.begin(); iter != vector.end(); ++iter) { - list.append(*iter); + for(const auto& vv : v){ + list.append(vv); } return list; } -PredictorWrapperPtr PredictorWrapper::create(const std::string& fname) +PredictorWrapperPtr PredictorWrapper::create(const std::string& python_dir, const std::string& model_path) { if (instance.lock()){ return PredictorWrapperPtr(); } - PredictorWrapperPtr ins (new PredictorWrapper(fname)); + PredictorWrapperPtr ins (new PredictorWrapper(python_dir, model_path)); instance = ins; return ins; } @@ -51,26 +50,29 @@ void PredictorWrapper::dump() LOG_DEBUG(TAG, ss); } -double PredictorWrapper::predict(int index, std::vector ff) +double PredictorWrapper::predict(int index, const std::vector& ff) { LOG_DEBUG(TAG, "predict"); + py::object ret; try{ - this->predict_func(index, toPythonList(ff)); + ret = this->predict_func(index, toPythonList(ff)); } catch (boost::python::error_already_set const &){ std::string perror_str = parse_python_exception(); LOG_ERROR(TAG, "Error in Python: " + perror_str) } - return 0.1; + double rr = py::extract(ret); + LOG_DEBUG(TAG, "return: " + std::to_string(rr)); + return rr; } -PredictorWrapper::PredictorWrapper(const std::string& fname) +PredictorWrapper::PredictorWrapper(const std::string& py_dir, const std::string& model_path) { Py_Initialize(); try{ py::object main_module = py::import("__main__"); py::object main_namespace = main_module.attr("__dict__"); py::exec("import sys", main_namespace); - std::string cmd = "sys.path.insert(0, '" + PREDICTOR_PY_DIR + "')"; + std::string cmd = "sys.path.insert(0, '" + py_dir + "')"; py::exec(cmd.c_str(), main_namespace); py::exec("import signal", main_namespace); py::exec("signal.signal(signal.SIGINT, signal.SIG_DFL)", main_namespace); @@ -78,7 +80,7 @@ PredictorWrapper::PredictorWrapper(const std::string& fname) py::object predictor_init = predictor_mod.attr("init"); dump_func = predictor_mod.attr("dump"); predict_func = predictor_mod.attr("predict"); - predictor_init(fname.c_str()); + predictor_init(model_path.c_str()); } catch (boost::python::error_already_set const &){ std::string perror_str = parse_python_exception(); LOG_ERROR(TAG, "Error in Python: " + perror_str)