PyEval_CallObject 和 PyObject_CallObject return 空对象

PyEval_CallObject and PyObject_CallObject return a null object

这是关于 的后续问题。 正如前面的问题评论所指出的,我最初导入模块的尝试是完全错误的,相反我应该这样做:

auto module = PyImport_ImportModule("Package1.Module1");

所以我这样做了:

auto module = PyImport_ImportModule("FV.F_V");

这样做之后,现在我得到了更有意义的输出,如您所见:

module: 0000021306FF9B38
size: 30
FaceVerification: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
Image: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
Path: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
__builtins__: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
__cached__: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
__doc__: FV.F_V
__file__: C:\Users\Master\Anaconda3\Lib\site-packages\FV\F_V.py
__loader__: FV
__name__: FV.F_V
__package__: FV
__spec__: FV
__warningregistry__ : C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
align_face: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
cv2: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
data_transforms: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
get_central_face_attributes: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
math: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
nn: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
np: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
os: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
pickle: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
plt: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
resnet101: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
resnet18: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
resnet50: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
scipy: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
time: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
torch: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
tqdm: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
transforms : C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc

但是,每当我尝试使用模块中定义的 class(即 FaceVerification 并实例化它时,它都会失败。 class 似乎没问题,但是尝试实例化对象的调用 return 是一个空对象!: 我尝试了 PyEval_CallObjectPyObject_CallObject:

auto python_class = PyDict_GetItemString(pdict, "FaceVerification");

if (PyCallable_Check(python_class)) 
{
    object = PyEval_CallObject(python_class, args);
    # or 
    //object = PyObject_CallObject(python_class, args);
    //Py_DECREF(python_class);
}
else 
{
    cout << "Cannot instantiate the Python class" << endl;
    //Py_DECREF(python_class);
    //return 1;
}

无济于事。这很奇怪,因此我向 F_V.py 添加了一个新的 CONSTANT 字段并尝试查看我是否可以访问它(查看该模块是否确实有效)。它实际上成功地检索了常量。但是,我注意到,在 c++ 应用程序的 re-运行 上,现在当我打印保存 PyModule_GetDict(module) 输出的 map 时,我看到了不同的结果:

module: 000001F7E07E8C28
size: 31
CONSTANT: THIS IS A TEST. REMOVE IT
FaceVerification: THIS IS A TEST. REMOVE IT
Image: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
Path: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
__builtins__: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
__cached__: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
__doc__: FV.F_V
__file__: C:\Users\Master\Anaconda3\Lib\site-packages\FV\F_V.py
__loader__: FV
__name__: FV.F_V
__package__: FV
__spec__: FV
__warningregistry__: THIS IS A TEST. REMOVE IT
align_face: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
cv2: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
data_transforms: THIS IS A TEST. REMOVE IT
get_central_face_attributes: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
math: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
nn: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
np: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
os: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
pickle: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
plt: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
resnet101: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
resnet18: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
resnet50: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
scipy: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
time: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
torch: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
tqdm: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
transforms: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc

最值得注意的是 FaceVerification,它应该是 class,没有显示 CONSTANT 字段的值!

我做错了什么?

事实证明(非常感谢@Scheff),对象之所以是null,是因为构造函数内部发生了异常,因此导致对象为空. 异常的原因是格式错误的路径和有问题的(错误的)参数。
基本上我是这样设置参数的:

auto model_name = "r189";
auto model_checkpoint_path = PyUnicode_DecodeFSDefault("L:\test_procs\trained_models\BEST_checkpoint_r189.tar");
auto align_fn = Py_None;
auto img_bank_folder_root = PyUnicode_DecodeFSDefault("L:\test_procs\data\facebank_2");
auto cache_folder = Py_None;
auto postfix = Py_None;
auto rebuild_cache = Py_False;
auto use_jit = Py_False;
auto threshold = 65; 
auto device = "cpu";

因此论点是这样的:

auto args = Py_BuildValue("sOOOOONNiN", model_name, model_checkpoint_path, align_fn,
                                  img_bank_folder_root, cache_folder, postfix, 
                                  rebuild_cache, use_jit, threshold, device);

这是错误的。 我将它们更改为:

auto model_name = "r189";
auto model_checkpoint_path = "L:\test_procs\trained_models\BEST_checkpoint_r189.tar";
auto align_fn = "";
auto img_bank_folder_root = "L:\test_procs\data\facebank_2";
auto cache_folder = "";
auto postfix = "";
auto rebuild_cache = 0;
auto use_jit = 0;
auto threshold = 65; 
auto device = "cpu";

并像这样简化参数:

auto args = Py_BuildValue("ssssssiiis", model_name, model_checkpoint_path, align_fn,
                          img_bank_folder_root, cache_folder, postfix, 
                          rebuild_cache, use_jit, threshold, device);

完成所有这些后,我仍然遇到异常并发现: model_checkpoint_path 需要修复,因为它的路径中缺少一个 \

auto model_checkpoint_path = "L:\test_procs\trained_models\BEST_checkpoint_r189.tar";  

修复它:

auto model_checkpoint_path = "L:\test_procs\trained_models\BEST_checkpoint_r189.tar";

一切都解决了。