A Real Time Barcode Scanner Python is a graphical representation of data that can be read by machines in real-time. We use the
pyzbar library to make a Real-Time Barcode Reader in Python. pyzbar lets us figure out what the one-dimensional barcode means.
pyzbar can return 3 fields based on the barcode object:
- Type: There are a number of different kinds of barcodes. Which have names like CODE-128, Code-11, CODE-39, etc., that make them stand out. If the symbol that pyzabr finds is a QR code, then that type of QR code is a QR code.
- Data: This is the information that is built into the barcode. Depending on the type of barcode, this information can be in many different formats, such as alphanumeric, numerical, binary, etc.
- Location: This is a list of all the points that are in the code. For barcodes, these are the beginning and end of the lines. And for QRcode, it’s a list of four points that match the four corners of the QR code quad.
Barcode Scanner in Python Project Information’s
|Project Name:||Real Time Barcode Scanner Python|
|Abstract:||A barcode scanner captures and reads the information in a bar code.|
|Language Used:||Python Opencv|
|Type:||Machine Learning Project|
|Developer:||Angel Jude Suarez|
In this article we will learn How To Create a OpenCV Barcode Scanner using Python in Real-Time! So, without further a do let’s begin!
First, we’ll set up the libraries we’ll need for this project, and then we’ll start writing code.
In this step, we will install the
Pillow library is also known as PIL, which stands for Python Image Library.
pip install pillow
OpenCV is a well-known library, especially for projects that involve computer vision.
pip install opencv-python
Pyzbar is a library for Python that lets us read barcodes and QR codes.
pip install pyzbar
This is the step where we write the decoding function to decode the barcodes, which is where most of the fun stuff will happen most of the time.
The decoding function will do three things, which are as follows:
- Seeing and figuring out what the barcode or QR code is that we will show the camera.
- Putting the stored information as text on the barcode/QR code reader that can be read.
- And finally, exporting the information stored as a text file.
Before we write to the function, let’s import the libraries we just installed.
#Importing cv2 and pyzbar libraries import cv2 from pyzbar import pyzbar
Now, let’s add the function to read the given barcode in real-time.
#read barcode function def read_barcodes(frame): barcodes = pyzbar.decode(frame) for barcode in barcodes: x, y, w, h = barcode.rect # 1 barcode_info = barcode.data.decode('utf-8') cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # 2 font = cv2.FONT_HERSHEY_DUPLEX 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 the bounding box of the barcode return frame
Understand The Given Function
- First, we take the information from the detecting barcode or QR code and figure out what it means. And then draw a rectangle around it. This lets us know if our machine found the barcode or QR code.
- The second thing we do is put text on top of the rectangle we just made. The information that can be read will be in the text.
- Third, we’re sending the information to a text file. If you plan to test with more than one barcode or QR code, you should change the name of the document or it will be overwritten.
Main Function of Real Time Barcode Scanner
In this step, we’ll write the application’s main function, which tells it to start working. The computer’s video camera will be turned on by the main function, which will then call the decoding function.
#main function def main(): camera = cv2.VideoCapture(0) ret, frame = camera.read() while ret: ret, frame = camera.read() frame = read_barcodes(frame) cv2.imshow('Real Time Barcode Scanner', frame) if cv2.waitKey(1) & 0xFF == 27: break camera.release() cv2.destroyAllWindows() if __name__ == '__main__': main()
Understand The Main Function
- First, we use OpenCV to turn on the camera on the computer. Depending on the device, you need to change the value from 0 to 1 if you have an external camera.
- Second, we use a while loop to run the decoding function over and over again until the “Esc” key is pressed. If not, the loop won’t stop, which could cause problems.
- In the third step, we turn off the camera that we turned on in the first step. Then we’ll close the window for the application. OpenCV does all the work for us; all we have to do is call the methods.
- Last, we start the program by calling the main function.
And now we have completely demonstrate on how to build the Python Real Time Barcode Scanner in a simple way.
Complete Source Code
Here are the complete source code of the project.
import cv2 from pyzbar import pyzbar def read_barcodes(frame): barcodes = pyzbar.decode(frame) for barcode in barcodes: x, y, w, h = barcode.rect # 1 barcode_info = barcode.data.decode('utf-8') cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # 2 font = cv2.FONT_HERSHEY_DUPLEX 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 the bounding box of the barcode return frame def main(): camera = cv2.VideoCapture(0) ret, frame = camera.read() while ret: ret, frame = camera.read() frame = read_barcodes(frame) cv2.imshow('Real Time Barcode Scanner', frame) if cv2.waitKey(1) & 0xFF == 27: break camera.release() cv2.destroyAllWindows() if __name__ == '__main__': main()
How To Run The Project?
To run the project just follow the steps provided below.
- Step 1: Open The Project Folder
- Step 2: Type CMD In Project Folder Directory
- Step 3: Type The Following Command
Type the following command given below in CMD.
However, If you run the project this will be the output.
After Scanning the barcode the data will store in a text document file named “barcode_result.txt“
Perfect! We have completely demonstrate on how to build a Real Time Barcode Scanner in Python and Detect any Type of Barcode, I hope you learned a lot on my discussion, Thank You.