我一直在尝试编写一个基于OpenCV构建的C ++视觉库的python包装器,但是当我尝试在Python中导入已编译的模块时,它已经遇到以下错误消息:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "BGSPy.py", line 21, in <module>
_BGSPy = swig_import_helper()
File "BGSPy.py", line 20, in swig_import_helper
return importlib.import_module('_BGSPy')
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: dynamic module does not define init function (init_BGSPy)
以下是我用来构建项目的CMakeList.txt:
cmake_minimum_required(VERSION 2.8)
project(bgs)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
#set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules)
# compilation mode setup
set(CMAKE_BUILD_TYPE Release)
#set(CMAKE_BUILD_TYPE Debug)
SET(OpenCV_STATIC OFF)
IF(UNIX)
# add some standard warnings
ADD_DEFINITIONS(-Wno-variadic-macros -Wno-long-long -Wall -Wextra -Winit-self -Woverloaded-virtual -Wsign-promo -Wno-unused-parameter -pedantic -Woverloaded-virtual -Wno-unknown-pragmas)
# -ansi does not compile with sjn module
#ADD_DEFINITIONS(-ansi)
# if you like to have warinings about conversions, e.g. double->int or double->float etc., or float compare
#ADD_DEFINITIONS(-Wconversion -Wfloat-equal)
endif(UNIX)
if(APPLE)
cmake_policy(SET CMP0042 NEW)
endif()
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
if(${OpenCV_VERSION} VERSION_LESS 3)
message (FATAL_ERROR "This branch of BGSLibrary is not compatible with your OpenCV version: ${OpenCV_VERSION}")
endif()
file(GLOB sources FrameProcessor.cpp PreProcessor.cpp VideoAnalysis.cpp VideoCapture.cpp)
file(GLOB main Main.cpp)
file(GLOB demo Demo.cpp)
file(GLOB demo2 Demo2.cpp)
file(GLOB bgspy BGSPy.cpp)
list(REMOVE_ITEM sources ${demo} ${demo2} ${bgspy})
file(GLOB_RECURSE analysis package_analysis/*.cpp)
file(GLOB_RECURSE bgs package_bgs/*.cpp package_bgs/*.c)
file(GLOB_RECURSE bgs_include package_bgs/*.h)
include_directories(${CMAKE_SOURCE_DIR})
add_library(bgs SHARED ${sources} ${bgs} ${analysis})
target_link_libraries(bgs ${OpenCV_LIBS})
set_property(TARGET bgs PROPERTY PUBLIC_HEADER ${bgs_include})
add_executable(bgs_bin ${main})
target_link_libraries(bgs_bin ${OpenCV_LIBS} bgs)
set_target_properties(bgs_bin
PROPERTIES OUTPUT_NAME bgs)
add_executable(bgs_demo ${demo})
target_link_libraries(bgs_demo ${OpenCV_LIBS} bgs)
add_executable(bgs_demo2 ${demo2})
target_link_libraries(bgs_demo2 ${OpenCV_LIBS} bgs)
set(WRAPPER_PREFIX "BGSPy")
# Swig Module
FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})
FIND_PACKAGE(PythonLibs)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
SET(CMAKE_SWIG_FLAGS "")
SET_SOURCE_FILES_PROPERTIES(${WRAPPER_PREFIX}.i PROPERTIES CPLUSPLUS ON)
SET_SOURCE_FILES_PROPERTIES(${WRAPPER_PREFIX}.i PROPERTIES SWIG_FLAGS "-includeall")
SWIG_ADD_MODULE(${WRAPPER_PREFIX} python ${WRAPPER_PREFIX}.i ${WRAPPER_PREFIX}.cpp)
SWIG_LINK_LIBRARIES(${WRAPPER_PREFIX} ${PYTHON_LIBRARIES} ${OpenCV_LIBS} bgs)
INSTALL(TARGETS bgs
bgs_bin
RUNTIME DESTINATION bin COMPONENT app
LIBRARY DESTINATION lib COMPONENT runtime
ARCHIVE DESTINATION lib COMPONENT runtime
PUBLIC_HEADER DESTINATION include/package_bgs COMPONENT dev
FRAMEWORK DESTINATION "/Library/Frameworks"
)
项目构建成功,但我无法导入它生成的模块。任何帮助将不胜感激。
编辑:这是我的BGSPy.i文件的内容
%module BGSPy
%include <std_string.i>
%{
#include "BGSPy.h"
%}
extern int adaptiveSOM(const std::string fName);