how to make a barcode and Qr code reader with Python and Machine Learning. This is a great machine learning task to get started with computer vision. Barcodes and QR codes are very cool and interesting because they store information in a different format. The fun part about them is that we can’t tell what they are storing until we analyze them. It’s like playing a puzzle game. And another thing I love about them is that they can be part of the physical world and still connect us to the internet world.
How does the barcode and QR code reader work?
If you are wondering how barcode and QR code readers work, let’s do a little practical exercise. Turn on your phone camera and view the featured image from this item.
You will see a link appear, it is very easy to use. Today you will learn how to create your barcode and QRcode reader with Python and Machine Learning, without wasting time, let’s get started.
I will start by installing the libraries we will need for this project and then I will start with the coding part. For this task of creating a barcode and QRcode reader with Python, I recommend using a regular code editor like VScode or Pycharm.
The first step is to install the following three libraries: Pillow, OpenCV and Pyzbar. Pillow is the extension of PIL, which stands for Python Image Library. OpenCV is a well-known library, especially when working with computer vision tasks. And the last library we need is Pyzbar, a python library that will help us read barcode and QR codes. You can easily install all the libraries using the pip command.
Building Barcode and QR code Reader with Python and Machine Learning
Now the next step is to write the decode function, where most of the cool stuff will happen. The decode function will mainly do three things and can be listed as follows:
- Recognize and decode the barcode / QR code that we are going to show to the camera.
- Added information stored as text on recognized barcode / QR code.
- And finally, export the stored information as a text document.
- Let’s import the libraries we installed before writing to the function:
Now let’s define the decoding function:
def read_barcodes(frame): barcodes = pyzbar.decode(frame) for barcode in barcodes: x, y , w, h = barcode.rect barcode_info = barcode.data.decode('utf-8') cv2.rectangle(frame, (x, y),(x+w, y+h), (0, 255, 0), 2) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(frame, barcode_info, (x + 6, y - 6), font, 2.0, (255, 255, 255), 1) with open("barcode_result.txt", mode ='w') as file: file.write("Recognized Barcode:" + barcode_info) return frame
the above function to understand what I did:
- First, we decode the barcode or QR code information. And then draw a rectangle around it. It helps us to see if our machine detected the barcode / Qr code.
- Second, we add text above the rectangle that has been created. The text will display the decoded information.
- Third, we export the information to a text document.
- Now the next step is to write the main function for building a Barcode and QR code reader with Python. Let’s create our main function:
def main(): camera = cv2.VideoCapture(0) ret, frame = camera.read() while ret: ret, frame = camera.read() frame = read_barcode(frame) cv2.imshow('Barcode/QR code reader', frame) if cv2.waitKey(1) & 0xFF == 27: break camera.release() cv2.destroyAllWindows() if __name__ == '__main__': main()
Now let’s go through the main function above to understand what I did:
- First of all, we turn on the computer camera using OpenCV. If you have an external camera, you need to change the value 0 to 1 depending on the device.
- Second, we run a while loop to continue performing the decode function until the “Esc” key is pressed. Otherwise, the loop will not stop and cause problems.
- Third, we launch the camera that we turned on in the first step. And then we close the application window. OpenCV does all the work, just call the methods.
- Finally, we call the main function to trigger the program.
- Now you can easily run the code and scan any barcode and QR code by showing the code to the camera of your laptop.
NOTE – In order to run the project of Python, you have to download Python IDLE or Pycharam. In the video I have explained how to download and install it.
- SEO Practices Everyone Should Follow SEO Rules
- Complete Top SEO Checklist
- Yoast Seo Premium 15.2 Nulled – WordPress SEO Plugin
- Top 50+ SEO Interview Questions
- What is a Backlink? How to Get More Backlinks
- TOP 20+ ANDROID PROJECT WITH SOURCE CODE
- Top 20+ interview Programs
If you found this post useful, don’t forget to share this with your friends, and if you have any query feel free to comment it in the comment section.
Thank you 🙂 Keep Learning !