今天,我实现了一个很有趣的demo,它可以在视频里找到并解析二维码,然后把解析的内容实时在屏幕上显示出来。
然后我们直入主题,首先你得确保你装了opencv,python,zbar等环境。然后这个教程对于学过opencv的人可能更好理解,但是没学过也无妨,到时候也可以直接用。
比如我的电脑上的环境是opencv2.4.x,python2.7,和最新的zbar,在Ubuntu 12.12的系统下运行的
假设你的opencv已经安装好了,那么我们就可以安装zbar
你可以先更新一下
sudo apt-get update
然后在输入
sudo apt-get install python-zbar
如果环境装好了,我们就可以接着下一步操作了。
首先让我们来实现找到在图片里面找到二维码的功能
先新建一个python文件叫做;simple_barcode_detection.py
代码如下,这定义了一个函数,实现从一副图片里面找出二维码的位置
我们要检测的二维码的图片
# 对上述的梯度图采用用9x9的核进行平均模糊,这是有利于降噪的
#然后进行二值化处理,要么是255(白)要么是0(黑)
blurred = cv2.blur(gradient, (9, 9))(_, thresh) = cv2.threshold(blurred, 225, 255, cv2.THRESH_BINARY)#模糊与二值化处理后,看起来是这个样子,
#我们发现还是存在一些小斑点,这时我们可以使用opencv里面的腐蚀和膨胀来处理他们,来去除白色的斑点
closed = cv2.erode(closed, None, iterations = 4)closed = cv2.dilate(closed, None, iterations = 4)#这时候的效果看起来是这样的最终结果
好了,上面的解释里面有中文,可能python解释的时候会通不过,我下面直接给出代码
完成了上述的工作,我们就完成了二维码和条形码的定位,接下去实现视频里面二维码的解析
你可以新建一个python文件,barcode_vid.py
解析二维码我们需要用zbar这个模块和PIL,PIL在python里面装好了
我们先导入模块
# import the necessary packagesimport simple_barcode_detectionimport cv2import numpy as npimport zbarfrom PIL import Image#接下去是创建一个扫描器,他可以解析二维码的内容
# create a readerscanner = zbar.ImageScanner()# configure the readerscanner.parse_config('enable')#设置屏幕显示字体font=cv2.FONT_HERSHEY_SIMPLEX#启用摄像头camera=cv2.VideoCapture(0)#接下去是一个大的while循环while True:#得到当前的帧# grab the current frame(grabbed, frame) = camera.read()#检测视频是否到底了,如果检测视频文件里面的二维码或条形码用这个,如果开启摄像头就无所谓了# check to see if we have reached the end of the# videoif not grabbed:break调用刚才我们建的那个函数来查找二维码返回二维码的位置# detect the barcode in the imagebox = simple_barcode_detection.detect(frame)if box != None:#这下面的3步得到扫描区域,扫描区域要比检测出来的位置要大min=np.min(box,axis=0)max=np.max(box,axis=0)roi=frame[min[1]-10:max[1]+10,min[0]-10:max[0]+10]#把区域里的二维码传换成RGB,并把它转换成pil里面的图像,因为zbar得调用pil里面的图像,而不能用opencv的图像roi=cv2.cvtColor(roi,cv2.COLOR_BGR2RGB)pil= Image.fromarray(frame).convert('L')width, height = pil.sizeraw = pil.tostring()#把图像装换成数据zarimage = zbar.Image(width, height, 'Y800', raw)#扫描器进行扫描scanner.scan(zarimage)#得到结果for symbol in zarimage: # 对结果进行一些有用的处理print 'decoded', symbol.type, 'symbol', '"%s"' %symbol.datacv2.drawContours(frame, [box], -1, (0, 255, 0), 2)#把解析的内容放到视频上cv2.putText(frame,symbol.data,(20,100),font,1,(0,255,0),4)# show the frame and record if the user presses a keycv2.imshow("Frame", frame)key = cv2.waitKey(1) & 0xFF# if the 'q' key is pressed, stop the loopif key == ord("q"):break# cleanup the camera and close any open windowscamera.release()cv2.destroyAllWindows()CSDN不能上传视频,我下面传一下图片
下面还是上源码
# import the necessary packagesimport simple_barcode_detectionimport cv2import numpy as npimport zbarfrom PIL import Image# create a readerscanner = zbar.ImageScanner()# configure the readerscanner.parse_config('enable')font=cv2.FONT_HERSHEY_SIMPLEXcamera=cv2.VideoCapture(0)while True:# grab the current frame(grabbed, frame) = camera.read()# check to see if we have reached the end of the# videoif not grabbed:break# detect the barcode in the imagebox = simple_barcode_detection.detect(frame)if box != None:min=np.min(box,axis=0)max=np.max(box,axis=0)roi=frame[min[1]-10:max[1]+10,min[0]-10:max[0]+10]print roi.shaperoi=cv2.cvtColor(roi,cv2.COLOR_BGR2RGB)pil= Image.fromarray(frame).convert('L')width, height = pil.sizeraw = pil.tostring()# wrap image datazarimage = zbar.Image(width, height, 'Y800', raw)# scan the image for barcodesscanner.scan(zarimage)# extract resultsfor symbol in zarimage: # do something useful with resultsprint 'decoded', symbol.type, 'symbol', '"%s"' %symbol.datacv2.drawContours(frame, [box], -1, (0, 255, 0), 2)cv2.putText(frame,symbol.data,(20,100),font,1,(0,255,0),4)# if a barcode was found, draw a bounding box on the frame# show the frame and record if the user presses a keycv2.imshow("Frame", frame)key = cv2.waitKey(1) & 0xFF# if the 'q' key is pressed, stop the loopif key == ord("q"):break# cleanup the camera and close any open windowscamera.release()cv2.destroyAllWindows()总结
以上所述是小编给大家介绍的使用python写的opencv实时监测和解析二维码和条形码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!