Barcode analysis is a crucial factor to consider in product packaging. Barcodes are present on most products, and it is essential to ensure that they are both accurate and readable. However, manually verifying the barcodes of numerous products is a time-consuming and costly task that is prone to errors. To overcome this challenge, computer vision systems can quickly and accurately verify barcodes, identifying any products with faulty codes and redirecting them accordingly. In this practice, we will demonstrate how to generate a barcode, extract its type and data and then wrap up its data extraction system into an API for further utilization.
- Generate EAN13 barcode as png file using barcode python library.
- Extract barcode data and type using pyzbar python library.
- Build an API based on flask for barcode data and type extraction.
Barcode
UUID
OPenCV
Numpy
Pyzbar
Requests
Json
Jsonpickle
Flask
For testing our system, we have used images from the internet, in particular manufactured products images such as bottles, snacks, boxes, cans...
barcode is a python library used to create different standard types of barcodes. The library can generate SVG objects as well as PNG images. Python-barcode can generate the following types of barcodes:
- EAN-8
- EAN-13
- EAN-14
- UPC-A
- JAN
- ISBN-10
- ISBN-13
from barcode import EAN13 # imported to create EAN13 barcodes
from barcode.writer import ImageWriter # imported to save generated bar
import uuid # imported to generate unique numbers, later used for barcode creation.
# Create Random and Unique number for our barcode
random_number = str(uuid.uuid1().int)[:14]
print(random_number)
# Create an EAN13 barcode
my_code = EAN13(random_number, writer=ImageWriter())
# Save the barcode as png image
my_code.save("barcode_1")
The API takes as input the product image containing the barcode, read it and output the result as a json response with 3 key pieces of information: barcode data, barcode type and request status.
We have 3 messages codes:
- 200: successful request
- 404: no barcode data has been extracted
- 403: wrong authentification key
- Try different barcode readers libraries since Pyzbar does not support all barcode types. It was unable to work on certain products like
product20
from our dataset.