forked from TonyJacb/Vision_based_ID_Reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Master.py
executable file
·155 lines (128 loc) · 4.04 KB
/
Master.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python3
import numpy as np
import cv2
import pyocr
import pyocr.builders
from PIL import Image
import re
from firebase import firebase
import os
import statistics
# Initialize path of the working directory and temporary file
path = os.getcwd()+r"/data.txt"
# Firebase path
firebase = firebase.FirebaseApplication('https://vision-based-id-reader.firebaseio.com/', None)
# Regex for Registration Number
def extract_reg_number(string):
pat3 = re.compile(r'[0-2][0-9][B|M][A-Z][A-Z][0-2][0-9][0-9][0-9]')
re3 = pat3.findall(string)
re3 = ''.join(re3)
return re3
# Regex for Name
def extract_names(string):
pattern = re.compile(r'[A-Z][a-z]+')
names = pattern.findall(string)
newname = ' '.join(names)
return newname
#Regex for Phone Number
def extract_phoneNo(string):
pat2 = re.compile(r'[6-9][0-9]{9}')
number = pat2.findall(string)
number = ''.join(number)
return number
# Activate Tesseract
tools = pyocr.get_available_tools()
tool = tools[0]
# Open Camera
cap = cv2.VideoCapture(0)
f = open(path, "w")
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
txt = tool.image_to_string(Image.fromarray(
frame), builder=pyocr.builders.TextBuilder())
RegID = extract_reg_number(txt)
Name = extract_names(txt)
# Writing the on file; Empty files should not be written
if Name != "":
if RegID != "":
f.write(Name)
f.write(",")
f.write(RegID)
f.write("\n")
cv2.imshow("frame", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
f.flush()
f.close()
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
if os.path.getsize(path) > 0:
with open(path, 'r') as file:
data = file.read()
regs = []
# Split the string into Names and Registration numbers
string = re.split('\n|,', data)
# Separate Registration numbers and other redundant data(if any) from list
for i in string:
if(i.isalpha() == False):
string.remove(i)
regs.append(i)
# Get the frequency of elements and the elements as lists (in ascending order)
freq = np.unique(string, return_counts=True)[1].tolist()
val = np.unique(string, return_counts=True)[0].tolist()
regfreq = np.unique(regs, return_counts=True)[1].tolist()
regval = np.unique(regs, return_counts=True)[0].tolist()
# Get the index where the maximum value is located
maxindex = freq.index(max(freq))
maxregindex = regfreq.index(max(regfreq))
# Store Name and Reg Number
ID = val[maxindex]
Name = regval[maxregindex]
os.remove(path)
#Phone Number
cap = cv2.VideoCapture(0)
f = open(path, "w")
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
txt = tool.image_to_string(Image.fromarray(frame), builder=pyocr.builders.TextBuilder())
PhoneNumber = extract_phoneNo(txt)
if PhoneNumber != "":
f.write(PhoneNumber)
f.write("\n")
cv2.imshow("frame", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
f.flush()
f.close()
break
cap.release()
cv2.destroyAllWindows()
regs =[]
if os.path.getsize(path) > 0:
with open(path, 'r') as file:
PhoneNumber = file.read()
with open(path) as f:
lines = [line.rstrip() for line in f]
contactNumber = statistics.mode(lines)
# printing and post the filtered values
Creds = {"Registration Number": ID, "Name": Name,"Contact Number":contactNumber}
print("Name:", Name)
print("Registration Number:", ID)
print("Contact Number:", contactNumber)
print("\nConfirm the details (y/n)")
submit = input()
if(submit == "y"):
results = firebase.post("/TestData/", Creds)
print("Your details have been registered")
os.remove(path)
else:
print("Try Again")
os.remove(path)
else:
print("No ID Card was detected. Please try again")
# delete the file
os.remove(path)