Quantcast
Channel: Barcode – Dynamsoft Developers
Viewing all articles
Browse latest Browse all 145

How to Run and Debug Python Barcode App in Docker Container

$
0
0

When I was using CPython to create the Python barcode extension with Dynamsoft Barcode Reader, I had to take concern for the compatibility of Python versions. However, I’m reluctant to install all Python versions in my operating system. To test the compatibility of my Python barcode app in Windows, I picked Docker container as the workaround.

Creating Docker Image with Python Barcode SDK

Install Docker desktop for Windows.

I don’t have Python 3.8 installed, so I pulled a Docker image that contains Python 3.8 from Docker hub and run it instantly:

docker run -it --rm python:3.8 bash

The command above will automatically trigger the download if there’s no target docker image existed.

docker python 3.8

To build a new Docker image that contains the Dynamsoft Barcode Reader SDK, create a simple Dockerfile:

FROM python:3.8

RUN pip install dbr

Build the Docker image:

docker build --rm -t yushulx/dynamsoft-barcode-reader:python-3.8 .

Run a container to test whether Dynamsoft Barcode Reader for Python can work:

docker run -it --rm yushulx/dynamsoft-barcode-reader:python-3.8
Docker container with Python barcode SDK

If there’s no problem, publish the Docker image to Docker hub :

docker login
docker push yushulx/dynamsoft-barcode-reader:python-3.8

If you want to test the image, please visit https://hub.docker.com/repository/docker/yushulx/dynamsoft-barcode-reader.

Mounting Local Folder to Docker Container

I’m going to mount a Windows folder to the Linux container and run a Python script using Python 3.8.

Open the settings of the Docker desktop and select the shared drive:

settings for shared drive

Run the following command to mount the Windows folder to the Linux container:

docker run -it --rm -v d:/code/docker:/dynamsoft yushulx/dynamsoft-barcode-reader:python-3.8  bash
mount windows folder to linux folder

Debugging Python Code with Visual Studio Code Remote-Containers

I like to use the Visual Studio Code to write and debug Python code.

Install Docker extension for VSCode.

vscode docker extension

Press F1 to run the “Attach to Running Container” option.

vscode remote container

Open the project folder.

Docker shared folder
python project

I can now edit the Python file in VSCode. To debug the code, install the Python debugger for the container first.

vscode Python debugger

Then you can press F5 to debug the code remotely.

debugging Python code
Python barcode result

Here is the full Python barcode detection code:

import os

from dbr import DynamsoftBarcodeReader
dbr = DynamsoftBarcodeReader()

def InitLicense(license):
    dbr.InitLicense(license)

def DecodeFile(fileName):
    try:
        results = dbr.DecodeFile(fileName)
        textResults = results["TextResults"]
        resultsLength = len(textResults)
        print("count: " + str(resultsLength))
        if resultsLength != 0:
            for textResult in textResults:
                print(textResult["BarcodeFormatString"])
                print(textResult["BarcodeText"])
                localizationResult = textResult["LocalizationResult"]
                x1 = localizationResult["X1"]
                y1 = localizationResult["Y1"]
                x2 = localizationResult["X2"]
                y2 = localizationResult["Y2"]
                x3 = localizationResult["X3"]
                y3 = localizationResult["Y3"]
                x4 = localizationResult["X4"]
                y4 = localizationResult["Y4"]
                localizationPoints = [(x1,y1),(x2,y2),(x3,y3),(x4,y4)]
                print(localizationPoints)
        else :
            print("No barcode detected")
    except Exception as err:
        print(err)

if __name__ == "__main__":
    # Get the license from https://www.dynamsoft.com/CustomerPortal/Portal/Triallicense.aspx
    licenseKey = "LICENSE-KEY" 
    fileName = r"test.jpg"
    InitLicense(licenseKey)
    dir_path = os.path.dirname(os.path.realpath(__file__))
    DecodeFile(os.path.join(dir_path, fileName))

The post How to Run and Debug Python Barcode App in Docker Container appeared first on Code Pool.


Viewing all articles
Browse latest Browse all 145

Trending Articles