在Python中使用微信扫码功能(OpenCV WeChatQRCode)

38 篇文章 5 订阅
36 篇文章 6 订阅

原文链接:http://www.juzicode.com/opencv-note-wechat-qrcode-detect-decode

微信开发团队在今年年初的时候将其二维码扫描功能贡献给了OpenCV社区,在OpenCV-Python中也可以使用微信扫码功能了。

使用前需要安装opencv-contrib-python包,注意安装的包不能低于4.5.2版本。

使用起来也非常简单,近乎一行流的风格,首先是用wechat_qrcode_WeChatQRCode()创建检测实例,再用detectAndDecode()检测和识别:

#VX公众号: 桔子code / juzicode.com 
import cv2  
print('cv2.__version__:',cv2.__version__)
detect_obj = cv2.wechat_qrcode_WeChatQRCode()
img = cv2.imread('pic/qr.jpg')
res,points = detect_obj.detectAndDecode(img)
print('res:',res)
print('points:',points)

运行结果:

cv2.__version__: 4.5.3
res: ['http://weixin.qq.com/r/Ejr54d-EkYLurZuC928A']
points: [array([[   0.,    0.],
       [1079.,    0.],
       [1079.,  393.],
       [   0.,  393.]], dtype=float32)]

返回的结果包含了2个参数,第一个为识别到的二维码信息,第2个为二维码的位置点,二者都是list类型。

我们可以像 QRCodeDetector识别二维码 一文中那样用points标注出二维码的位置:

for pos in points:
    color=(0,0,255)
    thick=3
    for p in [(0,1),(1,2),(2,3),(3,0)]:
        start = int(pos[p[0]][0]),int(pos[p[0]][1])
        end = int(pos[p[1]][0]),int(pos[p[1]][1])
        cv2.line(img,start,end,color,thick)

cv2.imshow('img',img)
cv2.imwrite('wechat-qrcode-detect-1.jpg',img)
cv2.waitKey()
cv2.destroyAllWindows()

但是这个时候看到检测到的二维码位置是错误的,那是因为在创建检测实例的时候没有传入模型文件导致的,为了更好的检测精度和检测效果,应该在创建实例的时候传入模型文件,模型文件可以在https://github.com/WeChatCV/opencv_3rdparty/tree/wechat_qrcode 下载到,该链接包含了4个模型文件。

下面的例子是在wechat_qrcode_WeChatQRCode()创建实例的时候传入4个模型文件,注意根据入参名称确定4个模型文件的顺序:
cv2.wechat_qrcode_WeChatQRCode( [, detector_prototxt_path[, detector_caffe_model_path[, super_resolution_prototxt_path[, super_resolution_caffe_model_path]]]] )

#VX公众号: 桔子code / juzicode.com 
import cv2  
print('cv2.__version__:',cv2.__version__)
detect_obj = cv2.wechat_qrcode_WeChatQRCode('detect.prototxt','detect.caffemodel','sr.prototxt','sr.caffemodel')
img = cv2.imread('pic/qr.jpg')
res,points = detect_obj.detectAndDecode(img)
print('res:',res)
print('points:',points)
for pos in points:
    color=(0,0,255)
    thick=3
    for p in [(0,1),(1,2),(2,3),(3,0)]:
        start = int(pos[p[0]][0]),int(pos[p[0]][1])
        end = int(pos[p[1]][0]),int(pos[p[1]][1])
        cv2.line(img,start,end,color,thick)
cv2.imshow('img',img)
cv2.imwrite('wechat-qrcode-detect.jpg',img)
cv2.waitKey()
cv2.destroyAllWindows()

运行结果:

cv2.__version__: 4.5.3
res: ['http://weixin.qq.com/r/Ejr54d-EkYLurZuC928A']
points: [array([[ 68.91869,  68.94066],
       [309.48257,  68.94066],
       [309.48257, 304.46286],
       [ 68.91869, 304.46286]], dtype=float32)]

从这里可以看到识别到二维码的位置就更精确了。

下面是一张图片中包含多个二维码的情况:

#VX公众号: 桔子code / juzicode.com 
import cv2  
detect_obj = cv2.wechat_qrcode_WeChatQRCode('detect.prototxt','detect.caffemodel','sr.prototxt','sr.caffemodel')
img = cv2.imread('pic/qr-multi.jpg')
res,points = detect_obj.detectAndDecode(img)
print('res:',res)
print('points:',points)
for pos in points:
    color=(0,0,255)
    thick=3
    for p in [(0,1),(1,2),(2,3),(3,0)]:
        start = int(pos[p[0]][0]),int(pos[p[0]][1])
        end = int(pos[p[1]][0]),int(pos[p[1]][1])
        cv2.line(img,start,end,color,thick)
