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();
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.