如何构建需要cmake的C

如何构建需要cmake的C

本文介绍了如何构建需要cmake的C ++嵌入式Python文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个C ++文件,它有一个应该从Python文件中调用的函数。但是这个C ++文件需要通过cmake构建,因为存在OpenCV依赖项。当我运行主Python文件时,我收到以下错误:



 ImportError:/usr/local/lib/python2.7 /dist-packages/getGender.so:未定义的符号:_ZN2cv3Mat10deallocateEv 





下面是C ++文件getGender.cpp,它具有该功能这将由Python代码调用。



  #include   <   Python.h  >  
#include opencv2 / core / core.hpp
#include opencv2 / contrib / contrib.hpp
#include opencv2 / highgui / highgui.hpp
#include < iostream >
#include < fstream >
#include < sstream >

使用 命名空间 cv;
使用 命名空间标准;

static Mat norm_0_255(InputArray _src){
Mat src = _src.getMat();
// 创建并返回标准化图像:
Mat dst;
switch (src.channels()){
case 1
cv :: normalize(_src,dst, 0 255 ,NORM_MINMAX,CV_8UC1);
break ;
case 3
cv :: normalize(_src,dst, 0 255 ,NORM_MINMAX,CV_8UC3);
break ;
默认
src.copyTo(dst);
break ;
}
return dst;
}



int getGender( const char * path){


vector< Mat>图片;

images.push_back(imread(path, 0 ));

Mat testSample = images [ 0 ];

Ptr< FaceRecognizer> model1 = createFisherFaceRecognizer();

model1-> load( fisherfaces.yml);

int predictLabel = model1-> predict(testSample);

string result_message = format( 预测类=%d,predictLabel );
cout<< result_message<< ENDL;

return predictLabel;
}

static PyObject * getGender_wrapper(PyObject * self,PyObject * args)
{

char * path;

return Py_BuildValue( i的,getGender(路径));

}


静态 PyMethodDef genderMethods [] = {{ getGender,getGender_wrapper,METH_VARARGS, find gender},
{NULL,NULL, 0 ,NULL}
};

PyMODINIT_FUNC initxt()
{
Py_InitModule( getGender ,genderMethods);
}





以下是链接代码的setup.py文件。



 来自 distutils.core  import 设置,扩展

extension_mod =扩展名( getGender,[ getGender.cpp])

setup(name = getGender,ext_modules = [extension_mod])





我运行命令python setup.py build然后python setup.py install。然后我创建了一个测试文件,如下所示并运行它。那是我收到错误的时候。



  import  getGender 
gender = \\ text.getGenderWrapper( / home / kavin / Desktop / actors_cropped / male / a1_200_200.jpg

print 性别





我认为错误是因为无法立即构建c ++文件getGender.cpp。我通常构建一个cmake文件,以便从这个cpp文件创建exe。在这种情况下,我不知道如何将该cmake文件合并到此。请帮助。

解决方案

I have created a C++ file which has a function that should be called from the Python file. But this C++ file requires to be built through cmake as there are OpenCV dependencies. When I run the main Python file I am getting the following error:

ImportError: /usr/local/lib/python2.7/dist-packages/getGender.so: undefined symbol: _ZN2cv3Mat10deallocateEv



Below is the C++ file,getGender.cpp, that has the function which would be called by Python code.

#include <Python.h>
#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <fstream>
#include <sstream>

using namespace cv;
using namespace std;

static Mat norm_0_255(InputArray _src) {
Mat src = _src.getMat();
// Create and return normalized image:
Mat dst;
switch(src.channels()) {
case 1:
    cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
    break;
case 3:
    cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3);
    break;
default:
    src.copyTo(dst);
    break;
}
return dst;
}



int getGender(const char *path) {


vector<Mat> images;

images.push_back(imread(path, 0));

 Mat testSample = images[0];

Ptr<FaceRecognizer> model1 = createFisherFaceRecognizer();

model1->load("fisherfaces.yml");

int predictedLabel = model1->predict(testSample);

string result_message = format("Predicted class = %d ",predictedLabel);
cout << result_message << endl;

return predictedLabel;
}

static PyObject *getGender_wrapper(PyObject *self,PyObject *args)
{

char *path;

return Py_BuildValue("i",getGender(path));

}


static PyMethodDef genderMethods[]= {{"getGender",getGender_wrapper,METH_VARARGS,"find gender"},
{NULL,NULL,0,NULL}
};

 PyMODINIT_FUNC initxt()
{
Py_InitModule("getGender",genderMethods);
} 



Below is the setup.py file which links the code.

from distutils.core import setup,Extension

extension_mod=Extension("getGender",["getGender.cpp"])

setup(name="getGender",ext_modules=[extension_mod])



I ran the commands, "python setup.py build" and then "python setup.py install". Then I created a test file as following and ran it. That is when I get the error.

import getGender
   gender=ext.getGenderWrapper("/home/kavin/Desktop/actors_cropped/male/a1_200_200.jpg")

print gender



I think the error is because the c++ file getGender.cpp cannot be built straight away. There is a cmake file I usually build in order to create the exe from this cpp file. In this scenario, I dont know how to incorporate that cmake file into this. Please help.

解决方案


这篇关于如何构建需要cmake的C ++嵌入式Python文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:43