Build your own barcode and QRcode scanner using python

Learn how to create Barcode or QRcode scanner in python

Pruthvi Hingu
Towards Data Science

--

Introduction about project

Let’s create a program that scans the QR codes and Barcodes from an image. For this program, we need three packages, which are OpenCV, NumPy, and pyzbar. Most of the python programmers are familiar with OpenCV and Numpy libraries. OpenCV is an open-source computer vision and machine learning library. It is a useful library for image processing. We are using this library in our project for processing each frame from a video captured by a device. We are using Numpy here because pyzbar works with OpenCV / numpy ndarrays. Whereas, pyzbar library is used to read barcodes and QR codes from a given image. It supports EAN-13/UPC-A, UPC-E, EAN-8, Code 128, Code 39, Interleaved 2 of 5, and QR Code. So, this is a small introduction to our project and libraries. Install the required libraries using the following commands.

For OpenCV

pip install opencv-python

For pyzbar

pip install pyzbar

For Numpy

pip install numpy

Let’s start coding

Import all the required libraries for our program.

import cv2
import numpy as np
from pyzbar.pyzbar import decode

Capture the video from the device camera.

Now, let’s create a decoder function that decodes barcode and QR code from a given image.

This function takes an image, then identifies the QRcode and barcode from the image, and decodes the value of it. Here barcode is a list of barcode and QRcode objects recognized by the decode function. Each object contains rect, polygon, data, type, etc attributes. rect and polygon attributes give the position of barcode and QRcode.

Check the final program below.

Let’s check the output.

Output

You can download the source code from Github.

Thanks for reading this piece. Hope to see you again in the next article! Please connect with me through LinkedIn:

www.linkedin.com/in/pruthvi-hingu-174521194

--

--