When labeling parcels, packages, products, and publications with linear barcode (1D barcode), the corresponding text is usually printed below the barcode. Barcode is machine-readable whereas the accompanying text is human readable If a barcode is damaged and unrecognizable, we can either check the information manually or take advantage of OCR (optical character recognition) technology as the assisted approach for barcode scanning automatically.
In this article, we will discuss how to use open-source OCR software Tesseract to recognize the accompanying text from barcode images. Furthermore, we demonstrate how to use Dynamsoft Barcode Reader SDK to get more precise results in complicated scenarios.
Recognizing Accompanying Text with Tesseract OCR
When looking for a free open source OCR engine, Tesseract is no doubt the first choice, which was initially developed by HP, and then developed and maintained by Google.
The source code is available on GitHub:
https://github.com/tesseract-ocr/tesseract
Visit the Wiki page to download and install the pre-built binary package.
Once the installation is complete, you can run the ‘tesseract’ command in the command-line tool.
Take a look at the simple scenario.
Run the following commands:
>tesseract accompanying-text.png output
>type output.txt
Sometimes, the result contains some misrecognized characters, which is inevitable for the default trained data. If you want to make it more precise, you’d better train the data by yourself.
You can also integrate Tesseract OCR into a Python program. Install pytesseract - a wrapper for Tesseract OCR engine:
pip install pytesseract
Create a test.py file:
import argparse
try:
from PIL import Image
except ImportError:
import Image
import pytesseract
import sys
def main():
# Get the input image file
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", type=str,
help="path to input image")
args = vars(ap.parse_args())
image = args["image"]
if image == None:
image = 'codabar.jpg'
custom_oem_psm_config = r'digits'
## Invoke Tesseract OCR
result = pytesseract.image_to_string(Image.open(image), config=custom_oem_psm_config)
print(result)
if __name__ == "__main__":
main()
The code above will only output the digital numbers:
What if we try a more complicated image?
Here is the Tesseract recognition result:
In this scenario, Tesseract can hardly work for assisting barcode scanning. First, the result is incorrect. Second, we don’t know which accompanying text belongs to which barcode.
The Efficient OCR Assist Provided by Dynamsoft Barcode Reader
In the latest version 7.3, Dynamsoft Barcode Reader features OCR function aiming to boost the accuracy of scanning barcode.
Download Dynamsoft Barcode Reader v7.3.
Import the sample code Dynamsoft\Barcode Reader 7.3\Samples\Desktop\C++\BarcodeReaderDemo to Visual Studio.
Get a free trial license and update the code:
reader.InitLicense("LICENSE-KEY");
Enable the mode of recognizing accompanying text:
reader.GetRuntimeSettings(&runtimeSettings);
runtimeSettings.barcodeFormatIds = BF_ALL;
runtimeSettings.furtherModes.accompanyingTextRecognitionModes[0] = ATRM_GENERAL;
iRet = reader.UpdateRuntimeSettings(&runtimeSettings,szErrorMsg,256);
Get the accompanying text from barcode results:
for (int iIndex = 0; iIndex < paryResult->resultsCount; iIndex++)
{
PExtendedResult* extResults = paryResult->results[iIndex]->results;
unsigned char* accompanyingTextBytes = (*extResults)->accompanyingTextBytes;
printf("Accompanying text: %s\n", accompanyingTextBytes);
}
Press F5 to run the app.
Dynamsoft Barcode Reader can work correctly. The text recognition rate will continue to improve as the size of the dataset grows in the next version.
What’s Coming Next
Dynamsoft barcode development team is working on price tag OCR and will release the first beta version as soon as possible. Price tag OCR can turn a robot with camera to an automated price checker, completely eliminating human involvement. The robot can scan the grocery aisle while capturing images of prices tags, reading barcode to determine the item SKU, as well as using OCR to return the structured pricing information to verify the prices against the database.
Technical Support
If you have any questions about Dynamsoft Barcode Reader SDK, please feel free to contact support@dynamsoft.com.
Release History
v7.3, 01/02/2020
NEW
- Added a new barcode type Postal codes, including USPS Intelligent Mail, Postnet, Planet, Australia Post barcode, RM4SCC.
- Added a new localization mode LM_STATISTICS_POSTAL_CODE in the struct PublicRuntimeSettings -> LocalizationModes to recognize Postal codes.
- Added the capability to obtain accompanying texts at the top or bottom of a linear barcode. It can be enabled by turning on the struct PublicRuntimeSettings -> FurtherModes -> AccompanyingTextRecognitionModes.
- Implemented the feature of recognizing distorted QR barcode. It can be enabled by turning on the struct PublicRuntimeSettings -> FurtherModes -> DeformationResistingModes.
- Implemented the feature of complementing missing parts of QR Code & DataMatrix barcodes. It can be enabled by turning on the struct PublicRuntimeSettings -> FurtherModes -> BarcodeComplementModes.
- Added a new setting AutoFilter to set whether to filter frames automatically in the struct FrameDecodingParameters.
- Added a new setting ScaleUpModes to set the scale-up mode for linear barcodes with small module size. It can be enabled by turning on the struct PublicRuntimeSettings -> ScaleUpModes.
IMPROVED
- Improved the decoding accuracy for DataMatrix that has a narrow quiet zone.
- Improved the decoding accuracy for 1D barcode that has a small module size.