OpenCV4.7版二维码检测识别代码比较与整理

文章对比了OpenCV4.7版本中自带的二维码识别接口与微信开源的二维码识别模型在实际应用中的效果。尽管OpenCV在官方基准测试中表现出色,但在作者的简单测试中,微信的模型对于特定图像的识别效果更优。文章提供了两种检测器类的实现代码供参考。
摘要由CSDN通过智能技术生成
引言
  • 最近有机会测试了一下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
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))
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值