Detect and Read Barcodes with OpenCV in Python
Step 1: Create a Virtual Environment
This is the first step in which we will create a virtual environment by using the following commands in your terminal:
python -m venv venv
.\venv\Scripts\activate
Step 2: Installation
At first, we will install OpenCV, Matplotlib, and pyzbar by using the following command:
pip install opencv-python matplotlib pyzbar
Step 3: Import Libraries
In this step, we have imported the following libraries:
- cv2: This is the OpenCV library used for computer vision tasks.
- decode from pyzbar.pyzbar: This function is used to decode the barcode data from an image.
- matplotlib.pyplot as plt: This library is used for visualizing the image with detected barcodes.
import cv2
from pyzbar.pyzbar import decode
import matplotlib.pyplot as plt
Step 4: Define Function to Detect and Decode Barcodes
In this step, the function detect_and_decode_barcode
aims to identify and decode barcodes present in an input image. It begins by converting the image to grayscale and then proceeds to detect barcodes using the decode
function. For each detected barcode, it extracts the data and type, draws a rectangle around it, and annotates the image with the extracted information. Finally, it displays the annotated image with the detected barcodes using Matplotlib.
def detect_and_decode_barcode(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect barcodes in the grayscale image
barcodes = decode(gray)
# Loop over detected barcodes
for barcode in barcodes:
# Extract barcode data and type
barcode_data = barcode.data.decode("utf-8")
barcode_type = barcode.type
# Print barcode data and type
print("Barcode Data:", barcode_data)
print("Barcode Type:", barcode_type)
# Draw a rectangle around the barcode
(x, y, w, h) = barcode.rect
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
# Put barcode data and type on the image
cv2.putText(image, f"{barcode_data} ({barcode_type})",
(x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
# Convert image from BGR to RGB (Matplotlib uses RGB)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(image_rgb)
plt.axis('off')
plt.show()
Step 5: Read Input Image and Call the Function
In this step, we are reading the input image and calling the function.
# Read input image
image = cv2.imread("your path:/barcode_image.png")
detect_and_decode_barcode(image)
Step 6: Run the Program
To run the program, type the following command:
python main.py
Barcode Used
Output:
Barcode Data: Wikipedia
Barcode Type: CODE128
Detect and Read Barcodes with OpenCV in Python
A barcode is a graphical representation of data that is machine-readable. It consists of parallel lines or rectangles of varying widths and spacings, along with specific patterns, that encode information. Barcodes are widely used for automatic identification and tracking of products, assets, inventory, and more. In this article, we will see how we can detect and read barcodes with OpenCV in Python.