Building a Barcode/QR Code Reader Using Python

Building a Barcode/QR Code Reader Using Python

Behic Guven 14/10/2020 6
Building a Barcode/QR code Reader using Python

Barcodes/QR codes are 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? In this article, you will discover how to build a barcode and QR code reader using Python. 

1. 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. 

2. Libraries

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. Pillow

Official documentation can be found here.

pip_install_Pillow.png
OpenCV

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.

Reference: https://opencv.org

pip_install_opencv-python.png

Pyzbar

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.


Pyzbar.png

3. 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_libraries.png 


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_barcodesframe.png

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.

4. 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:

main_function.png

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.

5. Video Demonstration

Video_Demonstration.png

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.

Follow my blog and youtube channel to stay inspired. 

Share this article

Leave your comments

Post comment as a guest

0
terms and condition.
  • Morgan Alvarez

    Thanks for the tips.. Easy to follow and straight to the point !!

  • Dean S

    It took me some extra minutes to understand. I am a newbie.. So helpful...

  • Dean S

    Thanks for sharing this awesome guide & video at the end! This helped me a lot considering I'm new.

  • Mark Fenelon

    Finally understanding it!!!

  • Tony P

    Can’t thank you enough!!

  • Craig Baines

    Excellent post

Share this article

Behic Guven

Tech Expert

Behic Guven is a computer scientist with a minor in mathematics. His main focus is in deep learning and artificial intelligence fields. He runs a personal blog with around 600 followers. Besides blogging, he is also making online tutorial videos on Youtube. Other than programming, he enjoys literature, photography and art. He is a nature lover. You can find him at biking trails over the weekends. 

   

Latest Articles

View all
  • Science
  • Technology
  • Companies
  • Environment
  • Global Economy
  • Finance
  • Politics
  • Society