5

I am exporting C++ API for python code using Cython. The application will be executed on Ubuntu. The project files are present here

The function I am wrapping, reads the file name of the image and displays the image. The Show_Img.pyx file looks as follows

import cv2

cdef public void Print_image(char* name):
  img = cv2.imread(name)
  cv2.imshow("Image", img)
  while(True):
    if cv2.waitKey(1) & 0xFF == ord('q'):
      break

The c++ interface generated from Cython looks as follows

__PYX_EXTERN_C DL_IMPORT(void) Print_image(char *);

The header file is included in my algo.cpp, which calls the function like below

#include<iostream>
#include<Python.h>
#include"Show_Img.h"
using namespace std;

int main(){
  char *name = "face.jpg"; 
  Py_Initialize();
  Print_image(name);
  Py_Finalize();
  return 0;
}

With the command below, I can also compile the above code and also generate the application

g++ algo.cpp `pkg-config --libs --cflags python-2.7` `pkg-config --libs --cflags opencv` -L. -lShow_Img -o algo

also the path to the library LD_LIBRARY_PATH is set correctly

Upon execution of the application, there is an error Segmentation fault (core dumped)

Why am I unable to execute the application, is there a mistake in the generation process? or Library linking?

1
  • You probably have to import the module. If you don't the stuff at the global scope (import cv2) won't get run. Commented Feb 17, 2017 at 16:43

1 Answer 1

3
+50

To follow up on my comment, You have to call an init function for the module:

// ...
Py_Initialize();
initShow_Img(); // for Python3
   // (especially with the more modern 2 phase module initialization)
   //  the process is a little more complicated - see the documentation
Print_image(name);
Py_Finalize();
// ...

The reason being is that this sets up the module, include executing the line import cv2. Without it things like accessing the module globals (to get to cv2) won't reliably work. This a likely cause of the segmentation fault.

This is in the documentation example.

Sign up to request clarification or add additional context in comments.

6 Comments

I tried importing the module cv2 inside the function, but still doesn't work, I added import cv2 after the line cdef public void Print_image(char* name):
The key change is the addition of initShow_Img(). You need to do this. It does a lot of things needed to make Cython work, beyond just import cv2.
Yes I realised it just now, that I had missed the statement. So dumb of me
No problem! Small additions aren't always obvious.
Hi David, I saw that you have worked extensively on Cython and Numpy tools. I am currently doing a student project that uses a lot of Cython, Is there anyother way I can contact you apart from StackOverflow? I definitely believe that your answers will be of great help. Thank you in advance
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.