Introduction: Scanning barcodes with Python on Linux devices
Having a Barcode Scanner SDK compatible with Linux devices opens up many possibilities for processing visual data on servers, embedded devices, and even drones. You can use one device to scan as many barcodes as you like (e.g., in a warehouse), send the barcodes’ values to the central server, and then process the data there. Or you can use one device for both steps. With Linux, virtually anything is possible.
To guide you through the setup process, we’ll run the Python version of our Linux SDK example app on a Raspberry Pi. When we’re done, the values of the scanned barcodes will be displayed in the terminal.
To achieve this, we’re going to follow these steps:
- Download and extract the example app
- Install the Python virtual environment and OpenCV modules
- Set up the Python virtual environment
- Install the Scanbot SDK package
- Run the Python barcode scanner script
⚠️ Our Linux SDK is currently in closed beta. To receive the necessary license key, API token, and version number, please send us an email at: beta@scanbot.io
We’ll be using a Raspberry Pi 4 running Raspberry Pi OS (we also have some tweaks you can use on an NVIDIA Jetson Nano). The Linux SDK also works on other distributions with GLIBC 2.27 or higher. Just make sure that your device has either an ARM64 or x86_64 architecture and enough internal storage for OpenCV. To test the live detection feature, your device also needs a camera.
Step 1: Download and extract the example app
Our example app includes Python scripts for processing barcodes both from image files and from a live camera feed. Just download or clone our GitHub repository and unzip the files if necessary.
The directory structure is as follows:
- scanbot-sdk-example-linux-main
- examples
- c++: Contains files for the C++ implementation, which we won’t cover in this tutorial.
- python
- minimal
- detect_barcodes.py: This is the Python script for reading barcodes from an image file.
- Readme.md: Contains the commands you’ll use to interact with the Python script for reading barcodes from an image file.
- live recognition
- detect_barcodes.py: This is the Python script for reading barcodes from a live camera feed.
- Readme.md: Contains the commands you’ll use to interact with the Python script for reading barcodes from a live camera feed.
- README.md: Contains information about installing Python and OpenCV and setting up the virtual environment in Python.
- minimal
- example.png: This is an image depicting three QR codes, which you can use to test out the example app’s functionality.
- Readme.md: Contains general information about the example app.
- examples
Step 2: Install the Python virtual environment and OpenCV modules
OpenCV is a library for real-time computer vision, which we’ll need to read our barcodes. We’ll also use venv to create a virtual environment for Python, which prevents conflicts with your development environment. We’ll install both modules using the command line.
But first, let’s make sure our package lists are up-to-date:
sudo apt update
Now we’ll install venv and the OpenCV modules for Python in one go:
sudo apt install -y python3-venv python3-opencv
💡 The -y
flag lets us run the installation unattended. Should this fail, rerun the command without the -y
flag to find out why.
Step 3: Set up the Python virtual environment
To exclude any interference from your existing installation, we’re going to perform our barcode experiment in a virtual environment. This is especially important if you’re using an NVIDIA Jetson Nano, since it comes with pre-installed packages that might not play nice.
Go to one of the lowest-level example app folders (“minimal” or “live recognition”) before inputting the following commands to save yourself the trouble of having to cd
into it later.
First, we create a virtual environment named “env” inside that folder:
python3 -m venv .env --system-site-packages
Then we activate it using the source command:
source .env/bin/activate
⚠️ Everything from now on happens in the virtual environment. If you exit the terminal or open another one, you need to reactivate the virtual environment with the above command.
Installing the Scanbot SDK package also requires the setuptools utilities and support for the binary distribution format wheels, so we’re going to install those in the virtual environment.
pip install --upgrade pip setuptools wheel
Step 4: Install the Scanbot SDK package
For this step, you need to know three things:
- Whether your device uses the ARM64 or x86_64 architecture
- The current Scanbot SDK version number
- Your API token for the Scanbot SDK
You need the latter two to download the appropriate Scanbot SDK binary. If you haven’t yet received them, please contact us at beta@scanbot.io.
First, export
the SCANBOTSDK_VERSION
variable with the version number so we can use them in the next step:
export SCANBOTSDK_VERSION=X.X.XXX
Then do the same for the SCANBOTSDK_API_TOKEN
variable with the token ID:
export SCANBOTSDK_API_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Now we’re going to install the correct Scanbot SDK package for your device’s architecture. You don’t need to paste the version number or the API token into the command, since we just exported them.
For ARM64 architectures
pip install https://scanbotsdk-deployment.s3.amazonaws.com/ScanbotSDK-Linux/${SCANBOTSDK_API_TOKEN}/scanbotsdk-${SCANBOTSDK_VERSION}-py3-none-linux_aarch64.whl
For x86_64 architectures
pip install https://scanbotsdk-deployment.s3.amazonaws.com/ScanbotSDK-Linux/${SCANBOTSDK_API_TOKEN}/scanbotsdk-${SCANBOTSDK_VERSION}-py3-none-linux_x86_64.whl
To test if the package has been installed correctly, run the following command. If it returns nothing, you’re good to go!
python3 -c "import scanbotsdk"
💡 If you want to run both the “minimal” and the “live recognition” experiments, repeat steps 3 and 4 for the other folder.
Step 5: Run the Python barcode scanner script
You’re almost ready to run detect_barcodes.py
! But first, open it in your text editor or IDE, go to line 5, and replace Put-your-license-key-here
with the license key you received from us (while keeping the double quotes).
The following steps differ depending on whether you want to run the script included in the “minimal” folder or the one in the “live recognition” folder.
For “minimal” (barcode recognition on an image file)
With the virtual environment activated, run the following command inside the “minimal” folder. If you’d like to use your own file for testing, just replace the image file path.
python3 detect_barcodes.py ../../example.png
Your output should look similar to this:
Initializing Scanbot SDK...
License Status: LicenseStatus.OK
Found 3 barcodes
Barcode(text='https://qrcodes.pro/lwKmGZ', format=<BarcodeFormat.QR_CODE: 11>, raw_bytes=b'https://qrcodes.pro/lwKmGZ', points=[135, 175, 256, 175, 256, 296, 135, 296])
Barcode(text='https://qrcodes.pro/eQnvl5', format=<BarcodeFormat.QR_CODE: 11>, raw_bytes=b'https://qrcodes.pro/eQnvl5', points=[325, 175, 446, 174, 446, 296, 325, 296])
Barcode(text='https://qrcodes.pro/XPAkG3', format=<BarcodeFormat.QR_CODE: 11>, raw_bytes=b'https://qrcodes.pro/XPAkG3', points=[515, 174, 636, 175, 636, 297, 515, 296])
🎉 Congratulations, you successfully scanned barcodes from an image file!
For “live recognition” (barcode reading with a camera)
You’ll need to run different commands depending on your setup.
Using a USB camera
In the following command, replace 1
with your camera’s device node number, then run it from the “live recognition” folder.
python3 detect_barcodes.py --input 1
Using a CSI camera on an NVIDIA Jetson Nano
python3 detect_barcodes.py --input jetson_csi
All you have to do then is to hold barcodes to the camera. The SDK will scan them automatically and print out their data. The SDK will continue to scan barcodes until you terminate the script using Ctrl+C.
🎉 Congratulations, you successfully scanned barcodes from a live camera feed!
Optional flags
You can add additional flags to your command to customize the output:
--use-display
: Display a window that shows the recognized barcodes in a live view. The recognized barcodes are indicated by a green border overlay.--use-tensorrt
: Enable TensorRT acceleration (Jetson only).
💡 The frames per second you can achieve with --use-display
during live detection depend on the power of your hardware. Hence, running the script on a Jetson Nano will result in smoother playback than on a Raspberry Pi.
Learn more about the Linux Barcode Scanner SDK
The Linux Barcode Scanner SDK is currently in closed beta, with the SDK itself and a trial license available on request via beta@scanbot.io.
Our customer Cypher Robotics uses the Linux SDK in its automated inventory cycle counting system to scan barcoded items autonomously in a warehouse environment.
If you have a use case you would like to discuss, feel free to reach out to us.