When I run the script, it shows each image and prints the type and data of it, press any key and you'll get the next image, here is my output: detected barcode: Decoded(data=b'0036000291452', type='EAN13', rect=Rect(left=124, top=58, width=965, height=812), polygon=)ĭetected barcode: Decoded(data=b'Wikipedia', type='CODE128', rect=Rect(left=593, top=4, width=0, height=294), polygon=)ĭetected barcode: Decoded(data=b'43770929851162', type='I25', rect=Rect(left=62, top=0, width=694, height=180), polygon=) Note that this will also detect QR codes, and that's fine, but for more accurate results, I suggest you check the dedicated tutorial for detecting and generating qr codes in Python. On each file, we load it using cv2.imread() function, and use the previously discussed decode() function to decode the barcodes and then we show the actual image.
In my current directory, I have barcode1.png, barcode2.png, and barcode3.png, which are all example images of a scanned barcode, I used glob so I can get all these images as a list and iterate over them. # decode detected barcodes & get the image Now let's use these functions for our example images: if _name_ = "_main_": I preferred the rectangle version.įinally, it returns the image that contains the drawn barcodes. This function takes the decoded object we just saw, and the image itself, it draws a rectangle around the barcode using cv2.rectangle() function, or you can uncomment the other version of the function drawing the polygon using cv2.line() function, the choice is yours. # uncomment above and comment below if you want to draw a polygon and not a rectangle # image = cv2.line(image, decoded.polygon, decoded.polygon, color=(0, 255, 0), thickness=5) This brings us to the next function that we used, draw_barcode(): def draw_barcode(decoded, image): So code() function returns the data containing the barcode, the type of barcode, as well as the location points as a rectangle and a polygon.
To make things clear, the following is how each obj looked like if we print it: Decoded(data=b'43770929851162', type='I25', rect=Rect(left=62, top=0, width=694, height=180), polygon=) We then iterate over all detected barcodes and draw a rectangle around the barcode and prints the type and the data of the barcode. I have wrapped every functionality into a function, the first function we gonna discuss is the following: def decode(image):ĭecode() function takes an image as a numpy array, and uses code() that is responsible for decoding all barcodes from a single image and returns a bunch of useful information about each barcode detected. I have few images to test with, you can use any image you want from the internet or your own disk, but you can get my test images in this directory.
Once you have these installed, open up a new Python file and import them: from pyzbar import pyzbar
To get started, we need to install few libraries: pip3 install pyzbar opencv-python
Related: How to Extract Frames from Video in Python. Today, we see barcodes everywhere, especially in products in supermarkets.īarcodes can be read by an optical barcode scanner, but in this tutorial, we will make a script in Python that is able to read and decode barcodes, as well as a drawing where they're located in a given image. Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.Ī barcode is a method of representing data in a visual and machine-readable form, it consists of bars and spaces.