-
Notifications
You must be signed in to change notification settings - Fork 66
/
ocr_main.py
136 lines (115 loc) · 3.75 KB
/
ocr_main.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
import requests
import json
import io
import re
import csv
import string
import dateutil.parser as dparser
def ocr_space_file(filename, overlay=False, api_key='18085fe6ab88957', language='eng'):
payload = {'isOverlayRequired': overlay,
'apikey': api_key,
'language': language,
}
with open(filename, 'rb') as f:
r = requests.post('https://api.ocr.space/parse/image',
files={filename: f},
data=payload,
)
m = r.content.decode()
jsonstr = json.loads(m)
print(jsonstr["ParsedResults"][0]["ParsedText"])
# Writing data into JSON
try:
to_unicode = unicode
except NameError:
to_unicode = str
# Write JSON file
with io.open('data_ocr_space.json', 'w', encoding='utf8') as outfile:
str_ = json.dumps(jsonstr, indent=4, sort_keys=True, separators=(',', ': '), ensure_ascii=False)
outfile.write(to_unicode(str_))
# Read JSON file
with open('data_ocr_space.json') as data_file:
data_loaded = json.load(data_file)
text = data_loaded["ParsedResults"][0]["ParsedText"]
text_output = open('outputbase_1.txt', 'w')
text_output.write(text)
text_output.close()
file = open('outputbase_1.txt', 'r')
text = file.read()
#print(text)
# Initializing data variable
name = None
fname = None
dob = None
pan = None
nameline = []
dobline = []
panline = []
text0 = []
text1 = []
text2 = []
# Searching for PAN
lines = text.split('\n')
for lin in lines:
s = lin.strip()
s = s.rstrip()
s = s.lstrip()
text1.append(s)
text1 = list(filter(None, text1)) # Attribute has to be converted into a list object before any additional processing
#print(text1) #at this operation the new line strings become a list of strings
lineno=0 # to start from the first line of the text file.
for wordline in text1:
xx = wordline.split('\n')
if ([w for w in xx if re.search('(INCOME|TAX|GOW|GOVT|GOVERNMENT|OVERNMENT|VERNMENT|DEPARTMENT|EPARTMENT|PARTMENT|ARTMENT|INDIA|NDIA)$', w)]):
text1 = list(text1)
lineno = text1.index(wordline)
break
#text1 = list(text1)
text0 = text1[lineno+1:]
#print(text0) #Contains all the relevant extracted text in form of a list - uncomment to check
try:
for x in text0:
for y in x.split():
nameline.append(x)
break
except:
pass
try:
name = nameline[0]
fname = nameline[1]
dob = nameline[2]
pan = nameline[4]
except:
pass
# Making tuples of data
data = {}
data['Name'] = name
data['Father Name'] = fname
data['Date of Birth'] = dob
data['PAN'] = pan
# Writing data into JSON
try:
to_unicode = unicode
except NameError:
to_unicode = str
# Write JSON file
with io.open('data_final.json', 'w', encoding='utf8') as outfile:
str_ = json.dumps(data, indent=4, sort_keys=True, separators=(',', ': '), ensure_ascii=False)
outfile.write(to_unicode(str_))
# Read JSON file
with open('data_final.json') as data_file:
data_loaded = json.load(data_file)
#print(data == data_loaded)
# Reading data back JSON(give correct path where JSON is stored)
with open('data_final.json', 'r') as f:
ndata = json.load(f)
ocr_space_file(filename='pancard_1.jpg', language='eng')
print('\t', "|+++++++++++++++++++++++++++++++|")
print('\t', '|', '\t', ndata['Name'])
print('\t', "|-------------------------------|")
print('\t', '|', '\t', ndata['Father Name'])
print('\t', "|-------------------------------|")
print('\t', '|', '\t', ndata['Date of Birth'])
print('\t', "|-------------------------------|")
print('\t', '|', '\t', ndata['PAN'])
print('\t', "|+++++++++++++++++++++++++++++++|")