0

Strange Segmentation Fault in PyArray_SimpleNewFromData

Segmentation fault in PyArray_SimpleNewFromData

I am posting this question after trying all the possible solutions in the two posts mentioned above, I am still facing a segmentation. My Code is pretty simple and straight but i dont know how to get rid of this. Any Solutions for this would be of great help.

My Code is:

#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <Python.h>
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
#include <numpy/arrayobject.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <vector>
using namespace boost::python;
using namespace boost::python::numpy;
using namespace cv;

PyObject* process(PyObject* arr,int rows,int cols)
{
    void* img_arr = PyArray_DATA((PyArrayObject*)arr);
    Mat image(rows, cols , CV_8UC3, img_arr);

    Mat median_blur;
    medianBlur(image, median_blur, 15);

    npy_intp dimensions[3] = {median_blur.rows, median_blur.cols, median_blur.channels()};
    return PyArray_SimpleNewFromData(median_blur.dims + 1, &dimensions[0], NPY_UINT8, median_blur.data);
}

BOOST_PYTHON_MODULE(PyWrap_CPPM)
{ 
    boost::python::numeric::array::set_module_and_type("numpy", "ndarray");
    def("process", process);
}

If I try to add the import_array() i get another error stating the following:

In file included from /usr/include/signal.h:316:0,
                 from /usr/include/numpy/npy_interrupt.h:84,
                 from /usr/include/numpy/arrayobject.h:5,
                 from /home/adminspin/srcs/Harish_Programs/numpy_pass_c++/PyWrap_CppMod.cpp:5:
/home/adminspin/srcs/Harish_Programs/numpy_pass_c++/PyWrap_CppMod.cpp: In function ‘void init_module_PyWrap_CPPM()’:
/home/adminspin/srcs/Harish_Programs/numpy_pass_c++/PyWrap_CppMod.cpp:27:2: error: return-statement with a value, in function returning 'void' [-fpermissive]
  import_array();
  ^
CMakeFiles/PyWrap_CPPM.dir/build.make:62: recipe for target 'CMakeFiles/PyWrap_CPPM.dir/PyWrap_CppMod.cpp.o' failed
make[2]: *** [CMakeFiles/PyWrap_CPPM.dir/PyWrap_CppMod.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/PyWrap_CPPM.dir/all' failed
make[1]: *** [CMakeFiles/PyWrap_CPPM.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

And My Cmake File is:

cmake_minimum_required(VERSION 3.0)

set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -std=c++11")
set(_Boost_NUMPY_DEPENDENCIES python${component_python_version})

project(boost_try_ex)

set(Boost_LIBS
 filesystem
 thread
 date_time
 chrono
 system
 timer
 serialization
 python3
 numpy3 
)

find_package(Boost REQUIRED COMPONENTS "${Boost_LIBS}")
add_definitions(-DBOOST_ALL_DYN_LINK)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIR})

find_package(PythonLibs 3 REQUIRED)
include_directories(${PYTHON_INCLUDE_PATH})

FIND_PACKAGE( OpenCV REQUIRED )                                
INCLUDE_DIRECTORIES( ${OpenCV_INCLUDE_DIRS} )

add_library(PyWrap_CPPM SHARED PyWrap_CppMod.cpp)
target_link_libraries(PyWrap_CPPM ${Boost_LIBRARIES} ${OpenCV_LIBRARIES})
set_target_properties(PyWrap_CPPM PROPERTIES PREFIX "" COMPILE_FLAGS "-DPYTHON_INTERFACE")
4
  • 1
    Where is the code calling import_array? Commented Oct 26, 2018 at 12:32
  • I would just add import_array at the first line inside boost_python_module and get the following error. Thank You Commented Oct 26, 2018 at 12:41
  • Possible duplicate of how to return numpy.array from boost::python? Commented Oct 26, 2018 at 12:48
  • @Matthieu Brucher. Thanks for the link. But I want to do this using this Py_ArraySimpleNewFromData() so that my conversion rate is optimal and efficient, I have an solution by duplicating the data's which ends up in a very conversion of approx 1.6sec to blur an image in c++ and return to python. Whereas the python alone takes 0.22sec and c++ alone takes 0.10secs. So on this note it becomes incompatible. Commented Oct 29, 2018 at 4:02

0

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.