OpenCV project QR code/barcode detection

         Without further ado, let’s show the effect first!

        Today Franpper brings you barcode and QR code recognition. Before starting the recognition, first generate the test images needed today.

1. Generate barcodes through the barcode library

        The Barcode library is a library for Python that makes generating barcodes simple. It supports multiple barcode formats, including common UPC, EAN, ISBN and QR codes.

        The method of generating barcode is as follows:

from barcode.writer import ImageWriter
import barcode

code_save_dir = r'E:\pythonProject\OpenCV\opencv_proj\qrbartest'
barcode_path = os.path.join(code_save_dir, 'barcode')

# 创建条形码格式对象
EAN = barcode.get_barcode_class('code39')  # Barcode库支持多种条形码格式,根据需要选择适合的格式

message = "6920231218220"  # 条形码内容

# 创建条形码对象
ean = EAN(message, writer=ImageWriter())

# 保存条形码图片,并且返回路径
ean.save(barcode_path)

The generated barcode is as follows: 

2. Generate QR code through qrcode library

        qrcode is a Python library for generating QR codes. It is simple and easy to use, can quickly generate standard QR codes, and also supports adding custom colors and embedding images.

code_save_dir = r'E:\pythonProject\OpenCV\opencv_proj\qrbartest'

qrcode_path = os.path.join(code_save_dir, 'qrcode.png')

qr_image = qrcode.make('https://franpper.blog.csdn.net/')
qr_image.save(qrcode_path)

The generated QR code is as follows:

Resize the generated QR code and splice it horizontally with the barcode


bar_image = cv2.imread(os.path.join(code_save_dir, 'barcode.png'))
bar_h, bar_w, _ = bar_image.shape
qr_image = cv2.imread(os.path.join(code_save_dir, 'qrcode.png'))


resize_qr = cv2.resize(qr_image, (bar_h, bar_h))

merge_image = cv2.hconcat([resize_qr, bar_image])

The merged result is as follows:

 

3. Use the Pyzbar library for identification

The GitHub homepage of pyzbar is as follows

GitHub - NaturalHistoryMuseum/pyzbar: Read one-dimensional barcodes and QR codes from Python 2 and 3.Read one-dimensional barcodes and QR codes from Python 2 and 3. - GitHub - NaturalHistoryMuseum/pyzbar: Read one-dimensional barcodes and QR codes from Python 2 and 3.icon-default.png?t=N7T8https://github.com/NaturalHistoryMuseum/pyzbar根据官方的介绍

The specific usage methods are as follows:

for code in decode(merge_image):
    data = code.data.decode('utf-8')  # 转为UTF-8 编码
    pts = np.array([code.polygon], np.int32)  # code.polygon 包含了识别到的条形码或二维码的多边形顶点坐标,将这些坐标转换为 NumPy 数组
    print(pts)
    pts = pts.reshape((-1, 1, 2))
    cv2.polylines(merge_image, [pts], True, (0, 255, 0), 5)  # 绘制外框
    code_box = code.rect
    # 将二维码或者条形码的的内容写在图中
    cv2.putText(merge_image, data, (code_box[0], code_box[3]), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)

cv2.imshow('result', merge_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

The output image is as follows: 

4. Use camera for identification

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


cap = cv2.VideoCapture(1)
cap.set(3, 640)
cap.set(4, 480)

while True:
    success, frame = cap.read()
    if success:
        for code in decode(frame):
            data = code.data.decode('utf-8')

            pts = np.array([code.polygon], np.int32)
            print(pts)
            pts = pts.reshape((-1, 1, 2))

            cv2.polylines(frame, [pts], True, (0, 255, 0), 5)

            code_box = code.rect
            cv2.putText(frame, data, (code_box[0], code_box[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
        cv2.imshow('result', frame)
        cv2.waitKey(1)


        The effect after recognition is as shown at the beginning of Franpper and will not be shown here.

        That’s about it for the generation and recognition of barcodes and QR codes. Let’s try it together!

 

Guess you like

Origin blog.csdn.net/weixin_58283091/article/details/134843134