首页 / 二维码 / python实现二维码、条形码识别
python实现二维码、条形码识别
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python实现二维码、条形码识别,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2487字,纯文字阅读大概需要4分钟。
内容图文
![python实现二维码、条形码识别](/upload/InfoBanner/zyjiaocheng/1189/779cd2dc722e43639eef2c7bfa4cee15.jpg)
环境:
- python 3.7
- Win7
依赖包安装:
pip install pillow pip install opencv-python pip install opencv-contrib-python pip install numpy pip instal pyzbar
首先使用cv2打开摄像头进行测试
import numpy as np import cv2 capture = cv2.VideoCapture(0,cv2.CAP_DSHOW) capture.set(3,640) capture.set(4,480) while True: # 读取摄像头中的图像,ok为是否读取成功的判断参数 ret,img = capture.read() cv2.imshow(‘frame‘, img) k = cv2.waitKey(1) if k == 27: # ‘ESC‘关闭 break
如果看到屏幕出现一个窗口,上面显示着你的摄像头所拍摄到的画面说明测试成功
然后就直接上代码:
#written by DY #http://dyblog.tk #e-mail:duyun888888@qq.com import cv2 from PIL import Image, ImageDraw, ImageFont import csv import pyzbar.pyzbar as pyzbar import numpy import time #-----------------time---------- time1 = (time.strftime("%Y!%m@%d(%H*%M)%S`", time.localtime())) time2 = time1.replace(‘!‘, ‘年‘) time3 = time2.replace(‘@‘, ‘月‘) time4 = time3.replace(‘(‘, ‘日‘) time5 = time4.replace(‘*‘, ‘时‘) time6 = time5.replace(‘)‘, ‘分‘) timenow = time6.replace(‘`‘, ‘秒‘) #-----------------time done---- barcodeData1 = ‘‘ found = set() capture = cv2.VideoCapture(0,cv2.CAP_DSHOW) Files = "qrcode+barcode.dat"#新建一个日志,方便查阅(不需要可以删除) while(1): ret,frame = capture.read() test = pyzbar.decode(frame) gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) barcodes = pyzbar.decode(gray) for barcode in barcodes: (x, y, w, h) = barcode.rect cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 0), 2) barcodeData = barcode.data.decode("utf-8") barcodeType = barcode.type if barcodeData==‘‘ or barcodeData!=barcodeData1: barcodeData1 = barcodeData #======================可以不需要,这是在图片上进行标注,里面采用了字体文件============================== img_PIL = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) font = ImageFont.truetype(‘arialbd.ttf‘, 25)#前面是字体文件,后面是字号 fillColor = (0,255,0) position = (x, y-25) strl = barcodeData draw = ImageDraw.Draw(img_PIL) draw.text(position, strl,font=font,fill=fillColor) img_PIL.save(‘Identification_results.jpg‘,‘jpeg‘)#保存一张标记过的图片 #==================================================== print("Recognize result>>> type: {0} content: {1}".format(barcodeType, barcodeData)) else: pass if barcodeType not in found or barcodeData not in found: with open(Files,‘a+‘) as w: csv_write = csv.writer(w) date = [‘类型:‘+barcodeType+‘ 识别结果:‘+barcodeData+‘ 时间:‘+timenow] csv_write.writerow(date) found.add(barcodeData) cv2.imshow(‘qrcode+barcode‘,frame) k = cv2.waitKey(1) if k == 27: break #written by DY #http://dyblog.tk #e-mail:duyun888888@qq.com
效果如图所示:
原文:https://www.cnblogs.com/dy8888/p/13275587.html
内容总结
以上是互联网集市为您收集整理的python实现二维码、条形码识别全部内容,希望文章能够帮你解决python实现二维码、条形码识别所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。