2017-09-12 10 views
5

C++プログラムに埋め込まれたPython 3.5インタープリタを使用して、訓練されたテンソルフローモデルの入力として使用します。まず、イメージをnumpy配列に変換し、それをpythonに送ります。PythonコードをC++で埋め込んでいます - Pythonライブラリをインポートするときにエラーが発生しました

Pythonコード:上記の関数を呼び出します

def multiply_fun(M): 
    V = M*2 
    print(V) 

私のC++コード:しかし

#include <Python.h> 
#include <abstract.h> 
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION 
#include <ndarrayobject.h> 
#include <vector> 


int main() 
{ 
    Py_InitializeEx(1); 

    PyObject* sysPath = PySys_GetObject((char*)"path"); 
    PyObject* curDir = PyUnicode_FromString("."); 
    PyList_Append(sysPath, curDir); 
    Py_DECREF(curDir); 

    PyObject* python_code = PyImport_ImportModule("python_code"); 
    PyObject* multiply_fun = PyObject_GetAttrString(python_code, "multiply_fun"); 
    Py_XDECREF(python_code); 

    import_array1(-1); 
    npy_intp dim[] = { 5, 5 }; 
    std::vector<double> buffer(5*5, 1); 

    PyObject* array_2d = PyArray_SimpleNewFromData(2, dim, NPY_DOUBLE, &buffer[0]); 
    PyObject* return_value1 = PyObject_CallFunction(multiply_fun, "O", array_2d); 

    Py_XDECREF(return_value1); 
    Py_XDECREF(array_2d); 
    Py_XDECREF(multiply_fun); 

    Py_Finalize(); 
    return 0; 
} 

、私が欲しい罰金(hereから採用さコード)を動作しますこれは私の簡素化コードほとんどのPythonライブラリから使用するには、エラーが発生しました。たとえば、このPythonコードのために:

def multiply_fun(M): 
    from skimage.io import imsave 
    imsave('test.png', M) 

私は、このエラーました:ところで

Exception ignored in: <module 'threading' from 'C:\\Users\\Matin\\Anaconda3\\Lib\\threading.py'> 
Traceback (most recent call last): 
    File "C:\Users\Matin\Anaconda3\Lib\threading.py", line 1283, in _shutdown 
    assert tlock.locked() 
SystemError: <built-in method locked of _thread.lock object at 0x0000000002AF4418> returned a result with an error set 

を、This related discussionは私を助けることができませんでした。

ありがとうございました。

EDIT 1: Python関数(@moooeeeepはコメントで示唆したように)私は、この行にNULLだの外側にfrom skimage.io import imsaveを移動させることにより:

PyObject* python_code = PyImport_ImportModule("python_code"); 
+1

関連:https://stackoverflow.com/q/1188640/1025391 – moooeeeep

+0

@moooeeeep感謝を。私の編集したポストを見てください。 – Matin

答えて

0

問題がPyImport_ImportModuleは、サブモジュールをロードできないということであるようですいくつかのパッケージのうちfrom package.submodule import functionを使用するとそれはPython/C API Reference Manualで説明されています

When the name argument contains a dot (when it specifies a submodule of a package), the fromlist argument is set to the list ['*'] so that the return value is the named module rather than the top-level package containing it as would otherwise be the case. (Unfortunately, this has an additional side effect when name in fact specifies a subpackage instead of a submodule: the submodules specified in the package’s all variable are loaded.) Return a new reference to the imported module, or NULL with an exception set on failure. A failing import of a module doesn’t leave the module in sys.modules.

This function always uses absolute imports.

+0

テンソルフローをインポートするときに新しいエラーが発生しました。 [this](https://stackoverflow.com/questions/46190234/error-when-importing-tensorflow-in-embedded-python-in-c)が私の新しい問題です。 – Matin

関連する問題