Introduction to QR Code Reader Python
Today we will be developing Barcode and QR code Reader Python. We all know how essential QR Codes have become these days. QR Codes make any information accessible, whether it is a QR Code for any website like a Linkedin profile, Github profile, or a QR Code that contains information. Keeping all these advantages in mind, in this article, we are going to develop a Barcode and QR Code Reader using Python and OpenCV.
This reader will be in two types (1) Image-based Reader and (2) Real-time Reader (Webcam)
For the development of this project of Barcode and QR Code Reader, we will be using a famous Python library named OpenCV and along with it NumPy and Pyzbar. These libraries will eventually help us to build the two types of readers. So let a start with the actual coding of this project.
- Introduction to QR Code Reader Python
- What we are going to develop?
- Image-based Qr code scanner Python
- Complete Code for Qr Code Scanner using Python
- Real-time Python Qr code scanner
- Complete Code for Real-time OpenCV Barcode Reader in Python
- Required Files
- Reference Links
What we are going to develop?
We are going to develop a Python program using which anyone can read a Barcode or Qrcode on their own computer. As we already told you that this program will read Barcodes and QR codes in the two following ways-
In this project development of OpenCV Barcode and QR code reader, at first, we have to install all the required libraries. Use the below-given commands to install them
pip install numpy
pip install opencv-python
pip install pyzbar
Image-based Qr code scanner Python
This type of Barcode and QR Code scanner Python is the simplest one. In this type, the user simply has to provide the image in which there are QR codes. The process is simple, once the user provides the script with the input image, the script will then process the image and will return whatever information is there in the
import cv2 import numpy as np import pyzbar.pyzbar as pyzbar
Reading the input image
image = cv2.imread("new.png")
Here we have used imread() – The imread() method opens the supplied file and loads an image. Use the cv2. imread() function of OpenCV to read an image in Python. Depending on how many color channels are present in the image, imread() produces either a 2D or 3D matrix. A 2D array will do for a binary or grayscale image.
This method produces an empty matrix if the picture cannot be read (due to a missing file, poor permissions, an unsupported or invalid format, etc.). syntax: imread.cv2 (path, flag)
Decoding the Image
decodedObjects = pyzbar.decode(image)
Then, all we have to do is use the decode function we imported and send the image as input in order to decode the barcode in the image.
The output of this function is an array of Decoded class objects. The array’s elements each stand for a QR code that was found. This implies that photos with multiple Qr codes can be used with the library.
for obj in decodedObjects: print("Type:", obj.type) print("Data: ", obj.data, "\n")
Since there is just one Qr code in our image, it is assumed that this array will only have one member. However, for a more reliable solution, we will use a for loop to repeatedly traverse through the array. This way, the code will function whether one, several, or no barcodes are detected.
Additionally, we will print the kind of barcode (type attribute) and the decoded content from the Qr code (data attribute), in the test picture. We have used the obj.data and obj.type for that purpose.
cv2.imshow("Frame", image) cv2.waitKey(0)
A window containing an image is displayed using the cv2. imshow() technique. The window adjusts itself to the size of the image. cv2.imshow(window name, picture) is the syntax.
The Python OpenCV waitkey() function enables users to display a window after a specified number of milliseconds or until a key is pushed. It requires a parameter of time in milliseconds and waits until that amount of time has passed before destroying the window; if the parameter is supplied as 0, it waits until a key is pushed.
Complete Code for Qr Code Scanner using Python
import cv2 import numpy as np import pyzbar.pyzbar as pyzbar image = cv2.imread("add_your_image_here") decodedObjects = pyzbar.decode(image) for obj in decodedObjects: # print("Type: QRCODE") # print("Data: b'www.copyassignment.com'") print("Type:", obj.type) print("Data: ", obj.data, "\n") cv2.imshow("Frame", image) cv2.waitKey(0)
Note: Replace add_your_image_here with your image.
Real-time Python Qr code scanner
You must understand how this process will function before beginning. To prepare your webcam to scan the QR code, you must first launch it and run your Python program. You can use your smartphone to take the QR code image, which you can then display in front of your webcam. It recognizes the QR code that appears on your screen appropriately. Furthermore, this application connects you to a website concealed in the QR code.
We will use the same libraries that we used in Image-based Barcode and QR Code Scanner
Capturing Video and Setting Fonts
cap = cv2.VideoCapture(0) font = cv2.FONT_HERSHEY_PLAIN
With the use of the VideoCapture() function, video is stored in the cap variable. In order to access the webcam, we gave the option 0 to VideoCapture(0). The device index or file name of a video can be found under VideoCapture. To define a Camera, the device index is simply an integer. The first or primary camera is used if we pass 0; the second camera is used if we pass 1, etc. Then, frame by frame, we record the video.
The next line helps in setting up the font. This font will be used to display the hidden information in the QR Code or Barcode.
Decoding and Main Logic
while True: _, frame = cap.read() decodedObjects = pyzbar.decode(frame) for obj in decodedObjects: #print("Data", obj.data) cv2.putText(frame, str(obj.data), (50, 50), font, 2, (255, 0, 0), 3) cv2.imshow("Frame", frame) key = cv2.waitKey(1) if key == 27: break
Line 1: We initialized the While loop with a True condition. Since the video stream of the image is already known to OpenCV to be a collection of images, it will be essential to include the processing inside a While loop.
Line 2: Here we used _ and frame variable that holds the output of cap.read()
- _ is a recognized Python identifier, so it is acceptable to use it as a variable name. It uses a special identifier to retain the outcome of the most recent evaluation. And in our case, it is True/False
- cap.read(): True/False is the result of cap.read(). If the frame is correctly read, True will be returned. So, by looking at this returned value, you can determine when the video ends.
Line 3: All we have to do is use the decode function and send the image as input in order to decode the barcode in the image. The output will be an array.
Line 4 to 6: We use a for loop to repeatedly traverse through the array. This way, the code will function whether one, several, or no barcodes are detected. Then we used a putText() function.
- putText(): To add text to images in Python, use the Opencv putText() function. With the help of OpenCV’s putText() function, we can add text to an image in the specified color, size, family, and placement. The image on which we wish to display the text is the first argument to the putText() function.
Line 7: A window containing an image is displayed using the cv2. imshow() technique.
Line 8: In this case Real-time Barcode and QR Code Scanner we used wait cv2.waitKey(1) whereas in the case of Image-based Barcode and QR Code Scanner we used cv2.waitKey(0)
- waitKey(1): A frame will be shown for 1 millisecond by waitKey(1) before the display is automatically closed.
Line 9 to 10: And finally we used a conditional to break the while loop. In order to do so, we compared the value of the key to 27, and if that satisfies then the while loop is exited.
Complete Code for Real-time OpenCV Barcode Reader in Python
import cv2 import numpy as np import pyzbar.pyzbar as pyzbar cap = cv2.VideoCapture(0) font = cv2.FONT_HERSHEY_PLAIN while True: _, frame = cap.read() decodedObjects = pyzbar.decode(frame) for obj in decodedObjects: #print("Data", obj.data) cv2.putText(frame, str(obj.data), (50, 50), font, 2, (255, 0, 0), 3) cv2.imshow("Frame", frame) key = cv2.waitKey(1) if key == 27: break
QR Code and Barcode used in this project:
We hope this article was helpful to you and was a great source of learning for you. As promised in this article Barcode and QR code Reader using OpenCV we have tried to explain each line of code in detail thus not leaving our fellow readers in doubt. We believe in developing projects and so we came up with an exciting OpenCV project. Try making changes according to your requirement and modify the project accordingly.
Thank you for visiting our website.
- SQLite | CRUD Operations in Python
- Event Management System Project in Python
- Ticket Booking and Management in Python
- Hostel Management System Project in Python
- Sales Management System Project in Python
- Bank Management System Project in C++
- Python Download File from URL | 4 Methods
- Python Programming Examples | Fundamental Programs in Python
- Spell Checker in Python
- Portfolio Management System in Python
- Stickman Game in Python
- Contact Book project in Python
- Loan Management System Project in Python
- Cab Booking System in Python
- Brick Breaker Game in Python
- Tank game in Python
- GUI Piano in Python
- Ludo Game in Python
- Rock Paper Scissors Game in Python
- Snake and Ladder Game in Python
- Puzzle Game in Python
- Medical Store Management System Project in Python
- Creating Dino Game in Python
- Tic Tac Toe Game in Python
- Test Typing Speed using Python App
- MoviePy: Python Video Editing Library
- Scientific Calculator in Python
- GUI To-Do List App in Python Tkinter
- Scientific Calculator in Python using Tkinter
- Website Blocker Using Python