Quantcast
Viewing latest article 4
Browse Latest Browse All 239

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 https://github.com/hardikvasa/google-images-download/issues/331. Fortunately, someone has provided a viable workaround. We can use the forked repository which has fixed the problem:

git clone https://github.com/Joeclinton1/google-images-download.git
cd google-images-download && python setup.py 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 = response.download(arguments)
print(paths) 

Save the Python script to google-image-downloader.py.

Let’s download 10 PDF417 images from Google:

python3 google-image-downloader.py -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 https://github.com/tzutalin/labelImg.git

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 labelImg.py. 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.:

reader.reset_runtime_settings() 
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())
reader.update_runtime_settings(settings)

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

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

Save the labelImg.py file and run the program:

python3 labelImg.py

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

Image may be NSFW.
Clik here to view.
labelimg barcode annotation

Source Code

https://github.com/yushulx/labelImg

The post How to Annotate Barcode Object with LabelImg for Machine Learning appeared first on Dynamsoft Developers.


Viewing latest article 4
Browse Latest Browse All 239

Trending Articles