关于二维码生成与识别,网上有很多资料,生成是比较容易的python相关的库也很多,但是识别才是技术难度比较高的,这里指的是复杂背景下的二维码快速识别;所谓复杂背景就是说二维码占画幅比例很小、存在扭曲、变形、模糊,比如随手拍的照片上二维码,这种情况下往往很随意的角度,给二维码识别带来不小困难。这两天查了不少资料也对比了很多开源实现,但没有找到可以媲美微信扫一扫识别效果的方案,不得不说这种成熟的商用产品即便是一个很小的细分功能也是下了不少功夫的。
我也研究了下,这里给出我自己的初步方案。
基于yolov5网络的二维码定位 ===> 二维码提取 ===> 二维码矫正===> 二维码图像增强 ===> pyzbar识别
我面对的场景是像下面这样的图片:
这种图片如果直接用zbar识别的话,不仅耗时较长而且识别率极低;但是如果直接形态学处理干扰又太多,因此这里先用目标检测网络yolov5做定位。
- yolo的训练这里就不再赘述了,训练数据是从网络上找的各种二维码图片
识别效果很好几乎100%,需要训练数据或者训练好的代码及模型的私信联系我。以下着重介绍定位后的预处理。定位并提取后的二维码如下:
- 尽管已经提取了二维码,但直接用pyzbar识别效果非常差,识别率不到10%,因此考虑对这些图像进行矫正。
以下面这张提取后的二维码为例:
首先将图像放缩到统一大小,然后识别二维码边界,最后进行仿射变换矫正图像,代码如下:
import cv2
import imutils
from skimage import measure
import numpy as np
image = cv2.imread('image.png')
image = cv2.resize(image, (600, 600))
height, width = image.shape[:2]
#size = (int(width * 0.25), int(height * 0.25))
#shrink = cv2.resize(image, size, interpolation=cv2.INTER_AREA)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imwrite('gray.jpg',gray)
ret2, image_binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
ret, binary = cv2.threshold(gray, ret2 * 0.85, 255, cv2.THRESH_BINARY)
#ret, binary = cv2.threshold(gray, 135, 255, cv2.THRESH_BINARY)
cv2.imwrite('binary.jpg',binary)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (80, 80))
iOpen = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
iClose = cv2.morphologyEx(iOpen, cv2.MORPH_CLOSE, kernel)
cv2.imwrite('iClose.jpg',iClose)
# cv2.imwrite('tempcolse.jpg',iClose)
img = 255 - iClose
cv2.imwrite('img.jpg',img)
def Get_cnt(edged):
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[1] if imutils.is_cv3() else cnts[0]
docCnt = None
if len(cnts) > 0:
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
if len(approx) == 4:
docCnt = approx
break
return docCnt
def order_points(pts):
rect = np.zeros((4, 2), dtype="float32")
s = pts.sum(axis=1)
rect[0] = pts[np.argmin(s)]
rect[2] = pts[np.argmax(s)]
diff = np.diff(pts, axis=1)
rect[1] = pts[np.argmin(diff)]
rect[3] = pts[np.argmax(diff)]
return rect
def four_point_transform(simage, pts,gap=50):
# print(pts)
rect = order_points(pts)
(tl, tr, br, bl) = rect
tl[0] = tl[0]-gap
tl[1] = tl[1]-gap
tr[0] = tr[0]+gap
tr[1] = tr[1]-gap
br[0] = br[0]+gap
br[1] = br[1]+gap
bl[0] = bl[0]-gap
bl[1] = bl[1]+gap
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
maxWidth = max(int(widthA), int(widthB))
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
maxHeight = max(int(heightA), int(heightB))
dst = np.array([
[0, 0],
[maxWidth - 1, 0],
[maxWidth - 1, maxHeight - 1],
[0, maxHeight - 1]], dtype="float32")
M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
warped = cv2.copyMakeBorder(warped,50,50,50,50, cv2.BORDER_CONSTANT,value=[255,255,255])
return warped
#69,64 555,551
#sx,sy = _black_edges(img)
#print(sx,sy)
ss = Get_cnt(img)
print(ss)
warped = four_point_transform(image,ss.reshape(4, 2),gap=35)#这个gap阈值控制下仿射变换的余量,避免有些二维码变换后识别不出来
cv2.imwrite('warped.jpg',warped)
- 接下来,就是对二维码做增强然后调用pyzbar做识别:
import pyzbar.pyzbar as pyzbar
from PIL import Image,ImageEnhance
from pyzbar.pyzbar import ZBarSymbol
import qreader
image2 = 'warped.jpg'
img = Image.open(image2)
#img = img.resize((600,600),Image.ANTIALIAS)
img = ImageEnhance.Brightness(img).enhance(2.0)#增加亮度
#
#img = ImageEnhance.Sharpness(img).enhance(1.5)#锐利化
#
#img = ImageEnhance.Contrast(img).enhance(2.0)#增加对比度
#
img = img.convert('L')#灰度化
img.save('cc.png')
barcodes = pyzbar.decode(img)
print(barcodes)
for barcode in barcodes:
barcodeData = barcode.data.decode("utf-8")
print(barcodeData)
识别结果如下:
[Decoded(data=b'WMWHSE6:2000041942,', type='QRCODE', rect=Rect(left=73, top=73, width=459, height=453), polygon=[Point(x=73, y=82), Point(x=78, y=526), Point(x=532, y=517), Point(x=521, y=73)])]
WMWHSE6:2000041942,