-
Notifications
You must be signed in to change notification settings - Fork 2
/
drivingLicenseFront.py
59 lines (52 loc) · 1.94 KB
/
drivingLicenseFront.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
import json
import boto3
import base64
import re
from drivingLicenseFront import drivingLicenseFront
from pan import pan
from complexPan import complexPan
from aadhaarFront import aadhaarFront
from aadhaarBack import aadhaarBack
from panClassifier import panClassifier
from aadhaarClassifier import aadhaarClassifier
def lambda_handler(event, context):
client = boto3.client('textract')
base64image = json.loads(event['body'])['base64image'].split('base64,')[1]
binaryImage = bytearray(base64.b64decode(base64image))
data = client.detect_document_text(
Document = {
'Bytes': binaryImage
}
)
# response['image'] = json.loads(event['body'])['base64image']
if(data['DocumentMetadata']['Pages'] > 1):
return {
'statusCode': 428,
'body': 'Multiple pages found in the document uploaded...'
}
else:
data = data['Blocks']
ocrData = {}
for line in data:
if(line['BlockType'] == 'LINE'):
text = line['Text'].strip()
# Driving license...
if(bool(re.search('(([A-Z]{2}[0-9]{2})( )|([A-Z]{2}-[0-9]{2}))((19|20)[0-9][0-9])[0-9]{7}$', text))):
ocrData = drivingLicenseFront(data)
break
# PAN
if(bool(re.match('[A-Z]{5}[0-9]{4}[A-Z]{1}', text))):
ocrData = panClassifier(data)
break
# Aadhaar
if(bool(re.match('[0-9]{4} [0-9]{4} [0-9]{4}$', text))):
ocrData = aadhaarClassifier(data)
break
resp = {}
resp['base64image'] = json.loads(event['body'])['base64image']
resp['ocrData'] = ocrData
resp['rawData'] = data
return {
'statusCode': 200,
'body': json.dumps(resp)
}