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.
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. Pillow
Official documentation can be found here.
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.
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.
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:
Let’s import the libraries we installed before we write to the function:
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.
Understanding the 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:
4. Main Function
Understanding the 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:
Understanding the function:
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.
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.
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.