vendredi 26 octobre 2018

Segmentation Fault on using PyArray_SimpleNewFromData on converting mat to numpyarray

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")

Aucun commentaire:

Enregistrer un commentaire