cv2.imshow('img',img)
cv2.imwrite('wechat-qrcode-detect-multi.jpg',img)
cv2.waitKey()
cv2.destroyAllWindows()

运行结果:

cv2.__version__: 4.5.3
res: ['this is a test image', 'www.juzicode.com  vx: juzicode']
points: [array([[529.5263 , 129.34688],
       [796.45605, 129.34688],
       [796.45605, 381.95035],
       [529.5263 , 381.95035]], dtype=float32), array([[ 61.99477,  73.86028],
       [416.3537 ,  73.86028],
       [416.3537 , 408.76263],
       [ 61.99477, 408.76263]], dtype=float32)]

当不加载模型文件时,如果图像中包含了多个二维码,就只能识别到其中1个:

#VX公众号: 桔子code / juzicode.com 
import cv2  
detect_obj = cv2.wechat_qrcode_WeChatQRCode()#'detect.prototxt','detect.caffemodel','sr.prototxt','sr.caffemodel'
img = cv2.imread('pic/qr-multi.jpg')
res,points = detect_obj.detectAndDecode(img)
print('res:',res)
print('points:',points)

运行结果:

cv2.__version__: 4.5.3
res: ['this is a test image']
points: [array([[  0.,   0.],
       [937.,   0.],
       [937., 465.],
       [  0., 465.]], dtype=float32)]

下面我们来看一下现实场景中的识别效果,从摄像头获取图像并进行解析:

#VX公众号: 桔子code / juzicode.com 
import cv2  
print('cv2.__version__:',cv2.__version__)

detect_obj = cv2.wechat_qrcode_WeChatQRCode('detect.prototxt','detect.caffemodel','sr.prototxt','sr.caffemodel')
cap = cv2.VideoCapture(0)
while cap.isOpened():
    ret, img = cap.read()
    if ret is not True:
        print("读取失败退出")
        break      

    res,points = detect_obj.detectAndDecode(img)
    print('res:',res)
    print('points:',points)
    for pos in points:
        color=(0,0,255)
        thick=3
        for p in [(0,1),(1,2),(2,3),(3,0)]:
            start = int(pos[p[0]][0]),int(pos[p[0]][1])
            end = int(pos[p[1]][0]),int(pos[p[1]][1])
            cv2.line(img,start,end,color,thick)
    cv2.imshow('img',img)
    
    #检查按键
    key = cv2.waitKey(200) & 0xff
    if  key == ord('q') or key == ord('Q') :
        break
        
cap.release()
cv2.destroyAllWindows()

opencv微信识别二维码:

opencv-wechat-qrcode

推荐阅读:

模糊照片修复神器GFPGAN

新鲜上架的Python3.10,来个match-case尝尝鲜

你别耍我,0.1+0.2居然不等于0.3?

有了这款神器,什么吃灰文件都统统现形

一行代码深度定制你的专属二维码(amzqr)

以下是一个示例代码,展示了如何在uni-app使用OpenCV的WeChatQRCode库来开发扫码功能: ```vue <template> <view class="container"> <button @tap="scanQRCode">扫码</button> <view>{{ result }}</view> </view> </template> <script> import WeChatQRCode from 'WeChatQRCode' // 导入WeChatQRCode库 export default { data() { return { result: '' // 用于存储扫码结果 } }, methods: { scanQRCode() { WeChatQRCode.scan({ success: (res) => { this.result = res.result // 将扫码结果赋值给result变量 }, fail: (err) => { console.error('扫码失败', err) } }) } } } </script> <style> .container { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; } </style> ``` 在以上示例代码,我们首先导入了WeChatQRCode库。然后,在`data`定义了一个`result`变量,用于存储扫码结果。接着,在`methods`定义了一个`scanQRCode`方法,该方法通过调用WeChatQRCode的`scan`方法来实现扫码功能。当扫码成功时,会将结果赋值给`result`变量。如果扫码失败,则会在控制台输出错误信息。 在模板,我们添加了一个按钮,并为其绑定了`tap`事件,当点击按钮时,会调用`scanQRCode`方法。扫码结果会通过插值表达式`{{ result }}`显示在页面上。 请注意,以上示例代码仅为演示如何使用OpenCV的WeChatQRCode库开发扫码功能,实际使用时需要根据你的项目需求进行适当的调整。另外,确保已正确安装和配置了OpenCV的WeChatQRCode库,并且将其正确引入到uni-app项目
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值