在Python中使用微信扫码功能(OpenCV WeChatQRCode) |
您所在的位置:网站首页 › 微信识别二维码接口是什么 › 在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()检测和识别: #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) |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |