|
| 1 | +import argparse |
| 2 | +import pyzbar.pyzbar as zbar |
| 3 | +from PIL import Image |
| 4 | +import zxingcpp |
| 5 | +from dbr import * |
| 6 | +import time |
| 7 | +import os |
| 8 | +import data |
| 9 | +import cv2 |
| 10 | + |
| 11 | + |
| 12 | +def zxing_decode(filename): |
| 13 | + start = time.time() |
| 14 | + img = cv2.imread(filename) |
| 15 | + zxing_results = zxingcpp.read_barcodes(img) |
| 16 | + elapsed_time = time.time() - start |
| 17 | + if zxing_results != None: |
| 18 | + for result in zxing_results: |
| 19 | + print('ZXing: {}. Elapsed time: {}ms'.format( |
| 20 | + result.text, int(elapsed_time * 1000))) |
| 21 | + return zxing_results |
| 22 | + else: |
| 23 | + print('ZXing failed to decode {}'.format(filename)) |
| 24 | + |
| 25 | + return None |
| 26 | + |
| 27 | + |
| 28 | +def zbar_decode(zbar_reader, filename): |
| 29 | + start = time.time() |
| 30 | + zbar_results = zbar.decode(Image.open(filename)) |
| 31 | + elapsed_time = time.time() - start |
| 32 | + if len(zbar_results) > 0: |
| 33 | + for zbar_result in zbar_results: |
| 34 | + print('ZBar: {}. Elapsed time: {}ms'.format( |
| 35 | + zbar_result.data.decode("utf-8"), int(elapsed_time * 1000))) |
| 36 | + |
| 37 | + return zbar_results |
| 38 | + else: |
| 39 | + print('ZBar failed to decode {}'.format(filename)) |
| 40 | + |
| 41 | + return None |
| 42 | + |
| 43 | + |
| 44 | +def dbr_decode(dbr_reader, filename): |
| 45 | + try: |
| 46 | + start = time.time() |
| 47 | + dbr_results = dbr_reader.decode_file(filename) |
| 48 | + elapsed_time = time.time() - start |
| 49 | + |
| 50 | + if dbr_results != None: |
| 51 | + for text_result in dbr_results: |
| 52 | + # print(textResult["BarcodeFormatString"]) |
| 53 | + print('Dynamsoft Barcode Reader: {}. Elapsed time: {}ms'.format( |
| 54 | + text_result.barcode_text, int(elapsed_time * 1000))) |
| 55 | + |
| 56 | + return dbr_results |
| 57 | + else: |
| 58 | + print("DBR failed to decode {}".format(filename)) |
| 59 | + except Exception as err: |
| 60 | + print("DBR failed to decode {}".format(filename)) |
| 61 | + |
| 62 | + return None |
| 63 | + |
| 64 | + |
| 65 | +def dataset(directory=None, zbar_reader=None, dbr_reader=None): |
| 66 | + if directory != None: |
| 67 | + print(directory) |
| 68 | + files = os.listdir(directory) |
| 69 | + files = [f for f in files if f.endswith('.jpg') or f.endswith('.png')] |
| 70 | + total_count = len(files) |
| 71 | + if total_count == 0: |
| 72 | + print('No image files') |
| 73 | + return |
| 74 | + |
| 75 | + # Create a .xlsx file |
| 76 | + datafile = 'benchmark.xlsx' |
| 77 | + wb = data.get_workbook(datafile) |
| 78 | + index = 2 |
| 79 | + |
| 80 | + print('Total count of barcode image files: {}'.format(total_count)) |
| 81 | + zbar_count = 0 |
| 82 | + dbr_count = 0 |
| 83 | + zxing_count = 0 |
| 84 | + |
| 85 | + for filename in files: |
| 86 | + file_path = os.path.join(directory, filename) |
| 87 | + expected_result = filename.split('_')[0] |
| 88 | + |
| 89 | + r1 = '' |
| 90 | + r2 = '' |
| 91 | + r3 = '' |
| 92 | + |
| 93 | + # ZBar |
| 94 | + if zbar_reader != None: |
| 95 | + zbar_results = zbar_decode(zbar_reader, file_path) |
| 96 | + if zbar_results != None: |
| 97 | + for zbar_result in zbar_results: |
| 98 | + zbar_text = zbar_result.data.decode("utf-8") |
| 99 | + r1 = zbar_text |
| 100 | + if r1 == expected_result: |
| 101 | + zbar_count += 1 |
| 102 | + break |
| 103 | + else: |
| 104 | + print('Fail to decode {}'.format(filename)) |
| 105 | + |
| 106 | + # DBR |
| 107 | + if dbr_reader != None: |
| 108 | + textResults = dbr_decode(dbr_reader, file_path) |
| 109 | + if textResults != None: |
| 110 | + for textResult in textResults: |
| 111 | + r2 = textResult.barcode_text |
| 112 | + if r2 == expected_result: |
| 113 | + dbr_count += 1 |
| 114 | + break |
| 115 | + else: |
| 116 | + print("DBR failed to decode {}".format(filename)) |
| 117 | + |
| 118 | + # ZXing |
| 119 | + print('ZXing decoding {}'.format(filename)) |
| 120 | + zxing_results = zxing_decode(file_path) |
| 121 | + if zxing_results != None: |
| 122 | + for result in zxing_results: |
| 123 | + r3 = result.text |
| 124 | + if r3 == expected_result: |
| 125 | + zxing_count += 1 |
| 126 | + else: |
| 127 | + print('ZXing failed to decode {}'.format(filename)) |
| 128 | + |
| 129 | + # Add results to .xlsx file |
| 130 | + data.update_row(wb, index, filename, expected_result, r1, r2, r3) |
| 131 | + index += 1 |
| 132 | + |
| 133 | + # Test |
| 134 | + # if index == 9: |
| 135 | + # break |
| 136 | + |
| 137 | + r1 = 0 |
| 138 | + r2 = 0 |
| 139 | + r3 = 0 |
| 140 | + if zbar_reader != None: |
| 141 | + zbar_rate = zbar_count * 100 / total_count |
| 142 | + r1 = '{0:.2f}%'.format(zbar_rate) |
| 143 | + print('ZBar recognition rate: {0:.2f}%'.format(zbar_rate)) |
| 144 | + |
| 145 | + if dbr_reader != None: |
| 146 | + dbr_rate = dbr_count * 100 / total_count |
| 147 | + r2 = '{0:.2f}%'.format(dbr_rate) |
| 148 | + print('DBR recognition rate: {0:.2f}%'.format(dbr_rate)) |
| 149 | + |
| 150 | + zxing_rate = zxing_count * 100 / total_count |
| 151 | + r3 = '{0:.2f}%'.format(zxing_rate) |
| 152 | + print('ZXing recognition rate: {0:.2f}%'.format(zxing_rate)) |
| 153 | + |
| 154 | + data.set_recognition_rate(wb, index, r1, r2, r3) |
| 155 | + # Save data to .xlsx file |
| 156 | + data.save_workbook(wb, datafile) |
| 157 | + |
| 158 | + |
| 159 | +def main(): |
| 160 | + ap = argparse.ArgumentParser() |
| 161 | + ap.add_argument("-i", "--image", type=str, |
| 162 | + help="path to input image") |
| 163 | + ap.add_argument("-d", "--directory", type=str, |
| 164 | + help="directory of image folder") |
| 165 | + args = vars(ap.parse_args()) |
| 166 | + |
| 167 | + image = args["image"] |
| 168 | + directory = args["directory"] |
| 169 | + if image == None and directory == None: |
| 170 | + print(''' |
| 171 | + Usage: |
| 172 | + python app.py -i <image_file> |
| 173 | + python app.py -d <folder_directory> |
| 174 | + ''') |
| 175 | + return |
| 176 | + |
| 177 | + # Initialize barcode reader |
| 178 | + BarcodeReader.init_license( |
| 179 | + 'DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==') |
| 180 | + dbr_reader = BarcodeReader() |
| 181 | + |
| 182 | + if image != None: |
| 183 | + # ZXing |
| 184 | + zxing_decode(image) |
| 185 | + |
| 186 | + # ZBar |
| 187 | + zbar_decode(zbar, image) |
| 188 | + |
| 189 | + # Dynamsoft Barcode Reader |
| 190 | + dbr_decode(dbr_reader, image) |
| 191 | + |
| 192 | + if directory != None: |
| 193 | + dataset(directory, |
| 194 | + zbar_reader=zbar, dbr_reader=dbr_reader) |
| 195 | + |
| 196 | + |
| 197 | +if __name__ == "__main__": |
| 198 | + main() |
0 commit comments