如何建立適用於 Windows、Linux 和 macOS 的 Python 條碼掃描器

DDD
發布: 2024-10-22 13:24:03
原創
760 人瀏覽過

條碼掃描已成為從零售、物流到醫療保健等各行業的必備工具。在桌面平台上,它可以快速捕獲和處理訊息,無需手動輸入數據​​,從而節省時間並減少錯誤。在本教學中,我們將透過建置適用於WindowsLinuxPython 條碼掃描器,繼續探索Dynamsoft Capture Vision SDK的功能和macOS

macOS 上的 Python 條碼掃描器演示

先決條件

  • Dynamsoft Capture Vision 試用授權:取得 Dynamsoft Capture Vision SDK 的 30 天試用授權金鑰。

  • Python 套件:使用以下指令安裝所需的 Python 套件:

    pip install dynamsoft-capture-vision-bundle opencv-python
    
    登入後複製
    登入後複製

    這些包有什麼用?

    • dynamsoft-capture-vision-bundle 是一個適用於 Python 的 Dynamsoft Capture Vision SDK。
    • opencv-python 會擷取相機幀並顯示處理後的影像結果。

從靜態影像中讀取條碼

由於 Dynamsoft Capture Vision SDK 是一個整合了各種映像處理任務的統一框架,因此我們可以透過將 PresetTemplate 名稱傳遞給 capture() 方法來輕鬆切換映像處理模式。

Dynamsoft Capture Vision SDK 內建模板

以下程式碼片段顯示了 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。

用於條碼檢測的Python程式碼

參考先前的文件偵測與機讀區辨識範例,可以使用以下程式碼從靜態影像讀取條碼:

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 替換為您的有效許可證金鑰。

使用多條碼影像測試 Python 條碼閱讀器

從單一影像解碼多個條碼是零售和物流中的常見用例。下圖包含多個不同格式的條碼:

How to Build a Python Barcode Scanner for Windows, Linux, and macOS

使用網路攝影機進行即時多條碼偵測

當從映像檔讀取條碼時,我們在主執行緒中呼叫 capture() 方法。然而,為了處理來自網路攝影機的即時視訊串流,需要採用不同的方法來避免阻塞主線程。 Dynamsoft Capture Vision SDK 提供了一種內建機制,用於處理即時視訊幀並在本機 C 工作執行緒上非同步處理它們。若要實現此目的,請擴充 ImageSourceAdapter 和 CapturedResultReceiver 類別來分別處理影像資料和擷取的結果,然後呼叫 start_capturing() 方法開始處理視訊串流。

pip install dynamsoft-capture-vision-bundle opencv-python
登入後複製
登入後複製

說明

  • FrameFetcher 類別實作 ImageSourceAdapter 接口,將幀資料送入內建緩衝區。
  • MyCapturedResultReceiver 類別實作了 CapturedResultReceiver 介面。 on_captured_result_received 方法在本機 C 工作執行緒上執行,並將 CapturedResult 物件傳送至主執行緒,並將它們儲存在執行緒安全性佇列中以供進一步使用。
  • CapturedResult 包含多個 CapturedResultItem 物件。 CRIT_BARCODE 類型表示已識別的條碼資料。

在 macOS 上測試 Python 條碼掃描儀

How to Build a Python Barcode Scanner for Windows, Linux, and macOS

原始碼

https://github.com/yushulx/python-barcode-qrcode-sdk/tree/main/examples/official/10.x

以上是如何建立適用於 Windows、Linux 和 macOS 的 Python 條碼掃描器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!