Click here to Skip to main content
15,886,105 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C++
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.

C++
#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.

Python
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.

Python
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.
Posted
Comments
Richard MacCutchan 22-Jul-15 13:11pm    
The message suggests that the OpenCV library needs to be incorporated into the program when you build it.
mayooran99 22-Jul-15 22:04pm    
Yes, how do I incorporate what is in the cmake file into "python setup.py build" ? Please advice.
Richard MacCutchan 23-Jul-15 3:18am    
Sorry, I am not sure as I have not done this myself. However, there are some notes in the Python documentation at https://docs.python.org/2/extending/extending.html which may help.

1 solution

Your extension definition needs to include your dependencies.

See here: https://docs.python.org/2/extending/building.html[^]


Example from above:
VB
module1 = Extension('demo',
                    define_macros = [('MAJOR_VERSION', '1'),
                                     ('MINOR_VERSION', '0')],
                    include_dirs = ['/usr/local/include'],
                    libraries = ['tcl83'],
                    library_dirs = ['/usr/local/lib'],
                    sources = ['demo.c'])
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900