c++ 调用python

#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’)”);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值