静态编译wechat_qrcode(windows)

wechat_qrcode是微信开源的QRCode解码功能库,目前已并被OpenCV官方收录在opencv_3rdParty中。实际测试,该库确实大幅度提高了QR code的识别率。

0. 关于wechat_qrcode

官方关于该库的描述如下(https://github.com/opencv/opencv_contrib/tree/4.x/modules/wechat_qrcode#wechat-qr-code-detector-for-detecting-and-parsing-qr-code):

WeChat QR code detector for detecting and parsing QR code.

WeChat QR code detector is a high-performance and lightweight QR code detect and decode library, which is contributed by WeChat Computer Vision Team (WeChatCV). It has been widely used in various Tencent applications, including WeChat, WeCom, QQ, QQ Browser, and so on. There are four primary features of WeChat QR code detector:

  1. CNN-based QR code detector. Different from the traditional detector, we introduce a tiny CNN model for multiple code detection. The detector is based on SSD architecture with a MobileNetV2-like backbone, which is run on caffe inference framework.

  2. CNN-based QR code enhancement. To improve the performance of tiny QR code, we design a lighten super-resolution CNN model for QR code, called QRSR. Depth-wise convolution, DenseNet concat and deconvolution are the core techniques in the QRSR model.

  3. More robust finder pattern detection. Besides traditional horizontal line searching, we propose an area size based finder pattern detection method. we calculate the area size of black and white block to locate the finder pattern by the pre-computed connected cells.

  4. Massive engineering optimization. Based on zxing-cpp, we conduct massive engineering optimization to boost the decoding success rate, such as trying more binarization methods, supporting N:1:3:1:1 finder pattern detection, finding more alignment pattern, clustering similar size finder pattern, and etc.

大意如下:

wechat_qrcode是由微信计算机视觉团队(WeChatCV)贡献的一款高性能、轻量级的二维码检测和解码库。它已被广泛应用于腾讯的各种应用,包括微信、微信、QQ、QQ浏览器等。wechat_qrcode有四个主要功能:

1. WeChatCV团队做了一个用于多码检测的微小CNN模型。检测模型是SSD,具有类似MobileNetV2的主干,运行在caffe推理框架上。

2. 为了提高微小QR码的性能,WeChatCV团队设计了超分辨率CNN模型,称为QRSR。

3. 除了传统的水平线搜索之外,WeChatCV团队提出了一种基于面积大小的取景器模式检测方法。

4. 对zxing-cpp,WeChatCV团队进行了大规模的优化,以提高解码成功率。

1. 编译带有wechat_qrcode的opencv

暂时未找到已有opencv的情况下,补充编译wechat_qrcode模块的办法。如果朋友们有找到更简单的办法,欢迎在评论区留言。以下将此次编译过程做一记录(windows)

1.1 下载源码

opencv官方将主干与扩展功能放在两个git里面,分别是opencvopencv_contrib。需要把这两个都下载下来。

opencv的git结构

开始编译之前,需要做两次configure。第一次configure,是opencv相关的配置;第二次是将wechat_qrcode加到opencv的Addition function,从而一同编译出来。

1.2 关于编译器版本的说明

wechat_qrcode依赖zxing-cpp,而(新版本的)zxing-cpp需要用到c++17,建议使用vs2017或更新的编译套件,会少走很多弯路。

如果要编译32位,注意在选择正确的编译选项。本次编译使用vs2019,编译win32版本。

编译32位版本

1.3 首次configure时,报“Could NOT find PythonInterp: Found unsuitable version "2.7.11", but required is at least "3.2" (found C:/Python27/python.exe)”

python的版本不够用,那就装一下python3.2

装完配Path,如果要保留python2.7,将python3.2的优先级提到python2.7之前

python2.7与python3.2共存

1.4 首次configure时,报“SIMD extensions disabled: could not find NASM compiler. Performance will suffer.”

CMake Warning at 3rdparty/libjpeg-turbo/src/simd/CMakeLists.txt:2 (message):

SIMD extensions disabled: could not find NASM compiler. Performance will suffer.
Call Stack (most recent call first):
3rdparty/libjpeg-turbo/src/simd/CMakeLists.txt:52 (simd_fail)

可以选择安装,也可以忽略;如果忽略,将影响jpeg-turbo的性能;注意安装NASM重启电脑后才能生效

1.5 首次configure时,报“Could NOT find JNI (missing: JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH)”

Could NOT find JNI (missing: JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH)
VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file

JNI相关的功能,目前用不到,先忽略了。如果需要用到,如提示所说,可以尝试安装个VTK,这个我没有验证。

1.6 添加扩展的opencv模块

第一次configure完成后,找到OPENCV_EXTRA_MODULES_PATH,将contrib路径添加进来,注意要具体到modules/,继续configure。

注意:路径分隔符必须写成“/”。如果按照Windows习惯写为“\”,会报类似以下错误,原因是错把“\”当成转义了。

添加opencv扩展模块报错

路径分隔符改为“/”就正常了

添加opencv扩展模块-路径写成这样就不报错了

1.7 再次configure时,报大量形如“IPPICV: Downloading ippicv_2020_win_ia32_20191018_general.zip from https://raw.githubusercontent.com/opencv/opencv_3rdparty/a56b6ac6f030c312b2dce17430eef13aed9af274/ippicv/ippicv_2020_win_ia32_20191018_general.zip”的错误

这是因为部分依赖的组件本地未找到,根据CMakeList的要求,自动从github上下载。由于众所周知的原因,下载失败了。

从报错指向的位置,或者从CMakeList里找到下载路径,使用适当的下载工具手动下载,再放到\.cache\ippicv里面。其他提示缺失的部件,也是类似处理方式。

.cache文件夹中,放置了各种依赖的下载打包。

.cache文件夹

当然也可以通过配置hosts的方式,或者修改CMakeList文件中github主站的位置改为国内加速站的方式实现依赖模块代码的下载。在此不赘述,可参考这篇文章:https://blog.csdn.net/tanmx219/article/details/101632917/

1.8 关于静态编译

去掉BUILD_SHARED_LIBS的选择即可

1.9 关于BUILD_opencv_world

按照opencv正常的编译方式,编译后会产生一大堆文件。opencv3开始,多了BUILD_opencv_world一个编译选项。加上该选项后,将只生成一个名为opencv_world的文件(.dll或者.lib),这个文件包含了opencv主干和被选择编译的扩展模块。这样一来发布的时候固然方便了许多,但是不可避免的这玩意体积肉眼可见的大。。。

 

参考文章:https://www.likecs.com/show-203631188.html?sc=2600

 

二零二二年十月二十三日

顾毅写于厦门