Real-time Barcode Detection with Python, OpenCV, and Pyzbar

This article demonstrates how to use Python's OpenCV and Pyzbar libraries to capture video from a webcam, decode barcodes in real time, display the results, and save captured frames, providing a practical guide for implementing barcode recognition in a retail checkout scenario.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Real-time Barcode Detection with Python, OpenCV, and Pyzbar

In the previous article we generated barcodes; this tutorial shows how a supermarket checkout can recognize them using Python.

First, we open the webcam with OpenCV and define a decode function that uses pyzbar to locate barcodes, draw bounding boxes, and overlay the decoded text and type on the image.

# coding=utf-8
# mac 需要通过命令终端安装zbar
import cv2
import pyzbar.pyzbar as pyzbar

img_path="/Users/XXX/Downloads/test-imgs/"

def decode(image):
    barcodes = pyzbar.decode(image)
    for barcode in barcodes:
        (x, y, w, h) = barcode.rect
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
        barcodeData = barcode.data.decode("utf-8")
        barcodeType = barcode.type
        text = "Text:" + barcodeData
        text2 = "Type:" + barcodeType
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(image, text, (30, 30), font, 0.8, (0, 255, 0), 2)
        cv2.putText(image, text2, (30, 80), font, 0.8, (0, 255, 0), 2)
    return image

Next, the camera function captures frames in a loop, applies decode, resizes the result, displays it, and exits when the user presses q. After the loop the final frame is saved to disk.

def camera():
    camera = cv2.VideoCapture(0)
    while True:
        ret, img = camera.read()
        img = decode(img)
        img = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_CUBIC)
        cv2.imshow("camera", img)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    camera.release()
    cv2.destroyAllWindows()
    cv2.imwrite(img_path+"test01.jpg", img)

if __name__ == '__main__':
    camera()

Finally, a separate snippet demonstrates how to read a saved image, convert it to RGB, and decode any barcodes it contains, printing the extracted text.

import cv2
import pyzbar.pyzbar as pyzbar

image = cv2.imread("/Users/XXX/Downloads/test-imgs.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
texts = pyzbar.decode(gray)
for text in texts:
    tt = text.data.decode("utf-8")
    print(tt)

These examples provide a complete, runnable guide for real‑time barcode recognition that can be integrated with a hardware scanner to match stored barcodes and retrieve product prices.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Computer VisionPythonOpenCVbarcodePyzbar
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.