-
Notifications
You must be signed in to change notification settings - Fork 8
/
adharTesting.py
106 lines (89 loc) · 2.99 KB
/
adharTesting.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
from unittest import result
import cv2
import imutils
import json
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import requests
import time
from base64 import b64encode
from IPython.display import Image
from pylab import rcParams
import re
import sys
import urllib.request
l=[]
AWS_URL = str(sys.argv[1])
l= AWS_URL.split('/')
fileName = l[-1]
imgpath = urllib.request.urlretrieve(AWS_URL,fileName)
ENDPOINT_URL = 'https://vision.googleapis.com/v1/images:annotate'
api_key = 'AIzaSyCGvdjR7J88bChwlzFB4muKZE2MYPcT23U'
img_req = None
details = {
'name':'',
'aadhar':'',
'dob':'',
'gender': '',
'location':''
}
name_flag=0
gender_flag=0
with open(fileName, 'rb') as f:
ctxt = b64encode(f.read()).decode()
img_req = {
'image': {
'content': ctxt
},
'features': [{
'type': 'DOCUMENT_TEXT_DETECTION',
'maxResults': 1
}]
}
imgdata = json.dumps({"requests" : img_req}).encode()
result = requests.post(ENDPOINT_URL, data= imgdata, params = { 'key' : api_key}, headers = {'Content-type' : 'application/json'})
if result.status_code != 200 or result.json().get('error'):
print ("Error")
else:
result = result.json()['responses'][0]['textAnnotations']
for index in range(1,len(result)):
item = result[index]["description"]
### for gender
if item=="Male" or item=="MALE":
details["gender"] = "Male"
continue
elif item=="Female" or item=="FEMALE":
details["gender"] = "Female"
continue
if (item.upper() not in ["","GOVERNMENT","OF","INDIA", "GOVERNMENT OF INDIA"]):
### for name
if (item.isalpha() and item!="" and name_flag==0):
res=""
item = result[index]["description"]
while (bool(re.match("^[A-Za-z]*$", item )) and item.upper() not in ["DOB","DOB:","DOB :"]):
res = res + item + " "
index = index + 1
item = result[index]["description"]
name_flag=1
details['name'] = res
### for dob
elif re.findall('\d{2}\/\d{2}\/\d{4}',item) :
if item=="Year" or item=="YEAR":
index += 4
details['dob'] = result[index]["description"]
else:
details['dob'] = item
### for aadhar number
elif (re.findall("[0-9]{4}",item)):
val = index -1
if(result[val]["description"]!=":"):
details['aadhar'] +=item
details['name'] = details['name'].strip()
details['dob'] = details['dob'].strip()
details['aadhar'] = details['aadhar'].strip()
details['gender'] = details['gender'].strip()
details['location'] = AWS_URL.strip()
print(details['name'] + "\n" + details['aadhar'] + '\n' + details['dob'] + '\n' + details["gender"] + '\n' + details['location'], end="")
#print(details)