A Standalone Application to Scan Barcode using Webcam | by Gautam Kumar | Towards Data Science

When you visit any shopping mall or stores, you must have seen a barcode detector machine (see figure 1, image source flickr) which read barcode of the product you purchased and calculate the cost. Finally, you get a receipt of shopping having items you have purchased and cost of respective items. Use of barcode scanner in shopping malls reduces paperwork and digitize shopping by a great extent. However, it still requires human effort.
Another problem with this is, it comes at a high cost. changes and durability is also an issue with this barcode scanner machine. Therefore, it became necessary to develop such a system which can automate the process of scanning the products and comes at low cost with low maintenance requirement.
In this article, a barcode scanner using python has been developed which can read barcode of the product using webcam, decode the cost of item which is encrypted in barcode and calculate the price of the product, finally at the end of shopping you will get number of items you have purchased, date of purchasing, name of items purchased and the corresponding cost. Developed system is found to be cost effective, durable, easy to use, fully automated the billing process i.e don’t require human effort to scan barcode.
Now a days, there are many standard libraries available in python to create barcode. I have used pyBarcode. It can provide EAN-8, EAN-13, EAN-14, UPC-A, JAN, ISBN-10, ISBN-13, ISSN, Code 39, Code 128, PZN types of standard barcode. To install pyBarcode use pip as
pip install pyBarcode
Once you install pyBarcode, run following four lines of code to create barcode image. This will save barcode image file in .svg format. However, you can also save it as .png with minor changes in the following code which is taken from here.
>>>import barcode>>>EAN = barcode.get_barcode_class('ean13')>>>ean = EAN(u'5901234123457')>>>fullname = ean.save('ean13_barcode')
Above code will save barcode in European Article Number (EAN) standard. You can change standard as per for choice. Generated barcode using python scripts is shown in Figure 2.
Alternatively, you can download any software freely available on the internet to generate barcode. I used tool downloaded from this website. You just need to set data and text above and below (optional) barcode. You can also set the width and height of the barcode image and extract it as .png or store in excel. In my application, i used CODE 128 standard and extracted image as .png. The barcode generated using software tool is look like shown in figure 3.
Similarly, I generated barcode for four products (2 books, one calculator and one computer mouse), took the print out of those barcodes and stick them to products. After these steps, my four products look like, as shown in figure 4.
So, in this way you can also create your own bar codes of products for your shopping store.
Here, i have developed a python application to read barcode that i have created and decode data from that using webcam. The script initializes live video stream and extracts each frame from the video. It finds edges by blurring frame. Bilateral filter or Gaussian filter can be used for this purpose. Then an adaptive threshold is used to obtained processed image. After the morphological operation, a contour is drawn around the barcode. All these steps are taken care of by decode object of pyzbar. Therefore, before executing program make sure that you have installed pyzbar which can be installed using pip as
pip install pyzbar
For rest of the process, please download complete code from GitHub repository and follow steps as mentioned in readme.
The data (cost of the product) encrypted in the barcode is a combination of product name and its cost. For example, data of barcode for the item calculator is set as C4 where ‘C’ is considered the item ID (Calculator) and ‘4’ as the item cost in $. For each scanned item, application records purchase date, purchased item ID and item cost. Once we press button ‘e’ to exit from the barcode reading process, the application adds the cost of every purchased item and display the final cost of shopping. The demo video of the complete process is shown here.
This application can be considered as cost effective, as cost of webcam is quite very low compared to cost of bar code reader machine.It is very easy to handle and also don’t require human effort as webcam can be fixed at certain place and customers only need to keep items to be purchased facing barcodes towards webcam.Webcam is more durable and even if some problem occurs, it is easy to fix compared to barcode reader machines.This application can be used in shopping malls and small stores to automate billing process. It can also be used to develop similar application like in library, a book can be assigned to student using barcode scanning system.
This application can be considered as cost effective, as cost of webcam is quite very low compared to cost of bar code reader machine.
It is very easy to handle and also don’t require human effort as webcam can be fixed at certain place and customers only need to keep items to be purchased facing barcodes towards webcam.
Webcam is more durable and even if some problem occurs, it is easy to fix compared to barcode reader machines.
This application can be used in shopping malls and small stores to automate billing process. It can also be used to develop similar application like in library, a book can be assigned to student using barcode scanning system.
If barcode is small in size or distance between webcam and barcode is more then system fails to scan barcode.
If barcode is small in size or distance between webcam and barcode is more then system fails to scan barcode.
This application can also be used to read QR code with miner changes in the code. You can download code and create your own barcode with setting cost of the product as per your requirement. Finally, you will have a complete application for automated billing process for your shop/shopping store. Thanks to Jayeeta Chakraborty, who helped me to develop this project. Hope this application may help researchers and developer to develop custom barcode scanner.