In this tutorial, we will use an example to show you how to detect and decode barcode in python opencv.
1.Install python pyzbar library
pip install pyzbar
In order to decode barcode, we need install python pyzbar library.
2.Read barcode image
import cv2 from pyzbar.pyzbar import decode image = cv2.imread('C:/Users/N/Desktop/barcode.png')
3.Use decode() method to decode barcode
detectedBarcodes = decode(image) for barcode in detectedBarcodes: (x, y, w, h) = barcode.rect cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 5) print(barcode.data) print(barcode.type)
Here barcode.data is the content of barcode.
4.Display barcode image in opencv
cv2.imshow("Image", image) cv2.waitKey(0) cv2.destroyAllWindows()