Secondly, we run a while loop to keep running the decoding function until the “Esc” key is pressed.If you have an external camera, you have to change the value 0 to 1 depending on the device. Firstly, we are turning on the camera of the computer using OpenCV.Here is the code: def main(): #1 camera = cv2.VideoCapture(0) ret, frame = camera.read() #2 while ret: ret, frame = camera.read() frame = read_barcodes(frame) cv2.imshow('Barcode/QR code reader', frame) if cv2.waitKey(1) & 0xFF = 27: break #3 camera.release() cv2.destroyAllWindows() #4 if _name_ = '_main_': main() The main function will turn on the video camera of the computer, and the then call the decoding function. In this step, we will write the main function, where the application is prompt to work. If you are planning to test with multiple barcodes or QR codes, I recommend changing the document name otherwise it will overwrite. Thirdly, we are exporting the information into a text document.The text will show the decoded information. Secondly, we are adding text on top of the rectangle that was created.This helps us to see if our machine has detected the barcode/Qr code. Firstly, we are decoding the information from the barcode or QR code.def read_barcodes(frame): barcodes = code(frame) for barcode in barcodes: x, y, w, h = barcode.rect #1 barcode_info = ('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 frame Since, indentation matters when writing in python, I don’t want to disorganize things by ruining the structure of the code. Instead of adding part by part, I will share the whole function with you. Let’s import the libraries we installed before we write to the function: #import libraries import cv2 from pyzbar import pyzbar And lastly, exporting the stored information as a text document.Adding the stored information as a text on the recognized barcode/QR code.Recognizing and decoding the barcode/QR code that we will be showing to the camera.The decoding function will be doing mainly three things, and can be listed as follows: In this step, we write the decoding function, where most of the cool things will be happening. # Mac OS version brew install zbar # Windows OS version pip install pyzbar You can also learn more from Pyzbar’s official documentation page. I will show both Mac OS and Windows installation lines. Installation of Pyzbar library is different depending on the computer you are using. Reference: pip install opencv-python Pyzbar OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in commercial products. OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. Official documentation can be found here. And lastly Pyzbar, a python library that will help us read the barcode and QR codes. OpenCV is a well-known library, especially when working with computer vision projects. Pillow library is also known as PIL, which stands for Python Image Library. In this step, we will install the following three libraries: Pillow, OpenCV and Pyzbar.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |