引言
- 最近有机会测试了一下4.7版opencv contrib下的二维码识别接口,只测了一张最基本的图像。
- 结论是效果没有微信开源的好
- 微信二维码识别模型下载地址:WeChatCV
安装
pip install opencv_contrib_python
测试图像
- 可自行去找一个带有二维码的图像,我尝试放,总是违规。。。。
官方benchmark
测试代码
import numpy as np
import cv2
class CvObjDetector():
def __init__(self):
super().__init__()
self.detector = cv2.QRCodeDetector()
def detect(self, image):
ret, corners = self.detector.detect(image)
if ret is False:
return False, np.array([])
self.detected_corners = corners
return ret, corners
def decode(self, image):
if self.detected_corners.size == 0:
return 0, [], None
r, decoded_info, straight_qrcode = self.detector.decode(image,
self.detected_corners)
self.decoded_info = decoded_info
return r, decoded_info, straight_qrcode
class CvWechatDetector():
def __init__(self, path_to_model="./models/"):
super().__init__()
self.detector = cv2.wechat_qrcode_WeChatQRCode(path_to_model + "detect.prototxt",
path_to_model + "detect.caffemodel",
path_to_model + "sr.prototxt",
path_to_model + "sr.caffemodel")
def detect(self, image):
decoded_info, corners = self.detector.detectAndDecode(image)
if len(decoded_info) == 0:
return False, np.array([])
corners = np.array(corners).reshape(-1, 4, 2)
self.decoded_info = decoded_info
self.detected_corners = corners
return True, corners
def decode(self, image):
if len(self.decoded_info) == 0:
return 0, [], None
return True, self.decoded_info, self.detected_corners
qr = CvObjDetector()
qr_wechat = CvWechatDetector()
img_path = 'qr/c7688f4e0d5c4c8490b466a1aef20be6.png'
img = cv2.imread(img_path, cv2.IMREAD_IGNORE_ORIENTATION)
ret, corners = qr.detect(img)
if ret:
r, decoded_info, straight_qrcode = qr.decode(img)
wechat_ret, wechat_corners = qr_wechat.detect(img)
info = qr_wechat.decode(img)
print('wechat: ' + info)
- 输出结果:
opencv qr: ('',)
wechat: (True, ('http://t.csdn.cn/FOLqH',), array([[[234. , 18.000002],
[300. , 18.000002],
[300. , 84. ],
[234. , 84. ]]], dtype=float32))