2017-09-12 83 views
5

C++에서 임베디드 된 Python 3.5 인터프리터를 사용하여 C++에서 이미지를 받고 훈련 된 텐서 플로 모델의 입력으로 사용하려고합니다. 먼저 이미지를 numpy 배열로 변환 한 다음 python으로 보냅니다.파이썬 코드를 C++에 임베드 할 때 - 파이썬 라이브러리를 가져올 때 오류가 발생했습니다.

파이썬 코드 : 위의 함수를 호출

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에서 채택 된 코드를) 작동이 내 단순화 코드 대부분의 파이썬 라이브러리에서 사용하려면 오류가 발생했습니다. 예를 들어,이 파이썬 코드 :

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 나를 도울 수 없었다.

도움 주셔서 감사합니다.

편집 1 :이 줄에 널 가지고 (@moooeeeep는 의견 제안) 파이썬 함수의 외부에 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

tensorflow를 가져올 때 새로운 오류가 발생했습니다. [this] (https://stackoverflow.com/questions/46190234/error-when-importing-tensorflow-in-embedded-python-in-c) 내 새로운 문제입니다. – Matin