條碼掃描已成為從零售、物流到醫療保健等各行業的必備工具。在桌面平台上,它可以快速捕獲和處理訊息,無需手動輸入數據,從而節省時間並減少錯誤。在本教學中,我們將透過建置適用於Windows、Linux 的Python 條碼掃描器,繼續探索Dynamsoft Capture Vision SDK的功能和macOS。
Dynamsoft Capture Vision 試用授權:取得 Dynamsoft Capture Vision SDK 的 30 天試用授權金鑰。
Python 套件:使用以下指令安裝所需的 Python 套件:
pip install dynamsoft-capture-vision-bundle opencv-python
這些包有什麼用?
由於 Dynamsoft Capture Vision SDK 是一個整合了各種映像處理任務的統一框架,因此我們可以透過將 PresetTemplate 名稱傳遞給 capture() 方法來輕鬆切換映像處理模式。
以下程式碼片段顯示了 Dynamsoft Capture Vision SDK 中的內建 PresetTemplate 枚舉:
class EnumPresetTemplate(Enum): PT_DEFAULT = _DynamsoftCaptureVisionRouter.getPT_DEFAULT() PT_READ_BARCODES = _DynamsoftCaptureVisionRouter.getPT_READ_BARCODES() PT_RECOGNIZE_TEXT_LINES = _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_TEXT_LINES() PT_DETECT_DOCUMENT_BOUNDARIES = ( _DynamsoftCaptureVisionRouter.getPT_DETECT_DOCUMENT_BOUNDARIES() ) PT_DETECT_AND_NORMALIZE_DOCUMENT = ( _DynamsoftCaptureVisionRouter.getPT_DETECT_AND_NORMALIZE_DOCUMENT() ) PT_NORMALIZE_DOCUMENT = _DynamsoftCaptureVisionRouter.getPT_NORMALIZE_DOCUMENT() PT_READ_BARCODES_SPEED_FIRST = ( _DynamsoftCaptureVisionRouter.getPT_READ_BARCODES_SPEED_FIRST() ) PT_READ_BARCODES_READ_RATE_FIRST = ( _DynamsoftCaptureVisionRouter.getPT_READ_BARCODES_READ_RATE_FIRST() ) PT_READ_SINGLE_BARCODE = _DynamsoftCaptureVisionRouter.getPT_READ_SINGLE_BARCODE() PT_RECOGNIZE_NUMBERS = _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_NUMBERS() PT_RECOGNIZE_LETTERS = _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_LETTERS() PT_RECOGNIZE_NUMBERS_AND_LETTERS = ( _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_NUMBERS_AND_LETTERS() ) PT_RECOGNIZE_NUMBERS_AND_UPPERCASE_LETTERS = ( _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_NUMBERS_AND_UPPERCASE_LETTERS() ) PT_RECOGNIZE_UPPERCASE_LETTERS = ( _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_UPPERCASE_LETTERS() )
PT_DEFAULT 範本支援多種任務,包括文件偵測、機讀區辨識和條碼偵測。若要專門最佳化條碼偵測的效能,請將範本設定為 EnumPresetTemplate.PT_READ_BARCODES.value。
參考先前的文件偵測與機讀區辨識範例,可以使用以下程式碼從靜態影像讀取條碼:
import sys from dynamsoft_capture_vision_bundle import * import os import cv2 import numpy as np from utils import * if __name__ == '__main__': print("**********************************************************") print("Welcome to Dynamsoft Capture Vision - Barcode Sample") print("**********************************************************") error_code, error_message = LicenseManager.init_license( "LICENSE-KEY") if error_code != EnumErrorCode.EC_OK and error_code != EnumErrorCode.EC_LICENSE_CACHE_USED: print("License initialization failed: ErrorCode:", error_code, ", ErrorString:", error_message) else: cvr_instance = CaptureVisionRouter() while (True): image_path = input( ">> Input your image full path:\n" ">> 'Enter' for sample image or 'Q'/'q' to quit\n" ).strip('\'"') if image_path.lower() == "q": sys.exit(0) if image_path == "": image_path = "../../../images/multi.png" if not os.path.exists(image_path): print("The image path does not exist.") continue result = cvr_instance.capture( image_path, EnumPresetTemplate.PT_READ_BARCODES.value) if result.get_error_code() != EnumErrorCode.EC_OK: print("Error:", result.get_error_code(), result.get_error_string()) else: cv_image = cv2.imread(image_path) items = result.get_items() print('Found {} barcodes.'.format(len(items))) for item in items: format_type = item.get_format() text = item.get_text() print("Barcode Format:", format_type) print("Barcode Text:", text) location = item.get_location() x1 = location.points[0].x y1 = location.points[0].y x2 = location.points[1].x y2 = location.points[1].y x3 = location.points[2].x y3 = location.points[2].y x4 = location.points[3].x y4 = location.points[3].y del location cv2.drawContours( cv_image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2) cv2.putText(cv_image, text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) cv2.imshow( "Original Image with Detected Barcodes", cv_image) cv2.waitKey(0) cv2.destroyAllWindows() input("Press Enter to quit...")
注意:將 LICENSE-KEY 替換為您的有效許可證金鑰。
從單一影像解碼多個條碼是零售和物流中的常見用例。下圖包含多個不同格式的條碼:
當從映像檔讀取條碼時,我們在主執行緒中呼叫 capture() 方法。然而,為了處理來自網路攝影機的即時視訊串流,需要採用不同的方法來避免阻塞主線程。 Dynamsoft Capture Vision SDK 提供了一種內建機制,用於處理即時視訊幀並在本機 C 工作執行緒上非同步處理它們。若要實現此目的,請擴充 ImageSourceAdapter 和 CapturedResultReceiver 類別來分別處理影像資料和擷取的結果,然後呼叫 start_capturing() 方法開始處理視訊串流。
pip install dynamsoft-capture-vision-bundle opencv-python
說明
https://github.com/yushulx/python-barcode-qrcode-sdk/tree/main/examples/official/10.x
以上是如何建立適用於 Windows、Linux 和 macOS 的 Python 條碼掃描器的詳細內容。更多資訊請關注PHP中文網其他相關文章!