1.Init
由于存在numpy的数据结构,除了Py_Initialize(); 还需要import_array();
PyRun_SimpleString("import sys");
std::string chdir_cmd = std::string("sys.path.append(\"" + python_rely_path + "\")");
2.import module and function
pModule = PyImport_ImportModule(pypath.c_str());
pLoadFunc = PyObject_GetAttrString(pModule, "init_facenet");//init_model
3 pass Argc
pArgs_7_2 = PyTuple_New(7);
pFaceNeedSaveIntoLib = PyList_New(0);
PyList_Append(pFaceNeedSaveIntoLib, Py_BuildValue("s", sImgPath.c_str()));
PyTuple_SetItem(pArgs_7_2, 0, pFaceNeedSaveIntoLib);
PyTuple_SetItem(pArgs_7_2, 1, pnet);
PyTuple_SetItem(pArgs_7_2, 2, rnet);
PyTuple_SetItem(pArgs_7_2, 3, onet);
PyTuple_SetItem(pArgs_7_2, 4, net_sess);
PyTuple_SetItem(pArgs_7_2, 5, mtcnn_graph);
PyTuple_SetItem(pArgs_7_2, 6, facenet_graph);
4 call function
pAddReturn = PyEval_CallObject(pFunc, pArgs_7_2);
5 convert to arrary
pyAarryFaceslib = (PyArrayObject*)pReturn;
6 python arrary to Carrary
for (Index_m = 0; Index_m < Rows; Index_m++) {
for (Index_n = 0; Index_n < columns; Index_n++) {
CArrays[Index_m][Index_n] = *(float *)(((PyArrayObject*)pReturn)->data + Index_m *((PyArrayObject*)pReturn)->strides[0] + Index_n * ((PyArrayObject*)pReturn)->strides[1]);
//std::cout << CArrays[Index_m][Index_n] << std::endl;
}
}
7 Carrary to python arrary
pReturn = PyArray_SimpleNewFromData(2, Dims, NPY_DOUBLE, CArrays);
8. analyze
PyArg_ParseTuple(pLoadReturn, "O|O|O|O|O|O", &pnet, &rnet, &onet, &net_sess, &mtcnn_graph, &facenet_graph);
i = _PyLong_AsInt(pReturn_2);
8 others
npy_intp Dims[2];