How to install Python Barcode SDK via Pip on Apple M1 Mac

Dynamsoft Barcode Reader SDK is a multi-functional barcode reading control that can embed the barcode reading function into web or desktop applications with just a few lines of code. This can save months of development time and cost. It can support a variety of image file formats and DIB formats obtained from cameras or scanners. Using Dynamsoft Barcode Reader SDK, you can create powerful and practical barcode scanner software to meet your business needs.

Click to download the latest version of Dynamsoft Barcode Reader

Some people may be curious about whether Intel-based Python applications can run well Apple M1 Mac. I recently tested it with Dynamsoft Python Barcode SDK, which uses the native x86_64 library and CPython. It turns out that there is no problem using pip to install the wheel package and run my Python barcode reader application.

Install Pip and Python barcode turntable software package

When you open the terminal application and enter Python3 for the first time, a prompt dialog box will pop up to install the command line developer tools.

Insert picture description here

Click Install and wait for a while to install related tools. After that, you can run python3 successfully in the terminal.

The next step is to install pip:

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

After preparing your ideas, you can try to install Dynamsoft Python Barcode SDK:

python3 -m pip install dbr

Don't be surprised if you see a failure message.
Insert picture description here

You are trying to install the x86_64 wheel package on the ARM64 architecture. However, don't be frustrated because Rosetta 2 can make it work. Let's take a look at the Python3 architecture:

% 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

As you can see, Python 3 is a general-purpose application and supports x86_64. Therefore, we can install the x86_64 wheel package by specifying arch:

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

Congratulations! You have installed Dynamsoft Python Barcode SDK. From now on, you can start using the webcam to create barcode scanning applications.

Build a webcam-based Python barcode reader on Apple M1 Mac

Install OpenCV-Python to capture the webcam video stream:

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

Turn on the camera and display the video stream in real time:

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

Since scanning barcodes is a CPU-intensive task, we should run it in a Python process instead of a Python thread:

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()

Note: You also need a valid SDK license.
In the main process, two queues are created to continuously send image frames and receive barcode scanning results:

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

Now, we can test the Python barcode program:

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

One more thing, due to camera permission restrictions, you may not be able to run the application. In addition to the terminal application, you can also install iTerm2 to request camera access.
Insert picture description here

Finally, we can perform barcode scanning on Apple M1 Mac.
Insert picture description here

Guess you like

Origin blog.csdn.net/RoffeyYang/article/details/114024628