OpenCV-Python bindings生成流程
通过上篇文章和opencv python模块中的CMakeLists.txt文件,可以了解到opencv-python bindings生成的整个流程:
-
生成headers.txt文件
将每个模块的头文件添加到list中,通过一些关键词过滤掉一些不需要扩展的头文件,file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/headers.txt" "${opencv_hdrs}")
将过滤后的list写入文件;
headers.txt保存需要转换的头文件路径,英文;隔开,注意文件末尾没有换行符 -
生成cv2.cpp中需要的头文件
set(cv2_generated_hdrs"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_include.h""${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_funcs.h""${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_types.h""${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_type_reg.h""${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_ns_reg.h")file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/headers.txt" "${opencv_hdrs}") add_custom_command(OUTPUT ${cv2_generated_hdrs}COMMAND ${PYTHON_EXECUTABLE} "${PYTHON_SOURCE_DIR}/src2/gen2.py" ${CMAKE_CURRENT_BINARY_DIR} "${CMAKE_CURRENT_BINARY_DIR}/headers.txt"DEPENDS ${PYTHON_SOURCE_DIR}/src2/gen2.pyDEPENDS ${PYTHON_SOURCE_DIR}/src2/hdr_parser.pyDEPENDS ${CMAKE_CURRENT_BINARY_DIR}/headers.txtDEPENDS ${opencv_hdrs})
通过调用gen2.py来实现的,第一个参数是生成头文件后保存的路径,第二个参数是headers.txt文件路径
-
将cv2.cpp编译成动态库
OpenCV-Python bindings实践操作
纸上得来终觉浅,绝知此事要躬行。整个事情的来龙去脉都已经讲清楚,单到底怎么应用到自己的项目中呢。比如将自己图像检测功能实现完成,老大要求将它写成Web服务,毕竟B/S模式非常流程。C++写Web?开国际玩笑吧,现在python非常流行的天下。那么接下来看看怎么讲c++功能函数类转换成python的扩展库吧。
网上查查资料,还真有相关的blog:
- How to convert your OpenCV C++ code into a Python module
- github code https://github.com/spmallick/learnopencv/tree/master/pymodule
博客写的很好,但是提供的源码千般尝试始终报错,于是乎决定自己从CMakeLists.txt看起,从gen2.py脚本看起,慢慢查实。
mkdir ovex
cd ovex
mkdir src
cp ../opencv-3.1.0/modules/python/src2/{pycompat.hpp,cv2.cpp,gen2.py,hdr_parser.py} ./
cp ../learnopencv-master/pymodule/src/* src/
cp ../learnopencv-master/pymodule/headers.txt ./
准备使用opencv-3.1.0源码中的pycompat.hpp,cv2.cpp,gen2.py,hdr_parser.py文件,使用上面博客中的opencv c++源文件,慢慢来
gen2.py ./ headers.txt
命令,第一步就报错了IOError: [Errno 2] No such file or directory: 'src/bvmodule.hpp\n'
看到文件名后面居然有个换行符,哎,拿到windows下删掉换行符。
去掉headers.txt中的换行符在去执行上面的命令,报出另外一个错误
Traceback (most recent call last):File "./gen2.py", line 943, in <module>generator.gen(srcfiles, dstdir)File "./gen2.py", line 855, in genself.code_include.write( '#include "{0}"\n'.format(hdr[hdr.rindex('opencv2/'):]) )
ValueError: substring not found
hdr.rindex('opencv2/')
取头文件中opencv/
的位置,hdr[hdr.rindex('opencv2/'):])
取头文件中opencv/
之后的所有字符串。看样子脚本是opencv定制,应用需要改动脚本某些地方,通过不断尝试,几经修改,终于大功告成。gen2.py ./ headers.txt
命令在当前目录下生成pyopencv_generated_funcs.h pyopencv_generated_include.h pyopencv_generated_ns_reg.h pyopencv_generated_type_reg.h pyopencv_generated_types.h文件。gen2.py修改如下
调整博客提供的编译命令,编译动态库
g++ -shared -rdynamic -g -O3 -Wall -fPIC \
-I . -I../ -I/usr/local/python2.7.14/lib/python2.7/site-packages/numpy/core/include \
cv2.cpp src/bvmodule.cpp \
-DNDEBUG \
`PKG_CONFIG_PATH=/usr/local/opencv_with_contrib3.1.0/lib/pkgconfig pkg-config --cflags --libs opencv` \
`/usr/local/python2.7.14/bin/python2.7-config --includes --ldflags` \
-L`/usr/local/python2.7.14/bin/python2.7-config --exec-prefix`/lib \
-o bv.so
出错
cv2.cpp:124: 错误:‘Stitcher’未声明
cv2.cpp:124: 错误:expected initializer before ‘Status’
cv2.cpp:474: 错误:ISO C++ 不允许声明无类型的‘Status’
cv2.cpp:474: 错误:expected ‘,’ or ‘...’ before ‘&’ token
cv2.cpp:474: 错误:‘PyObject* pyopencv_from(int)’的模板标识符‘pyopencv_from<>’不匹配任何模板声明
cv2.cpp:1257: 警告:‘int convert_to_char(PyObject*, char*, const char*)’定义后未使用
类型未声明,用不到的话就删掉吧
再次执行编译命令,期待的so动态库在当前目录生成了。那就试他一试吧,
ovex]$ python2.7
Python 2.7.14 (default, Jul 25 2018, 13:52:02)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import bv
Traceback (most recent call last):File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define init function (initbv)
这个错误的出现不得不促使我去了解一下C/C++为python做扩展的整个流程,Extending and Embedding the Python Interpreter,注意python2与python3是有差异的。
很容易发现,模块需要一个对应初始化函数来初始化模块,比如PyInit_bv,那么在opencv中这个初始化函数在哪定义的呢?直接点在当前目录下执行grep -n --color=auto "cv2" -R *
,在cv2.cpp中找到我们想要的函数了,接下来就是修改了
模块的名称是通过MODULESTR来定义的,python2以及python3中模块初始化函数的声明与定义。
再次编译成功通过,测试OK
$ python2.7
Python 2.7.14 (default, Jul 25 2018, 13:52:02)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import bv
>>> import numpy as np
>>> dir(bv)
['CV_16S', 'CV_16SC1', 'CV_16SC2', 'CV_16SC3', 'CV_16SC4', 'CV_16U', 'CV_16UC1', 'CV_16UC2', 'CV_16UC3', 'CV_16UC4', 'CV_32F', 'CV_32FC1', 'CV_32FC2', 'CV_32FC3', 'CV_32FC4', 'CV_32S', 'CV_32SC1', 'CV_32SC2', 'CV_32SC3', 'CV_32SC4', 'CV_64F', 'CV_64FC1', 'CV_64FC2', 'CV_64FC3', 'CV_64FC4', 'CV_8S', 'CV_8SC1', 'CV_8SC2', 'CV_8SC3', 'CV_8SC4', 'CV_8U', 'CV_8UC1', 'CV_8UC2', 'CV_8UC3', 'CV_8UC4', 'Filters', '__doc__', '__file__', '__name__', '__package__', '__version__', 'createTrackbar', 'error', 'fillHoles', 'setMouseCallback']
>>> import cv2
>>> img = cv2.imread("holes.jpg")
>>> bv.fillHoles(img)
>>> cv2.imwrite("filledHoles.jpg", img)
True
>>>