当前位置: 首页 > news >正文

学习笔记_在Python中使用微信扫码功能(OpenCV WeChatQRCode)

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

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

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

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

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)

image

 

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

image

 下面的例子是在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]]]] )

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=3for 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()

image

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

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=3for 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()

image

 

image

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

#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=3for 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) & 0xffif  key == ord('q') or key == ord('Q') :breakcap.release()
cv2.destroyAllWindows()

 

http://www.hskmm.com/?act=detail&tid=15797

相关文章:

  • 国标GB28181视频平台EasyCVR如何构建安防监控“中枢神经”?
  • vscode中element-plus组件无属性提示
  • day07
  • minio集群搭建
  • 在AI技术唾手可得的时代,挖掘新需求成为核心竞争力——某知名餐饮菜谱应用需求洞察
  • 英伟达入资 11Labs,黄仁勋:语音 AI 带来情感、共情和联结;Qwen3-TTS-Flash:多语言,多音色,多方言丨日报
  • 深入解析:一文详解回归分析的探索、分析、检验阶段,以Stata和SPSS为例
  • Vue 包依赖总结
  • 笔记_OpenCV4.5.1新增微信QRCode解码功能
  • 数字孪生 + 碳痕追踪:MyEMS 给能源管理装了套 “全链路全景导航”
  • 空间复杂度和时间复杂度
  • 基于IOS26的iOS 内存分析与必要内存界定
  • 破局 “节能不省钱” 悖论:开源 EMS 生态如何让中小企业用 1/3 成本实现能效跃升?
  • iOS 26 性能测试实战,如何评估启动速度、CPUGPU 负载、帧率与系统资源适配(uni-app 与 iOS 原生应用性能方案)
  • P14062 【MX-X21-T7】[IAMOI R5] 若我不曾见过太阳 题解
  • unity确定性帧同步框架
  • 03-堆和栈
  • 视频汇聚平台EasyCVR如何构建智慧农业监控监管系统?
  • 一套自用的git提交规范,可清晰的识别到关联的任务/bug - 实践
  • 撕开厂商锁定黑箱:MyEMS 如何用开源代码夺回能源管理的 “自主控制权”?
  • 继续 Vibe Coding 撸工具:Markdown写作 + 一键发布
  • C造桥与砍树
  • Keil uVision5 MDK 5.42安装教程(支持ARM Cortex全系列开发)
  • 2024 ICPC ECfinal E
  • 从Void到Task<PublishAggregateResult>:一次服务方法返回类型重构的纠结与决策
  • LVGL移植到STM32F4出现无法运行的问题
  • 题目记录(Before NOIP2025 ver)
  • 专业修复sqlserver master 数据库损坏。
  • jenkins job的configure中配置git时 选择的credential为什么不能选择secret认证方式的数据
  • Day21继承