运行opencv-python脚本在子解释器中挂起

时间:2017-05-05 13:57:42

标签: python c++ opencv numpy

我在C应用程序中托管python解释器(2.7.13)并运行一些python脚本,该脚本对opencv-python函数进行了一些调用。

问题是当脚本在主解释器中运行时,它可以正常工作。但是,当脚本在子解释器中运行时,它会挂起。

opencv-python包和子解释器之间是否存在任何已知的不兼容性?

C应用程序代码如下: -

const char *pythonSource = "abc_module";
const char *funcName = "abc_function";

Py_Initialize();
PyEval_InitThreads();

if(run_in_subinterpreter){
    PyThreadState* threadState = Py_NewInterpreter();
    PyThreadState *mainState = PyThreadState_Swap(threadState);
}

pName = PyString_FromString(pythonSource);
pModule = PyImport_Import(pName);

pDict = PyModule_GetDict(pModule);
pFunc = PyDict_GetItemString(pDict, funcName);

PyObject *pyList = PyList_New(gArray.size());              //std::vector<int> gArray;
for (size_t i = 0; i < gArray.size(); ++i)
{
   PyObject *elem = PyInt_FromLong(gArray[i]);
   PyList_SetItem(pyList, i, elem);
}

PyObject* pArgs = PyTuple_New(1);
PyTuple_SetItem(pArgs, 0, pyList);

pValue = PyObject_CallObject(pFunc, pArgs);

Py_Finalize();
return;


python脚本代码如下: -

import sys
import cv2
import numpy as np

def abc_function(list):
    array = np.asarray(list)
    array = np.reshape(array, (100, 200, 4)).astype(np.uint8)
    array = abc_function_core(array)
    return array

def abc_function_core(array):
    # array is manipulated by calling the below functions
    cv2.resize(....)
    cv2.cvtColor(...)
    cv2.equalizeHist(...)
    cv2.CascadeClassifier.detectMultiScale(...)
    cv2.rectangle(...)
    return array

0 个答案:

没有答案