python - C++ to numpy and back to C++, tuple PyObject -
i implementing visual c++ code (ms vstudio 2013) acquires image camera, processes image, sends pre-processed data (an array 2 or more doubles) python through pyobject, , gets numpy-processed results python (a tuple of 3 doubles in py, becomes array of 3 doubles in c++). tested py function standalone script. however, keep getting null return pyobject_callobject(). python , vc++ project reside in same directory.
i have set log file record in py function document calls c, know in cases c++ calls not reach py function, , in other cases py function called, but, perhaps, returned values cannot processed. specifically, found can call py function (positive log record) when tuple contains 1 object, cannot call (no log file records) when tuple contains 2 or more objects. in addition, if py function needs no arguments, , not need return anything, calls successful.
i have googled , read having using tuples communicate between c++ , py, still did not make work. have read documentation , examples, still no luck.
i have stripped code bare minimum below , reproduced problem. below example when pass 2 zeros input, , expect 3 zeros back. grateful leads on this...
//c++ part py_initialize(); pyname = pyimport_importmodulenoblock(_t("find_center_def")); if (pyname == null) { return false; } pyfunc = pyobject_getattrstring(pyname, _t("find_center")); if (pyfunc == null) { return false; } py_decref(pyname); if (pycallable_check(pyfunc) == 0) { return false; } pyobject * arg = pytuple_new(2); pyobject * datum0 = pyfloat_fromdouble(0.0); pyobject * datum1 = pyfloat_fromdouble(0.0); if (pytuple_setitem(arg, 0, datum0) != 0) { return false; } if (pytuple_setitem(arg, 1, datum1) != 0) { return false; } pyobject * result = pyobject_callobject(pyfunc, arg); if (result == null) { return false; } outputdebugstring(_t("\nsuccess calling find_center function!")); #python part, inside of script called find_center_def.py def find_center(args): import numpy fo=open('foo.txt' ,'w') fo.write('find_center called c++!') fo.close() return (0,0,0) #python output: python 3.4.3 (v3.4.3:9b73f1c3e601, feb 24 2015, 22:44:40) [msc v.1600 64 bit (amd64)] on win32 >>> import importlib >>> test = importlib.import_module("find_center_def") >>> test.find_center((0,0)) (0, 0, 0) >>>
you're able add single value tuple because
pyobject* pyobject_callobject(pyobject *callable_object, pyobject *args) call callable python object callable_object, with arguments given tuple args
(emphasis mine). in other words, second argument pyobject_callobject
tuple of arguments. function takes single argument, length of tuple has 1.
since you're trying pass tuple 1 argument, need two-element tuple first element of single-element tuple pass second argument pyobject_callobject
.
another option use pyobject_callfunction
instead, , let build argument tuple.
Comments
Post a Comment