How to Annotate Barcode Object with LabelImg for Machine Learning

LabelImg is a free and open-source image annotation tool written in Python and Qt5. It supports Pascal VOC format and Yolo format that are commonly adopted by mainstream machine learning frameworks. If you are interested in barcode object detection, you can use the tool to annotate different barcode symbologies with bounding boxes. In this article, I will share how to quickly download a bunch of barcode images from Google and utilize Dynamsoft Barcode Reader SDK to automatically add label names.

Image Download

First, we need images. How can we get a bunch of images with the same classification?

There is a convenient Python library called google_images_download.

pip install google_images_download

When I was running the library, I found it could not normally work. It always failed to download any images. The issue has been mentioned on Fortunately, someone has provided a viable workaround. We can use the forked repository which has fixed the problem:

git clone
cd google-images-download && python install

According to the online documentation, we can write a simple Python script to download images as follows:

from google_images_download import google_images_download  
import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-k", "--keywords", required=True,
    help="The keywords/key phrases you want to search for.")
ap.add_argument("-l", "--limit", required=True,
    help="The number of images that you want to download.")
args = vars(ap.parse_args())

response = google_images_download.googleimagesdownload()
arguments = {"keywords":args["keywords"],"limit":args["limit"],"print_urls":True}  
paths =

Save the Python script to

Let’s download 10 PDF417 images from Google:

python3 -k pdf417 -l 10

Barcode Object Annotation

Once the barcode images are ready, we can get started to label barcodes.

The first step is to get the source code of LabelImg repository:

git clone

With LabelImg, we can label most objects through our eyes. However, you may not know all barcode symbologies. To precisely and swiftly name barcode objects, we can utilize Dynamsoft Barcode Reader as an assistant tool:

pip install dbr

Initializing Dynamsoft Barcode Reader is such easy:

from dbr import *
reader = BarcodeReader()

Go to the function newShape(self) in As we finish drawing a bounding box for an object, the function will be triggered.

The next step is to get coordinates from the current shape, which is stored as the last element of a shape array in canvas:

shape = self.canvas.shapes[-1]

A shape has four points. To extract the coordinate values and set the region values for Dynamsoft Barcode Reader, we use the top-left point and the bottom-right point.:

shape = self.canvas.shapes[-1]
points = shape.points
settings = reader.get_runtime_settings()
settings.region_bottom  = round(points[2].y())
settings.region_left    = round(points[0].x())
settings.region_right   = round(points[2].x())
settings.region_top     = round(points[0].y())

Afterward, we can get the barcode information inside the region by invoking the Python barcode decoding API:

            text_results = reader.decode_file(self.filePath)
            if text_results != None:
                for text_result in text_results:
                    print("Barcode Format :")
                    self.prevLabelText = text_result.barcode_format_string
                    print("Barcode Text :")
                    print("Localization Points : ")
        except BarcodeReaderError as bre:

Save the file and run the program:


When you create a rectangular box for a barcode object, it will automatically fill out the label dialog with the corresponding barcode type.

labelimg barcode annotation

Source Code