https://blog.csdn.net/liukcqu/article/details/79464435
C++调用TensorFlow
https://blog.csdn.net/xiaomu_347/article/details/81040855
调用TensorFlow为NULL
https://blog.csdn.net/u014515463/article/details/81836420
出现NULL的问题,可能是不能调用conda安装的TensorFlow,因此先在pycharm中把TensorFlow卸载了,然后通过pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow,这样就会从清华这边的镜像去安装tensorflow库。
然后就解决了!!!
C++调用python程序主要引入pip中的库,所以涉及的库用pip安装一遍
python程序中涉及的路径均是以.vcxproj为基准的,这里要注意!!!这是调试模式
若是直接运行exe文件,那么默认exe所处的路径就是python当前路径
然后,涉及库的路径问题,我们一般做程序,希望程序在没有安装TensorFlow的电脑上也能跑,所以,我们应该把site-packages放到我们自己程序下,因此要弄懂python import的原理,
https://baijiahao.baidu.com/s?id=1596345816517002361&wfr=spider&for=pc
而且,我们一般是以相对路径来处理,所以,我们要处理一下
# test_model.py
import os
import sys
path1 = os.path.abspath('.')
# 添加库路径
site_packages_path = path1 +"\site-packages"
sys.path.append(site_packages_path)
print("appended site-packages path")
from modelimport getCrackModel
import numpyas np
from matplotlibimport pyplotas plt
import cv2
from tqdmimport tqdm
import tensorflowas tf
就可以了
Py_BuildValue要求字符串不能有中文
pArg = Py_BuildValue("(s)", path);
pArg = Py_BuildValue("s", "sdfsfd");注意两者格式
而且path为char类型变量,不是string,注意
目前碰到的问题是vs调试与直接运行exe文件,工作路径不一样
MFC调用python运行不了,现在查找原因
C++调试时,pRet = PyEval_CallObject(pFunc, pArg); 就看pRet是否为空,为空调用失败
python中屏蔽函数中的代码,在节点放入print来测试
最近发现可以在vs中提前引入必要的python库,一下为操作方式
//---------------------------------------------------------------------
//---------------------------------------------------------------------
//initial python module and append customer path
Py_Initialize();
char path0[50];
PyRun_SimpleString("import sys");
sprintf(path0, "sys.path.append('%s')", path.c_str());
//import bin path
PyRun_SimpleString(path0);
size_t pos = path.length();
string boot_path = path.substr(0, pos - 10);
char path1[50], path2[50], path3[50], path4[50], path5[50];
sprintf(path1, "sys.path.append('%s')", boot_path.c_str());
//import lib path
PyRun_SimpleString(path1);
string site_packages_path = boot_path + "\\site-packages";
sprintf(path2, "sys.path.append('%s')", site_packages_path.c_str());
//import site_packages path
PyRun_SimpleString(path2);
string python_lib_path = boot_path + "\\lib\\python\\lib";
sprintf(path3, "sys.path.append('%s')", python_lib_path.c_str());
//import python_lib path
PyRun_SimpleString(path3);
string site_packages_numpy_path = boot_path + "\\site-packages\\numpy";
sprintf(path4, "sys.path.append('%s')", site_packages_numpy_path.c_str());
//import python_numpy path
PyRun_SimpleString(path4);
string site_packages_cv2_path = boot_path + "\\site-packages\\cv2";
sprintf(path5, "sys.path.append('%s')", site_packages_cv2_path.c_str());
//import python_cv2 path
PyRun_SimpleString(path5);
PyEval_InitThreads();
import_array();
PyRun_SimpleString(path1);
PyRun_SimpleString("print(sys.path)");
PyRun_SimpleString("print(sys.path[-1:])");
//------------------------------------------------------------------------
//------------------------------------------------------------------------












网友评论