This article describes how to detect and read barcodes with OpenCV in Python.

  • cv2.barcode is included in the OpenCV contrib module
  • Super Resolution Model
  • Detect and read barcodes from an image
  • Detect and read barcodes from camera video

See the following article on how to detect and read QR codes instead of barcodes.

  • Detect and read QR codes with OpenCV in Python

You can also use ZBar (pyzbar). Although not thoroughly verified, ZBar seems to have better detection accuracy.

  • Detect and read barcodes and QR codes with ZBar in Python

The version of OpenCV used in the sample code is 4.6.0.

cv2.barcode is included in the OpenCV contrib module

The cv2.barcode to detect and read barcodes is included in the contrib module (as of version 4.6.0).

  • OpenCV: Bar code Recognition
  • OpenCV: cv::barcode::BarcodeDetector Class Reference

In the output of cv2.getBuildInformation(), barcode must be included in To be built of OpenCV modules.

  • Check OpenCV Build Information: getBuildInformation()

For example, on macOS, if you installed OpenCV with Homebrew, the contrib module should be included, but if you installed opencv-python with pip, it might not be included. You need to install OpenCV with pip install opencv-contrib-python.

  • opencv-python · PyPI

Super Resolution Model

The official tutorial introduces the Super Resolution Model.

  • OpenCV: Bar code Recognition

You can download and use sr.prototxt, sr.caffemodel from the following repository.

  • WeChatCV/opencv_3rdparty: OpenCV – 3rdparty
See also  How Much Does Ideal Image Hair Removal Cost

Specify the path of the downloaded file to cv2.barcode.BarcodeDetector(). If omitted, no model is used.

I may be doing something wrong, but I did not notice any difference in accuracy with or without the model in my environment. It is not used in the sample code below.

Detect and read barcodes from an image

The following barcode image is used as an example.

Barcode sample

Create an instance of cv2.barcode.BarcodeDetector and execute detectAndDecode(). Although detect() to only detect and decode() to decode based on the detected coordinates are also provided, they are not mentioned here.

Note that the results may differ depending on the version and environment.

retval is True if a barcode is detected and False if none is detected.

decoded_info is a tuple whose elements are strings stored in barcodes. If it can be detected but not decoded, it is an empty string ”.

decoded_type is a tuple whose elements are numbers representing barcode types.

  • OpenCV: Barcode detecting and decoding methods

points is a numpy.ndarray representing the coordinates of the four corners of the detected QR Code.

Draw frames for the detected barcode and superimpose the decoded text.

Detect and decode barcode with OpenCV

Detect and read barcodes from camera video

The following is a sample code that detects and reads barcodes from real-time camera video.

See the following article for more information on the handling of videos in OpenCV.

  • Capture video from camera/file with OpenCV in Python

Press q on the keyboard to exit.

In an actual application, the while loop would be terminated by break when the decoded_info string is obtained, and the application would move on to operation using the string.