Opencv detects QR codes and barcodes

1 Create a simple QR code in excel

Using excel can realize the production of QR codes, but only English and digital types can be realized. The steps are as follows:

  1. Enter content in any cell

  2. Find the development tool in the tab - Insert - click the bottom right corner of the ActiveX control.

  3. In the pop-up window, slide down to select Microsoft BarCode Control 16.0, and click OK.

  4. In any area, press and hold the left mouse button, drag the mouse to draw a rectangle of a suitable size, and release the mouse to obtain a barcode.

  5. Right-click the barcode, select Properties, click Customize on the right "...", the style in the pop-up window, select the last "11 - QR Code", and click OK.

  6. Go back to the properties, find "LinledCell", and enter the cell position with content in front

  7. get QR code

2 Recognizing QR codes and barcodes

2.1 Related Libraries

Install the libraries used

! pip install --user opencv-contrib-python
! pip install --user pyzbar

Import the library used

import cv2
import numpy as np 
from pyzbar.pyzbar import decode

2.2 decode decoding

Use the decode function to decode the picture of the QR code, and you will see the following parameters in the result. Among them, data represents the content of the two-dimensional code; type represents the type of barcode or two-dimensional code; rect represents the information of the two-dimensional code outline; the position of the polygon two-dimensional code outline is rotated counterclockwise from the upper left corner.

img = cv2.imread('code.png')
result = decode(img)
print(result)

The result is:

[Decoded(data=b’1230V’, type=‘QRCODE’, rect=Rect(left=34, top=33, width=193, height=187), polygon=[Point(x=34, y=33), Point(x=34, y=220), Point(x=227, y=220), Point(x=227, y=33)], quality=1, orientation=‘UP’)]

Use the loop to obtain the decoded information separately:

for barCode in result:
    print(barCode.data)
    print(barCode.rect)
    print(barCode.polygon)

The result is:

b’1230V’

Rect(left=34, top=33, width=193, height=187)
[Point(x=34, y=33), Point(x=34, y=220), Point(x=227, y=220), Point(x=227, y=33)]

It is found that "b'168Qr'" will appear, and b means that it is in bytes. If you want to remove this "b", you need to decode it.

for barCode in result:
    print(barCode.data)
    myData = barCode.data.decode('utf-8')
    print(myData)

The result of the operation is as follows:

b’1230V’

1230V

Conclusion: Decoding succeeded

2.3 Circle the location of the QR code

After decoding, the location of the QR code and the content of the QR code are displayed on the picture.

According to the return value of the decoded polygon, frame the QR code.

for barCode in result:
    myData = barCode.data.decode('utf-8')
pts = np.array([barCode.polygon], np.int32)     # 创建一个int32数据类型的二维码轮廓数组
pts = pts.reshape((-1, 1, 2))        # 自适应维度大小
cv2.polylines(img, [pts], True, (255, 0, 255), 5)

cv2.polylines():
cv2.polylines(img, pts, isClosed, color, thickness)
Among them, pts: polyline vertex array; isClosed: whether it is a closed polyline (polygon)

According to the decoded rect return value, determine the position where the content of the QR code is placed.

pts2 = barCode.rect
cv2.putText(img, myData, (pts2[0], pts2[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 255), 2)

cv.putText(img, text, org, fontFace,fontScale,color[, thickness[, lineType[, bottomLeftOrigin]]])

Among them, the parameters are: picture, added text, coordinates of the upper left corner, font, font size, color, font thickness

2.4 Determine whether the QR code is authorized

First create a text file in the current folder, and enter some authorized QR code content in it.

with open('myDataList.txt') as f:
    myDataList = f.read().splitlines()
    print(myDataList)

According to the content of the two-dimensional code to determine whether to authorize. If the content of the QR code is in this text file, then output "Authorized", and the line of the box QR code is green; if the content of the QR code is not in this text file, then output "Un-Authorized", and the box The lines of the QR code are red.

for barCode in result:
    myData = barCode.data.decode('utf-8')

    if myData in myDataList:
        myOutPut = "Authorized"
        myColor = (0, 255, 0)
    else:
        myOutPut = "Un-Authorized"
        myColor = (0, 0, 255)

    pts = np.array([barCode.polygon], np.int32)  # 创建一个int32数据类型的二维码轮廓数组
    pts = pts.reshape((-1, 1, 2))  # 自适应维度大小
    cv2.polylines(img, [pts], True, myColor, 5)
    pts2 = barCode.rect
    cv2.putText(img, myData, (pts2[0], pts2[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.9, myColor, 2)
    cv2.imshow('22', img)

The realization result is:

insert image description here

3 complete code

3.1 Using pictures for recognition

import cv2
import numpy as np
from pyzbar.pyzbar import decode

img = cv2.imread('code.png')
result = decode(img)

with open('myDataList.txt') as f:
    myDataList = f.read().splitlines()
    print(myDataList)

for barCode in result:
    myData = barCode.data.decode('utf-8')

    if myData in myDataList:
        myOutPut = "Authorized"
        myColor = (0, 255, 0)
    else:
        myOutPut = "Un-Authorized"
        myColor = (0, 0, 255)

    pts = np.array([barCode.polygon], np.int32)  # 创建一个int32数据类型的二维码轮廓数组
    pts = pts.reshape((-1, 1, 2))  # 自适应维度大小
    cv2.polylines(img, [pts], True, myColor, 5)
    pts2 = barCode.rect
    cv2.putText(img, myData, (pts2[0], pts2[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.9, myColor, 2)
    cv2.imshow('22', img)
cv2.waitKey(0)

3.2 Real-time recognition using camera

import cv2
import numpy as np
from pyzbar.pyzbar import decode
 
# 导入摄像头
cap = cv2.VideoCapture(0)
cap.set(3, 320) #摄像头的宽度
cap.set(4, 320) #摄像头的高度
cap.set(10, 100) #摄像头的亮度
 
with open('myDataList.txt') as f:
    myDataList = f.read().splitlines()
 
while True:
    success, img = cap.read()
    for barcode in decode(img):
        myData = barcode.data.decode('utf-8')
        print(myData)
 
        if myData in myDataList:
            myOutPut = "Authorized"
            myColor = (0, 255, 0)
        else:
            myOutPut = "Un-Authorized"
            myColor = (0, 0, 255)
 
        pts = np.array([barcode.polygon], np.int32)
        pts = pts.reshape((-1, 1, 2))
        cv2.polylines(img, [pts], True, myColor, 5)
        pts2 = barcode.rect
        cv2.putText(img, myOutPut, (pts2[0], pts2[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.9, myColor, 2)
    cv2.imshow("Result",img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
IMPLEX, 0.9, myColor, 2)
    cv2.imshow("Result",img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

4 Summary

Two-dimensional code detection and recognition is an important task in the field of computer vision, which aims to detect, locate and analyze two-dimensional code information from images or videos.
In the process of realizing two-dimensional code detection and recognition, we can use open source tool libraries, such as OpenCV and ZBar, to use technologies such as image processing, feature extraction, and classifier training.

Guess you like

Origin blog.csdn.net/apple_52030329/article/details/131312846