如何在Apple M1 Mac上通过Pip安装Python Barcode SDK

Dynamsoft Barcode Reader SDK一款多功能的条码读取控件,只需要几行代码就可以将条码读取功能嵌入到Web或桌面应用程序。这可以节省数月的开发时间和成本。能支持多种图像文件格式以及从摄像机或扫描仪获取的DIB格式。使用Dynamsoft Barcode Reader SDK,你可以创建强大且实用的条形码扫描仪软件,以满足你的业务需求。

点击下载Dynamsoft Barcode Reader最新版

有人可能对基于Intel的Python应用程序能否很好地运行感到好奇Apple M1 Mac。我最近用Dynamsoft Python条形码SDK进行了测试,该SDK使用本机x86_64库和CPython。事实证明,使用pip该工具安装轮包并运行我的Python条码读取器应用程序没有任何问题。

安装Pip和Python条形码转盘软件包

当您打开终端应用程序并Python3第一次输入时,将弹出一个提示对话框,用于安装命令行开发人员工具。

在这里插入图片描述

单击Install并等待一段时间以安装相关工具。之后,您可以python3在终端中成功运行。

下一步是安装pip:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py 

准备好点子之后,您可以尝试安装Dynamsoft Python条形码SDK:

python3 -m pip install dbr

如果看到失败消息,请不要感到惊讶。
在这里插入图片描述

您正在尝试在ARM64体系结构上安装x86_64 wheel软件包。但是,不要因为Rosetta 2可以使其工作而沮丧。让我们看一下Python3架构:

% file $(which python3)
/usr/bin/python3: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [arm64e:Mach-O 64-bit executable arm64e]
/usr/bin/python3 (for architecture x86_64): Mach-O 64-bit executable x86_64
/usr/bin/python3 (for architecture arm64e): Mach-O 64-bit executable arm64e

如您所见,Python 3是通用应用程序,支持x86_64。因此,我们可以通过指定arch安装x86_64 wheel软件包:

arch -x86_64 $(which python3) -m pip install dbr

恭喜你!您已经安装了Dynamsoft Python条形码SDK。从现在开始,您可以开始使用网络摄像头创建条形码扫描应用程序。

在Apple M1 Mac上构建基于网络摄像头的Python条形码阅读器

安装OpenCV-Python以捕获网络摄像头视频流:

arch -x86_64 $(which python3) -m pip opencv-python

打开相机并实时显示视频流:

import cv2 as cv
cap = cv.VideoCapture(0)
while True:
    _ret, frame = cap.read()

    if not _ret:
        break
        
    cv.imshow('BarcodeReader', frame)

    ch = cv.waitKey(1)
    # ESC
    if ch == 27:
        break

由于扫描条形码是一项占用大量CPU的任务,因此我们应该在Python进程而不是Python线程中运行它:

from dbr import *
from multiprocessing import Process, Queue

def process_barcode_frame(license, frameQueue, resultQueue):
    # Create Dynamsoft Barcode Reader
    reader = BarcodeReader()
    # Apply for a trial license: https://www.dynamsoft.com/customer/license/trialLicense
    reader.init_license(license)
    settings = reader.get_runtime_settings()
    settings.max_algorithm_thread_count = 1
    reader.update_runtime_settings(settings)

    while True:
        results = None

        try:
            frame = frameQueue.get(False, 10)
            if type(frame) is str:
                break
        except:
            continue

        try:
            frameHeight, frameWidth, channel = frame.shape[:3]
            results = reader.decode_buffer_manually(np.array(frame).tobytes(), frameWidth, frameHeight, frame.strides[0], EnumImagePixelFormat.IPF_RGB_888)
        except BarcodeReaderError as error:
            print(error)

        try:
            resultQueue.put(results, False, 10)
        except:
            pass

barcodeScanning = Process(target=process_barcode_frame, args=(license, frameQueue, resultQueue))
barcodeScanning.start()

注意:您还需要有效的SDK许可证。
在主要过程中,创建两个队列以连续发送图像帧和接收条形码扫描结果:

frameQueue = Queue(size)
resultQueue = Queue(size)

while True:
    _ret, frame = cap.read()

    if not _ret:
        break

    try:
        results = resultQueue.get(False, 10)
    except:
        pass
        
    cv.imshow('BarcodeReader', frame)

    # Append a frame to the frame queue
    try:
        frameQueue.put(frame.copy(), False, 10)
    except:
        pass

    ch = cv.waitKey(1)
    # ESC
    if ch == 27:
        break

现在,我们可以测试Python条形码程序:

arch -x86_64 $(which python3) barcode_scanning.py license.txt

还有一件事,由于相机权限的限制,您可能无法运行该应用程序。除了终端应用程序,您还可以安装iTerm2来请求摄像头访问。
在这里插入图片描述

最后,我们可以在Apple M1 Mac上进行条形码扫描。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/RoffeyYang/article/details/114024628