-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBsbsn
232 lines (116 loc) · 3.32 KB
/
Bsbsn
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
'use strict';
/**
* @author - jedwards
* @date - September 2017
*/
// Constants required for analyzing the drivers license
const IDENTITY_DOCUMENT_LABEL = 'identity document';
// Super basic/specific text matching patterns (start, offset, stop/length) to fill in the CA Driver's License metadata fields
const driversLicenseFields = {
'idNumber' : {
'start': 'LICENSE',
'offset': 11,
'numChars': 8
},
'firstName': {
'start': 'FN',
'offset': 3,
'stop': '\n'
},
'lastName': {
'start': 'LN',
'offset': 3,
'stop': 'FN'
},
'address': {
'start': 'FN',
'offset': 3,
'stop': 'DOB'
},
'gender': {
'start': 'SEX',
'offset': 4,
'numChars': 1
},
'dateOfBirth': {
'start': 'DOB',
'offset': 4,
'numChars': 10
},
'issuingDate': {
'start': 'ISSEE\nDD',
'offset': 31,
'numChars': 10
},
'expirationDate': {
'start': 'EXP',
'offset': 4,
'numChars': 10
}
}
/**
* exports.getMetadataValueForDriversLicense()
*
* Helper function to extract the text found by the Google Cloud Vision API to the corresponding CA Driver's License field.
*
* Input:
* (JSON) annotationImageResponse - the classifications found by the Cloud Vision API in JSON format
*
* Output:
* (JSON) - the formatted metadata for the CA Driver's License
*/
exports.getMetadataValueForDriversLicense = (annotationImageResponse) => {
console.log(annotationImageResponse);
var driversLicenseMetadata = {};
// If this is not an identity document, the "identity document" label will not be returned as part of the
// labelAnnotations response
var isIdentityDocument = false;
if (annotationImageResponse.hasOwnProperty('labelAnnotations')) {
annotation = annotationImageResponse['labelAnnotations'];
if (annotation.length > 0) {
for (var i = 0; i < annotation.length - 1; i++) {
if (annotation[i].description == IDENTITY_DOCUMENT_LABEL) {
isIdentityDocument = true;
break;
}
}
}
}
if (!isIdentityDocument) {
return driversLicenseMetadata;
}
// This is an identity document, so parse the text that Google Cloud Vision found
if (annotationImageResponse.hasOwnProperty('fullTextAnnotation')) {
var annotation = annotationImageResponse['fullTextAnnotation'];
if (annotation && annotation.hasOwnProperty('text')) {
var text = annotation.text;
var info = '';
for (var key in driversLicenseFields) {
var field = driversLicenseFields[key];
var start = text.indexOf(field['start']) + field['offset'];
// For the address, there's no good text to signal the beginning
// of the field. Instead we can add the length of the first name
// as an additional offset (since they are side-by-side)
if (key === 'address') {
start += driversLicenseMetadata['firstName'].length
}
var end = -1;
if (start > -1) {
if (field['stop']) {
// Stop at the occurrence of the "stop" string that first appears
// after the "start" string
end = text.indexOf(field['stop'], start);
} else {
// This field has a fixed number of characters
end = start + field['numChars'];
}
info = text.substring(start, end);
info = info.replace(new RegExp('/', 'g'), '-');
}
driversLicenseMetadata[key] = info;
info = '';
}
}
}
return driversLicenseMetadata;
}