Skip to content

Instantly share code, notes, and snippets.

@yushulx
Created January 7, 2020 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yushulx/32566858fc799b7d2e59899f0712c735 to your computer and use it in GitHub Desktop.
Save yushulx/32566858fc799b7d2e59899f0712c735 to your computer and use it in GitHub Desktop.
import argparse
try:
from PIL import Image
except ImportError:
import Image
import pytesseract
import sys
from dbr import DynamsoftBarcodeReader
dbr = DynamsoftBarcodeReader()
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'
# barcode detection
dbr.initLicense('LICENSE-KEY')
try:
results = dbr.DecodeFile(image)
textResults = results["TextResults"]
resultsLength = len(textResults)
print("count: " + str(resultsLength))
if resultsLength != 0:
for textResult in textResults:
print('Barcode Type: %s' % (textResult["BarcodeFormatString"]))
print('Barcode Result: %s' % (textResult["BarcodeText"]))
# localizationResult = textResult["LocalizationResult"]
# x1 = localizationResult["X1"]
# y1 = localizationResult["Y1"]
# x2 = localizationResult["X2"]
# y2 = localizationResult["Y2"]
# x3 = localizationResult["X3"]
# y3 = localizationResult["Y3"]
# x4 = localizationResult["X4"]
# y4 = localizationResult["Y4"]
# localizationPoints = [(x1,y1),(x2,y2),(x3,y3),(x4,y4)]
# print(localizationPoints)
else :
print("No barcode detected")
except Exception as err:
print(err)
custom_oem_psm_config = r'digits'
## Inovke Tesseract OCR
result = pytesseract.image_to_string(Image.open(image), config=custom_oem_psm_config)
print('OCR Result: %s' % (result))
## Filter string and keep digital numbers
# digits = ''
# for i in result:
# if ord(i) >= 48 and ord(i) <= 57:
# digits += i
# print(digits)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment