#include “include\Python.h”
#include “C:\Users\ly\AppData\Local\Programs\Python\Python36\Lib\site-packages\numpy\core\include\numpy\arrayobject.h”
/ c++ 调用python mtcnn版本
void python_mtcnn_tensorflow()
{
//Py_Initialize();
//import_array();
//PyRun_SimpleString(“import sys”);
// 导入脚本路径
//PyRun_SimpleString(“sys.path.append(‘D:/algorithm/MTCNN-Tensorflow-master/test’)”);
PyObject* pMtcnnModule = NULL;
PyObject* pFunc1 = NULL;
PyObject* pFunc2 = NULL;
PyObject* pMtcnn = NULL;
PyObject* result = NULL;
const char *image_path = "D:\\algorithm\\MTCNN-Tensorflow-master\\test\\lala\\00001.jpg";
// 导入模块
PyObject* pModule = PyImport_ImportModule("hello_word");
//获取模块中的函数
pFunc1 = PyObject_GetAttrString(pModule, "Init_start");
// 传入一张图片
Mat img = imread(image_path);
int m, n;
n = img.cols * 3;
m = img.rows;
unsigned char *data = (unsigned char*)malloc(sizeof(unsigned char) * m * n);
int p = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
data[p] = img.at<unsigned char>(i, j);
p++;
}
}
npy_intp Dims[2] = { m, n }; //给定维度信息
PyObject* PyArray = PyArray_SimpleNewFromData(2, Dims, NPY_UBYTE, data);
PyObject* ArgArray = PyTuple_New(1);
PyTuple_SetItem(ArgArray, 0, PyArray);
PyObject* pFuncFive = PyObject_GetAttrString(pModule, "load_image");
PyObject_CallObject(pFuncFive, ArgArray);
//初始化调用对象
pMtcnn = PyObject_CallObject(pFunc1, NULL);
if (pMtcnn == NULL)
{
cout << " not success " << endl;
}
else
{
cout << "success" << endl;
}
pFunc2 = PyObject_GetAttrString(pModule, "Show_Image");
if (pFunc2 == NULL)
{
cout << "pFunc2 not success" << endl;
}
PyObject *args = PyTuple_New(1);
PyTuple_SetItem(args, 0, Py_BuildValue("s", image_path));
result = PyEval_CallObject(pFunc2, args);
//PyObject* args = PyTuple_New(1);
//PyTuple_SetItem(args,0,)
//result = PyObject_CallFunction(pFunc2, "s",Py_BuildValue("s", image_path.c_str()));
//result = PyObject_CallObject(pFunc2,pMtcnn,);
if (result == NULL)
{
cout << "tran not success" << endl;
}
if (PyList_Check(result))
{
cout << "is list" << endl;
}
int SizeOfList = PyList_Size(result);
cout << SizeOfList << endl;
for (int i = 0; i < SizeOfList; i++)
{
PyObject *ListItem = PyList_GetItem(result, i);
int NumOfItem = PyList_Size(ListItem);
int point_x1;
float score;
vector<int> tmp_vec;
for (int j = 0; j < NumOfItem; j++)
{
PyObject *InListItem = PyList_GetItem(ListItem, j);
if (j < NumOfItem - 1)
{
point_x1 = int(PyFloat_AsDouble(InListItem));
tmp_vec.push_back(point_x1);
//cout << PyFloat_AsDouble(InListItem)<<" ";
}
else
{
score = float(PyFloat_AsDouble(InListItem));
tmp_vec.push_back(score);
}
}
Point pt1, pt2;
pt1.x = tmp_vec[0];
pt1.y = tmp_vec[1];
pt2.x = tmp_vec[2];
pt2.y = tmp_vec[3];
rectangle(img, pt1, pt2, Scalar(255, 0, 0), 1);
//cout << endl;
//break;
//cout << NumOfItem << endl;
}
imshow("123", img);
waitKey(0);
}/ c++ 调用python mtcnn版本
void python_mtcnn_tensorflow()
{
//Py_Initialize();
//import_array();
//PyRun_SimpleString(“import sys”);
// 导入脚本路径
//PyRun_SimpleString(“sys.path.append(‘D:/algorithm/MTCNN-Tensorflow-master/test’)”);
PyObject* pMtcnnModule = NULL;
PyObject* pFunc1 = NULL;
PyObject* pFunc2 = NULL;
PyObject* pMtcnn = NULL;
PyObject* result = NULL;
const char *image_path = "D:\\algorithm\\MTCNN-Tensorflow-master\\test\\lala\\00001.jpg";
// 导入模块
PyObject* pModule = PyImport_ImportModule("hello_word");
//获取模块中的函数
pFunc1 = PyObject_GetAttrString(pModule, "Init_start");
// 传入一张图片
Mat img = imread(image_path);
int m, n;
n = img.cols * 3;
m = img.rows;
unsigned char *data = (unsigned char*)malloc(sizeof(unsigned char) * m * n);
int p = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
data[p] = img.at<unsigned char>(i, j);
p++;
}
}
npy_intp Dims[2] = { m, n }; //给定维度信息
PyObject* PyArray = PyArray_SimpleNewFromData(2, Dims, NPY_UBYTE, data);
PyObject* ArgArray = PyTuple_New(1);
PyTuple_SetItem(ArgArray, 0, PyArray);
PyObject* pFuncFive = PyObject_GetAttrString(pModule, "load_image");
PyObject_CallObject(pFuncFive, ArgArray);
//初始化调用对象
pMtcnn = PyObject_CallObject(pFunc1, NULL);
if (pMtcnn == NULL)
{
cout << " not success " << endl;
}
else
{
cout << "success" << endl;
}
pFunc2 = PyObject_GetAttrString(pModule, "Show_Image");
if (pFunc2 == NULL)
{
cout << "pFunc2 not success" << endl;
}
PyObject *args = PyTuple_New(1);
PyTuple_SetItem(args, 0, Py_BuildValue("s", image_path));
result = PyEval_CallObject(pFunc2, args);
//PyObject* args = PyTuple_New(1);
//PyTuple_SetItem(args,0,)
//result = PyObject_CallFunction(pFunc2, "s",Py_BuildValue("s", image_path.c_str()));
//result = PyObject_CallObject(pFunc2,pMtcnn,);
if (result == NULL)
{
cout << "tran not success" << endl;
}
if (PyList_Check(result))
{
cout << "is list" << endl;
}
int SizeOfList = PyList_Size(result);
cout << SizeOfList << endl;
for (int i = 0; i < SizeOfList; i++)
{
PyObject *ListItem = PyList_GetItem(result, i);
int NumOfItem = PyList_Size(ListItem);
int point_x1;
float score;
vector<int> tmp_vec;
for (int j = 0; j < NumOfItem; j++)
{
PyObject *InListItem = PyList_GetItem(ListItem, j);
if (j < NumOfItem - 1)
{
point_x1 = int(PyFloat_AsDouble(InListItem));
tmp_vec.push_back(point_x1);
//cout << PyFloat_AsDouble(InListItem)<<" ";
}
else
{
score = float(PyFloat_AsDouble(InListItem));
tmp_vec.push_back(score);
}
}
Point pt1, pt2;
pt1.x = tmp_vec[0];
pt1.y = tmp_vec[1];
pt2.x = tmp_vec[2];
pt2.y = tmp_vec[3];
rectangle(img, pt1, pt2, Scalar(255, 0, 0), 1);
//cout << endl;
//break;
//cout << NumOfItem << endl;
}
imshow("123", img);
waitKey(0);
}
int main()
{
Py_Initialize();
import_array();
PyRun_SimpleString(“import sys”);
// 导入脚本路径
PyRun_SimpleString(“sys.path.append(‘D:/algorithm/MTCNN-Tensorflow-master/test’)”);
}