-
Notifications
You must be signed in to change notification settings - Fork 1
/
aadharocr.py
180 lines (140 loc) · 3.68 KB
/
aadharocr.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import os.path
import json
import pytesseract
import re
import cv2
import imutils
import difflib
from flask import jsonify
import csv
from PIL import Image
def aadharocr(imageCopy, resized):
print('In Aadhar code')
greyRes = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
greyCopy = cv2.cvtColor(imageCopy, cv2.COLOR_BGR2GRAY)
resizedCopy = imutils.resize(greyCopy, height=550)
cv2.imwrite("temp.png", greyRes)
cv2.imwrite("temp1.png", greyCopy)
text = pytesseract.image_to_string(Image.open('temp.png'))
textOri = pytesseract.image_to_string(Image.open('temp1.png'))
# Initializing data variable
gender = None
yearline = []
genline = []
nameline = []
text1 = []
lines= ""
for item in text:
lines = lines + item
#print('LINES',lines)
# Searching for Year of Birth [Just need for text1 and wordlist]
for wordlist in lines.split('\n'):
xx = wordlist.split()
if ([w for w in xx if re.search('(Year|Birth|irth|YoB|YOB:|DOB:|DOB)$', w)]):
yearline = wordlist
break
else:
text1.append(wordlist)
try:
text2 = text.split(yearline,1)[1]
except:
pass
# Date of Birth
try:
dateObj = re.search('\d{1,2}[-/]\d{1,2}[-/]\d{4}', lines, re.M|re.I)
date = dateObj.group()
except:
print('Date of Birth not found')
date = ""
# Gender
try:
for wordlist in lines.split('\n'):
xx = wordlist.split()
if ([w for w in xx if re.search('(Female|Male|emale|male|ale|FEMALE|MALE|EMALE)$', w)]):
genline = wordlist
break
if 'FEMALE' in genline:
gender = "Female"
elif 'Female' in genline:
gender = "Female"
elif 'MALE' in genline:
gender = "Male"
elif 'Male' in genline:
gender = "Male"
except:
print('Gender not found')
gender = ''
pass
# Name
try:
newName = max(text1, key=len)
newName = re.sub('[^A-Za-z ]+',"", newName)
newName = re.sub('^\s+|\s+\Z', "", newName)
except:
print('Name not found')
tempName = ""
# Reading Database
with open('namedb.csv', 'rt', encoding = 'utf8') as f:
reader = csv.reader(f)
newlist = list(reader)
newlist = sum(newlist, [])
# Searching for Name and finding closest name in database
try:
text1 = filter(None, text1)
for x in text1:
for y in x.split():
if(difflib.get_close_matches(y.upper(), newlist)):
nameline.append(x)
break
name = ''.join(str(e) for e in nameline)
except:
pass
# Mobile Number
try:
mobileObj = re.search('[0-9]{10}', lines, re.M | re.I)
mobile = mobileObj.group()
mobile = re.sub('^\s+|\s+\Z', "", mobile)
except:
print('Mobile number not found')
mobile = ""
# Aadhar Number
try:
uidObj = re.search('[0-9]{4}\s[0-9]{4}\s[0-9]{4}', lines, re.M|re.I)
uid = uidObj.group()
uid = re.sub('^\s+|\s+\Z', "", uid)
except:
try:
uidObj = re.search('[0-9]{4}\s[0-9]{4}\s[0-9]{4}', textOri, re.M | re.I)
uid = uidObj.group()
uid = re.sub('^\s+|\s+\Z', "", uid)
print('Aadhar Number', uid)
except:
print('Aadhar number not found')
uid = ""
# Making tuples of data
data = {}
data['name'] = newName
data['gender'] = gender
data['birthYear'] = date
data['mobileNumber'] = mobile
data['aadharNo'] = uid
# Writing data into JSON
with open('raghav.json', 'w') as fp:
json.dump(data, fp)
# Removing dummy files
os.remove('temp.png')
# Reading data back JSON
with open('raghav.json', 'r') as f:
ndata = json.load(f)
print("+++++++++++++++++++++++++++++++")
print(ndata['name'])
print("-------------------------------")
print(ndata['birthYear'])
print("-------------------------------")
print(ndata['gender'])
print("-------------------------------")
print(ndata['mobileNumber'])
print("-------------------------------")
print(ndata['aadharNo'])
print("-------------------------------")
return jsonify(ndata)