开发二维码识别功能,使用到开源三方库opencv-contrib-python。
在使用docker封装时,中途遇到几个报错,记录下来解决办法。
使用前需要安装 opencv-contrib-python 包,注意安装的包不能低于4.5.2版本。
本处使用版本opencv-contrib-python==4.6.0.66
import cv2
def decode_wechat_cv(path):
"""
使用 WeChatCv
:param path:
:return:
"""
print('cv2.__version__:', cv2.__version__)
detect_obj = cv2.wechat_qrcode_WeChatQRCode()
img = cv2.imread(path)
res, points = detect_obj.detectAndDecode(img)
print('res:', res)
print('points:', points)
模型文件从此处:https://github.com/WeChatCV/opencv_3rdparty/tree/wechat_qrcode 下载:

实测模型版识别效果更好,对于模糊图片,基础版无法识别,但模型版可以。
import cv2
def decode_wechat_cv_load_model(path):
"""
使用 WeChatCv, 加载模型
:param path:
:return:
"""
print('cv2.__version__:', cv2.__version__)
root = "./model/"
p1 = root + 'detect.prototxt'
p2 = root + 'detect.caffemodel'
p3 = root + 'sr.prototxt'
p4 = root + 'sr.caffemodel'
detect_obj = cv2.wechat_qrcode_WeChatQRCode(p1, p2, p3, p4)
img = cv2.imread(path)
res, points = detect_obj.detectAndDecode(img)
print('res:', res)
print('points:', points)
参考:在Python中使用微信扫码功能(OpenCV WeChatQRCode)
基础环境:centos8、python3.7
当直接使用 pip install opencv-contrib-python 时,出现如下报错:

解决:pip3 install --upgrade pip
成功安装opencv-contrib-python后,使用 import cv2 时,报错如下:

解决:yum install mesa-libGL.x86_64
pip3 install --upgrade pip
pip3 install opencv-contrib-python==4.6.0.66 -i https://pypi.douban.com/simple
yum install mesa-libGL.x86_64
import cv2
ob = cv2.wechat_qrcode_WeChatQRCode()
无报错即可