Building a Barcode/QR code Reader using Python

Simple and hands-on machine learning project with Pyzbar library


Sonsuz Design

3 years ago | 5 min read

In this post, I will show you how to build a barcode and Qr code reader using Python. This is a great machine learning project to get started with computer vision. In my previous posts, I showed how to recognize faces and also how to recognize text in an image using python.

These are great projects to practice your skills. Today, we will something a little more different and that is barcode and QR code reader.

I think Barcodes/QR codes are very cool and interesting because they store information in a different format. The fun part about them is we can’t really tell what they are storing until we scan them. It’s like playing a puzzle game. And one more thing that I like about them is they can be part of the physical world and still connects us to the internet world. Isn’t that really cool?

Table of Contents:

  • Getting Started
  • Libraries
  • Decoding Function
  • Main Function
  • Video Demonstration

Getting Started

If you are wondering how barcode and QR code readers work, let’s do a quick real-life practice. Turn on your phone’s camera and show the featured image of this article. You will see a link show up, it’s very simple to use. Today, we will create our own reader, without losing any time let’s get started!

We will start by installing the libraries that we will need for this project and then we will start programming. For this project, I recommend using a regular code editor instead of a Jupyter notebook.


In this step, we will install the following three libraries: Pillow, OpenCV and Pyzbar. Pillow library is also known as PIL, which stands for Python Image Library. OpenCV is a well-known library, especially when working with computer vision projects. And lastly Pyzbar, a python library that will help us read the barcode and QR codes. Let’s start installing them.


Official documentation can be found here.

pip install Pillow


OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in commercial products.

pip install opencv-python


Installation of Pyzbar library is different depending on the computer you are using. I will show both Mac OS and Windows installation lines. You can also learn more from Pyzbar’s official documentation page.

# Mac OS version
brew install zbar# Windows OS version
pip install pyzbar

Decoding Function

In this step, we write the decoding function, where most of the cool things will be happening. The decoding function will be doing mainly three things, and can be listed as follows:

  • Recognizing and decoding the barcode/QR code that we will be showing to the camera.
  • Adding the stored information as a text on the recognized barcode/QR code.
  • And lastly, exporting the stored information as a text document.

Let’s import the libraries we installed before we write to the function:

#import librariesimport cv2
from pyzbar import pyzbar

Now, let’s write the function. Instead of adding part by part, I will share the whole function with you. Since, indentation matters when writing in python, I don’t want to disorganize things by ruining the structure of the code. I will add my comments below the code.

def read_barcodes(frame):
barcodes = pyzbar.decode(frame)
for barcode in barcodes:
x, y , w, h = barcode.rect #1
barcode_info ='utf-8')
cv2.rectangle(frame, (x, y),(x+w, y+h), (0, 255, 0), 2)

cv2.putText(frame, barcode_info, (x + 6, y - 6), font, 2.0, (255, 255, 255), 1) #3
with open("barcode_result.txt", mode ='w') as file:
file.write("Recognized Barcode:" + barcode_info) return frame

Understanding the function:

  • Firstly, we are decoding the information from the barcode or QR code. And then drawing a rectangle around it. This helps us to see if our machine has detected the barcode/Qr code.
  • Secondly, we are adding text on top of the rectangle that was created. The text will show the decoded information.
  • Thirdly, we are exporting the information into a text document. If you are planning to test with multiple barcodes or QR codes, I recommend changing the document name otherwise it will overwrite.

Main Function

In this step, we will write the main function, where the application is prompt to work. The main function will turn on the video camera of the computer, and the then call the decoding function. Here is the code:

def main(): #1
camera = cv2.VideoCapture(0)
ret, frame = #2
while ret:
ret, frame =
frame = read_barcode(frame)
cv2.imshow('Barcode/QR code reader', frame)
if cv2.waitKey(1) & 0xFF == 27:
break #3
if __name__ == '__main__':

Understanding the function:

  • Firstly, we are turning on the camera of the computer using OpenCV. If you have an external camera, you have to change the value 0 to 1 depending on the device.
  • Secondly, we run a while loop to keep running the decoding function until the “Esc” key is pressed. Otherwise, the loop will not stop and cause some issues.
  • Thirdly, we are releasing the camera that we turned on in the first step. And then we are closing the application window. OpenCV is doing all the work, we just need to call the methods.
  • Lastly, we are calling the main function to trigger the program.

Perfect! We are done with the programming part. Let me show you a quick demonstration to see how it works when we run the program.

Video Demonstration

Testing the program

Congrats!! You have created a program the reads barcodes and QR codes for you. Now, you have an idea of how to use computer vision and artificial intelligence in the real life. Working on hands-on programming projects like this one is the best way to sharpen your coding skills. I would be glad if you learned something new today.

Feel free to contact me if you have any questions while implementing the code.


Created by

Sonsuz Design







Related Articles