C++调用python训练的pytorch模型(二)-----C++调用python例子

8 篇文章 1 订阅
5 篇文章 0 订阅

文章目录


参考:1

参考:2

C++/Python如何在C++中使用一个Python类? (Use Python-defined class in C++)

python代码
# test_python.py
def hello(s):
    print("python: hello: %s " %s)

def add(a,b):
    print("python: a=%d,b=%d" %(a,b))
    return a+b

def add2(s1,s2):
    print("python: s1=%s,s2=%s" %(s1,s2))
    return s1+s2

class ctest: 
    def __init__(self): 
        print("python: test class nit") 
    def say_hello(): 
        print ("python: test class say hello")
    def say(self, name): 
        print ("python: test class hello: %s" % name) 
        return name
hello('zwh')
add(2,4)
add('aaa','bbb')
test = ctest()
test.say('zwt')
C++代码
#include <Python.h>
#include <iostream>
#include <string>
using namespace std;
void init_python_env(){
	Py_Initialize();
	if(!Py_IsInitialized()){
		cout << "initialize failed"<<endl;
	}
	PyRun_SimpleString("import sys");
	PyRun_SimpleString("sys.path.append('./')");
//	PyRun_SimpleString("sys.path.append('/home/bob/wkspace/git/detectron/tools/')");
}

void free_python_env(){
	Py_Finalize();
}
//调用输出"Hello Python"函数
int hello()
{
	PyObject * pModule = NULL;
	PyObject * pFunc = NULL;
	PyObject * pDict = NULL;
	//导入python文件模块
	pModule = PyImport_ImportModule("test_python");
	if(pModule == NULL){
		cout << "test_python pModule is null"<<endl;
		return -1;
	}
	//获取字典属性(可选)
	pDict = PyModule_GetDict(pModule);
	if(pDict == NULL){
		cout << "test_python pDict is null"<<endl;
		return -1;
	}
	//1.从模块中直接获取函数
//	pFunc = PyObject_GetAttrString(pModule, "hello");//调用的函数名
	//2.从字典属性中获取函数
	pFunc = PyDict_GetItemString(pDict, "hello");
	if(pFunc == NULL){
		cout << "test_python pFunc is null"<<endl;
		return -1;
	}
    //参数类型转换,传递一个字符串。将c/c++类型的字符串转换为python类型,元组中的python类型查看python文档
	PyRun_SimpleString("print('----------Py_BuildValue')");
	PyObject *pArg = Py_BuildValue("(s)", " c++ bob");
    //调用直接获得的函数,并传递参数
    PyEval_CallObject(pFunc, pArg);
    //PyEval_CallObject(pFunc, NULL);//调用函数,NULL表示参数为空
    Py_DECREF(pModule);
}
//调用Add函数,传两个int型参数
int add()
{
	PyObject * pModule = NULL;
	PyObject * pFunc = NULL;
	pModule = PyImport_ImportModule("test_python");//
	if(pModule == NULL){
		cout << "test_python module add is null"<<endl;
		return -1;
	}
	pFunc = PyObject_GetAttrString(pModule, "add");//Add:Python文件中的函数名
	if(pFunc == NULL){
		cout << "test_python add pFunc is null"<<endl;
		return -1;
	}
	//创建参数:
	PyObject *pArgs = PyTuple_New(2);//函数调用的参数传递均是以元组的形式打包的,2表示参数个数
	PyTuple_SetItem(pArgs, 0, Py_BuildValue("i", 6));//0--序号,i表示创建int型变量
	PyTuple_SetItem(pArgs, 1, Py_BuildValue("i", 8));//1--序号
	// pArg = Py_BuildValue("(i, i)", 6, 8);
	//返回值
	PyObject *pReturn = NULL;
	pReturn = PyEval_CallObject(pFunc, pArgs);//调用函数
	if(pReturn == NULL){
		cout << "test_python add pReturn is null"<<endl;
		return -1;
	}
	//将返回值转换为int类型
	int result;
	PyArg_Parse(pReturn, "i", &result);//i表示转换成c++ int型变量
	cout << "6 + 8 = " << result << endl;
	Py_DECREF(pModule);
}
//调用Add函数,传两个int型参数
int add2(const char* s1,const char*s2)
{
	PyObject * pModule = NULL;
	PyObject * pFunc = NULL;
	pModule = PyImport_ImportModule("test_python");//
	if(pModule == NULL){
		cout << "test_python module add is null"<<endl;
		return -1;
	}
	pFunc = PyObject_GetAttrString(pModule, "add2");//Add:Python文件中的函数名
	if(pFunc == NULL){
		cout << "test_python add pFunc is null"<<endl;
		return -1;
	}
	//创建参数:
	PyObject *pArgs = PyTuple_New(2);//函数调用的参数传递均是以元组的形式打包的,2表示参数个数
	PyTuple_SetItem(pArgs, 0, Py_BuildValue("s", s1));//0--序号,i表示创建int型变量
	PyTuple_SetItem(pArgs, 1, Py_BuildValue("s", s2));//1--序号
	//返回值
	PyObject *pReturn = NULL;
	pReturn = PyEval_CallObject(pFunc, pArgs);//调用函数
	if(pReturn == NULL){
		cout << "test_python add pReturn is null"<<endl;
		return -1;
	}

	char *res;
	PyArg_Parse(pReturn, "s", &res);
	cout << "111s1+s2 = " << res << endl;
	Py_DECREF(pModule);
}
int test_class()
{
	PyObject * pModule = NULL;
	pModule = PyImport_ImportModule("test_python");
	PyObject *pDict = PyModule_GetDict(pModule);
	PyObject *pClass = PyDict_GetItemString(pDict, "ctest");
	PyObject *result =NULL;

	PyRun_SimpleString("print('----------PyInstance_New test class')");
	PyObject *pInstance = PyInstanceMethod_New(pClass); //python3
//  PyObject *pInstance = PyInstance_New(pClass, NULL, NULL);
    if (!pInstance) {
         printf("Cant create second instance./n");
         return -1;
    }
	//调用类的方法
	PyRun_SimpleString("print('----------PyObject_CallMethod')");
	result = PyObject_CallMethod(pInstance, "say", "(s)", "Charity");
	if(result == NULL){
		cout << "test_python class result is null"<<endl;
		return -1;
	}
	//输出返回值
	char* name=NULL;
	PyRun_SimpleString("print('----------PyArg_Parse')");
	PyArg_Parse(result, "s", &name);
	printf("%s\n", name);
	PyRun_SimpleString("print('Python End')");
	//释放
	Py_DECREF(pInstance);
	Py_DECREF(pClass);
	Py_DECREF(pModule);
}
void test_cpp_call_python(){
	init_python_env();
	cout << "调用test_python.py中的Hello函数..." << endl;
	hello();
	cout << "\n调用test_python.py中的Add函数..." << endl;
	add();
	cout << "\n调用test_python.py中的Add2函数..." << endl;
	add2("aaa","bbbb");
	cout << "\n调用test_python.py中的Test类.." << endl;
	test_class();
	free_python_env();
}

int main(int argc, char** argv){
	test_cpp_call_python();
    return 0;
}
  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值