我的C++源代码为:
//#include <Python.h>
#include<python2.7/Python.h>
#include<iostream>
//#include <windows.h>
#include <string.h>
#include <string>
#include "opencv2/opencv.hpp"
#include <numpy/arrayobject.h>
#include "thread"
#include <mutex>
# define _GLIBCXX_USE_CXX11_ABI 0
using namespace std;
using namespace cv;
/**
string Utf8ToGbk(const char* src_str)
{
int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, NULL, 0);
wchar_t* wszGBK = new wchar_t[len + 1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char* szGBK = new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
string strTemp(szGBK);
if (wszGBK) delete[] wszGBK;
if (szGBK) delete[] szGBK;
return strTemp;
}
**/
void init_numpy()
{
import_array();
}
int transport(cv::Mat img)
{
//Py_SetPythonHome(L"C:/Users/38487/Anaconda3");
Py_SetPythonHome("/home/jk/anaconda2/envs/car_dect/lib/python3.6");
Py_Initialize();//初始化
//import_array();
init_numpy();
PyRun_SimpleString("import sys"); //
PyRun_SimpleString("sys.path.append('./')");//
PyObject* pModule = PyImport_ImportModule("car_plate_frozen");// 加载车牌识别模块
PyObject* pDict = PyModule_GetDict(pModule);
PyObject* pFuncFive = PyDict_GetItemString(pDict, "load_image"); //加载载入图片模块。
//PyObject* pFuncFive2 = PyDict_GetItemString(pDict, "init");//
//PyObject_CallObject(pFuncFive2, NULL);
npy_intp dims[] = { img.rows, img.cols, 3 };
PyObject* pValue = PyArray_SimpleNewFromData(3, dims, NPY_UINT8, img.data);
PyObject* ArgArray = PyTuple_New(1);
//PyTuple_SetItem(ArgArray, 0, pValue);
PyObject* pRet = PyObject_CallObject(pFuncFive, ArgArray);
char* res;
PyArg_Parse(pRet, "s", &res);//
//string str_res = Utf8ToGbk(res);
cout << "res:" << res << endl;//
//Py_Finalize();
return 0;
}
int main()
{
cv::Mat img = cv::imread("/home/jk/Downloads/Project3/1.jpg", 1);
cv::Mat img1 = cv::imread("/home/jk/Downloads/Project3/2.jpg", 1);
PyEval_InitThreads();
//PyEval_ReleaseThread(PyThreadState_Get());
thread t1(transport, img);
//thread t2(transport, img1);
t1.join();
//t2.join();
//transport(pFuncFive, img);
Py_Finalize();
return 0;
}
我在终端使用的编译命令为:g++ test_multithread.cpp -I/usr/include/python3.5 -L/usr/lib/python3.5 -lpython3.5m -std=c++11 -lpthread `pkg-config opencv --cflags --libs`
然后报的错为:
In file included from /usr/include/numpy/ndarraytypes.h:1777:0,
from /usr/include/numpy/ndarrayobject.h:18,
from /usr/include/numpy/arrayobject.h:4,
from test_multithread.cpp:8:
/usr/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
#warning "Using deprecated NumPy API, disable it by " \
^
test_multithread.cpp:11:0: warning: "_GLIBCXX_USE_CXX11_ABI" redefined
# define _GLIBCXX_USE_CXX11_ABI 0
^
In file included from /usr/include/c++/5/iostream:38:0,
from test_multithread.cpp:3:
/usr/include/x86_64-linux-gnu/c++/5/bits/c++config.h:212:0: note: this is the location of the previous definition
# define _GLIBCXX_USE_CXX11_ABI 1
^
test_multithread.cpp: In function ‘int transport(cv::Mat)’:
test_multithread.cpp:40:74: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
Py_SetPythonHome("/home/jk/anaconda2/envs/car_dect/lib/python3.6");
^
/tmp/ccAIqf6I.o: In function `_import_array':
test_multithread.cpp:(.text+0x181): undefined reference to `PyCObject_Type'
test_multithread.cpp:(.text+0x1e5): undefined reference to `PyCObject_AsVoidPtr'
请问有知道的大神吗,可以告诉我怎么解决吗。。。。快崩溃了。。。。