[Python]使用pyzbar 與pylibdmtx讀取條碼

[Python]使用pyzbar 與pylibdmtx讀取條碼

更新於 發佈於 閱讀時間約 1 分鐘

本文主要使用pyzbar 與pylibdmtx來讀取條碼,並用靜態方法將這兩個套件的讀碼功能包裝起來,因應不同需求,調用相對應的方法來讀取QR code,一維條碼,Data Matrix。最後再將讀到的條碼資料與框選條碼位子於原圖上。


靜態方法可參考我下方文章

[Python基礎]裝飾器staticmethod 定義靜態方法

結果圖

raw-image
raw-image
raw-image

以下是使用 pyzbar 來讀取一維條碼和 QR Code,並使用 pylibdmtx.pylibdmtx 來讀取 Datamatrix 的內容。


確保你已經安裝了所需的 Python 套件

pip install opencv-python 
pip install pyzbar
pip install pylibdmtx

程式範例

將條碼讀取功能進行封裝,並根據不同條碼類型調用不同函式。

使用 pyzbar 來讀取一維條碼和 QR Code

使用 pylibdmtx.pylibdmtx 來讀取 Datamatrix

import cv2
import numpy as np
from pyzbar.pyzbar import decode as pyzbar_decode
import pylibdmtx.pylibdmtx

class BarcodeReader:

@staticmethod
def read_img(path):
img = cv2.imdecode(np.fromfile(file=path, dtype=np.uint8), cv2.IMREAD_COLOR)
return img

@staticmethod
def read_qr_and_1d(image):
"""使用 pyzbar 來讀取 QR Code 和一維條碼"""
barcodes = pyzbar_decode(image)

for barcode in barcodes:
x, y, w, h = barcode.rect
# 畫框顯示條碼位置
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

# 條碼資訊
barcode_data = barcode.data.decode('utf-8')

# 顯示條碼資訊在影像上
text = f'{barcode_data}'
cv2.putText(image, text, (x, y-3), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

return barcode_data,image

@staticmethod
def read_datamatrix(image):
"""使用 pylibdmtx 來讀取 Datamatrix 條碼"""
barcodes = pylibdmtx.pylibdmtx.decode(image)
results =[]
if barcodes:
results = barcodes[0][0].decode('utf-8')
for barcode in barcodes:
x, y, w, h = barcode.rect.left, barcode.rect.top, barcode.rect.width, barcode.rect.height
# 畫框顯示條碼位置
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)

# 顯示條碼資訊在影像上
text = f'Datamatrix: {results} '
cv2.putText(image, results, (x, y-3), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)

return results,image

@staticmethod
def show_image(image):
"""顯示處理後的影像"""
cv2.imshow('Barcode Reader', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 使用範例
if __name__ == "__main__":
# 調用read_img讀圖
QR_Code_img = BarcodeReader.read_img('PATH')
Data_Matrix_Code_img = BarcodeReader.read_img('PATH')
Code_128_img = BarcodeReader.read_img('PATH')

# 調用read_qr_and_1d讀取 QR 和一維條碼
qr_code_results, QR_Code_res_img = BarcodeReader.read_qr_and_1d(QR_Code_img)
print("QR Code : ", qr_code_results)

Code_128_1d_results, Data_Matrix_Code_res_img = BarcodeReader.read_qr_and_1d(Code_128_img)
print("1D Barcodes: ", Code_128_1d_results)

# 用read_datamatrix讀取 Datamatrix
Data_Matrix_results, Data_Matrix_res_img = BarcodeReader.read_datamatrix(Data_Matrix_Code_img)
print("Datamatrix: ", Data_Matrix_results)

# 顯示處理後的影像
show_image(QR_Code_res_img)
show_image(Data_Matrix_Code_res_img)
show_image(Data_Matrix_res_img)


套件網址與介紹






avatar-img
螃蟹_crab的沙龍
138會員
245內容數
本業是影像辨識軟體開發,閒暇時間進修AI相關內容,將學習到的內容寫成文章分享。
留言
avatar-img
留言分享你的想法!
螃蟹_crab的沙龍 的其他內容
在影像處理或機器學習的應用中,我們常常需要將影片逐幀擷取出來,進一步進行辨識或分析。 本篇教學將示範如何使用 Python + OpenCV 來: ✅ 讀取 MP4 影片 測試影片可由下方超連結下載,從file-examples.com下載 file-examples.com 是一個 免費提
本文將指導你如何修改現有的 OpenCV 程式碼,使其利用 CUDA 加速進行深度神經網絡(DNN)推理,如超分辨率圖像放大任務。這將顯著提升運行速度,特別是在高分辨率圖像處理中。 在CMake上這選項要開,才可支援DNN模組。 CMake編譯OpenCV教學文 連結 [OpenCV][Py
OpenCV 提供了專門針對 CUDA 優化的模組,這些模組使用 cv2.cuda 命名空間,並且可以直接使用 GPU 進行加速。,cv2.cuda 模塊需要在 OpenCV 編譯時啟用 CUDA 支援才能使用。 本文主要比較經過CMAKE重新編譯OpenCV使其支援Cuda,原OpenCV只支援
在影像處理或機器學習的應用中,我們常常需要將影片逐幀擷取出來,進一步進行辨識或分析。 本篇教學將示範如何使用 Python + OpenCV 來: ✅ 讀取 MP4 影片 測試影片可由下方超連結下載,從file-examples.com下載 file-examples.com 是一個 免費提
本文將指導你如何修改現有的 OpenCV 程式碼,使其利用 CUDA 加速進行深度神經網絡(DNN)推理,如超分辨率圖像放大任務。這將顯著提升運行速度,特別是在高分辨率圖像處理中。 在CMake上這選項要開,才可支援DNN模組。 CMake編譯OpenCV教學文 連結 [OpenCV][Py
OpenCV 提供了專門針對 CUDA 優化的模組,這些模組使用 cv2.cuda 命名空間,並且可以直接使用 GPU 進行加速。,cv2.cuda 模塊需要在 OpenCV 編譯時啟用 CUDA 支援才能使用。 本文主要比較經過CMAKE重新編譯OpenCV使其支援Cuda,原OpenCV只支援