-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDEAValidator.py
More file actions
109 lines (91 loc) · 3.95 KB
/
Copy pathDEAValidator.py
File metadata and controls
109 lines (91 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from tkinter import *
# initializing tkinter
root = Tk()
# setting geometry
root.geometry('350x350')
# setting title
root.title('DEA Validation')
def DEAvalidator():
import os
import easyocr
import cv2
import numpy as np
import re
try:
# updating root
root.update()
# getting countries names entered by the user
IMAGE_PATH = user_input.get()
DEA_FORMAT = r'[ABCDEFGHJKLMPRSTUX]+[A-Z]{1}\d{7}'
IS_DEA_FORMAT = []
if os.path.isfile(IMAGE_PATH):
OCRtext = []
reader = easyocr.Reader(['en'], gpu=False)
result = reader.readtext(IMAGE_PATH)
for OCR_ITEM in result:
OCRtext.append(OCR_ITEM[1])
#check for valid DEA number format with a regex
for ID in OCRtext:
UNCHECKED_DEA = re.findall(DEA_FORMAT, ID)
IS_DEA_FORMAT.extend(UNCHECKED_DEA)
else:
UNCHECKED_DEA = re.findall(DEA_FORMAT, IMAGE_PATH)
IS_DEA_FORMAT.extend(UNCHECKED_DEA)
print(IS_DEA_FORMAT)
print(f'Found potential DEA#:')
if IS_DEA_FORMAT == []:
print('None found')
else:
for num in IS_DEA_FORMAT:
print(num)
#validates each DEA number
IS_VALID_DEA = []
IS_NOT_VALID_DEA = []
for DEA_NUMBER in IS_DEA_FORMAT:
# calculates sum of digits 1, 3 and 5
CALC1_3_5 = sum(int(DEA_NUMBER[i]) for i in [2, 4, 6])
#calculates sum of digits 2, 4 and 6, multiplied by 2
CALC2_4_6 = 2*sum(int(DEA_NUMBER[i]) for i in [3, 5, 7])
#sum of both values
CHECK = CALC1_3_5 + CALC2_4_6
#isolate the last digit of the sum
check_lastdigit = CHECK % 10
dea_lastdigit = int(DEA_NUMBER[-1])
#compares last digit of sum to checksum
if dea_lastdigit == check_lastdigit:
IS_VALID_DEA.append(DEA_NUMBER)
else:
IS_NOT_VALID_DEA.append(DEA_NUMBER)
if IS_VALID_DEA == [] and IS_DEA_FORMAT != []:
for invalid_dea in IS_NOT_VALID_DEA:
result_text.insert('end', f'{invalid_dea} ')
result_text.tag_configure('invalid_tag', foreground='red')
result_text.insert('end-2c', ' INVALID', 'invalid_tag')
result_text.insert('end-1c', '\n')
elif IS_VALID_DEA == []:
result_text.insert('end', 'No DEA number found.\n')
else:
for valid_dea in IS_VALID_DEA:
result_text.insert('end', f'{valid_dea} ')
result_text.tag_configure('valid_tag', foreground='green')
result_text.insert('end-2c', ' VALID', 'valid_tag')
result_text.insert('end-1c', '\n')
for invalid_dea in IS_NOT_VALID_DEA:
result_text.insert('end', f'{invalid_dea} ')
result_text.tag_configure('invalid_tag', foreground='red')
result_text.insert('end-2c', ' INVALID', 'invalid_tag')
result_text.insert('end-1c', '\n')
except Exception as e:
result_text.delete(1.0, 'end') # Clear the text widget
result_text.insert('end', 'An error occurred. Please enter correct details again.')
#input GUI
Label(root, text='DEA VALIDATION', font='Consolas 15 bold').pack()
Label(root, text='Input source image path OR directly enter DEA#: ').pack()
user_input = StringVar()
user_input.set('Insert Image Path or DEA #')
entry = Entry(root, textvariable=user_input, width=50).pack()
Button(root, text='Validate', command=DEAvalidator).pack()
# Text widget to display results
result_text = Text(root, wrap='word', height=10, width=20) # Specify height and width
result_text.pack()
root.mainloop()