-
Notifications
You must be signed in to change notification settings - Fork 11
/
BlinkCardRecognizer.ts
435 lines (348 loc) · 10.5 KB
/
BlinkCardRecognizer.ts
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/**
* Copyright (c) Microblink Ltd. All rights reserved.
*/
import
{
ExtensionFactors,
FullDocumentImageOptions,
ImageResult,
validateDpi
} from "../BlinkID/ImageOptions";
import
{
MBDate,
Recognizer,
RecognizerResult,
RecognizerSettings,
WasmSDK
} from "../../MicroblinkSDK/DataStructures";
export enum LivenessStatus {
/**
* Verification process has not been started yet
*/
NotAvailable = 0,
/**
* Hand not present
*/
HandNotPresent = 1,
/**
* Not enough hand present
*/
NotEnoughHandPresent = 2,
/**
* Detected document not in hand
*/
DocumentNotInHand = 3,
/**
* Detected document in hand
*/
DocumentInHand = 4,
}
export type DocumentLivenessCallback = ( livenessStatus: LivenessStatus ) => void;
export enum CheckResult {
NotPerformed = 0,
Pass = 1,
Fail = 2
}
export enum MatchLevel {
Disabled = 0,
Level1 = 1,
Level2 = 2,
Level3 = 3,
Level4 = 4,
Level5 = 5,
Level6 = 6,
Level7 = 7,
Level8 = 8,
Level9 = 9,
Level10 = 10,
}
export interface TieredCheck {
result: CheckResult;
matchLevel: MatchLevel;
}
export interface DocumentLivenessCheckSingleSideResult {
screenCheck: TieredCheck;
photocopyCheck: TieredCheck;
handPresenceCheck: CheckResult;
}
export interface DocumentLivenessCheck {
front: DocumentLivenessCheckSingleSideResult;
back: DocumentLivenessCheckSingleSideResult;
}
/**
* Determines which data is anonymized in the returned recognizer result
*/
export enum AnonymizationMode
{
/** No anonymization is performed in this mode. */
None = 0,
/**
* Sensitive data in the document image is anonymized with black boxes covering selected sensitive data. Data
* returned in result fields is not changed.
*/
ImageOnly,
/** Document image is not changed. Data returned in result fields is redacted. */
ResultFieldsOnly,
/**
* Sensitive data in the image is anonymized with black boxes covering selected sensitive data. Data returned in
* result fields is redacted.
*/
FullResult,
/** Number of possible anonymization modes. */
Count
}
/**
* Holds the settings which control card number anonymization.
*/
export class CardNumberAnonymizationSettings
{
/** Defines the mode of card number anonymization. */
mode: AnonymizationMode = AnonymizationMode.ImageOnly;
/** Defines how many digits at the beginning of the card number remain visible after anonymization. */
prefixDigitsVisible = 0;
/** Defines how many digits at the end of the card number remain visible after anonymization. */
suffixDigitsVisible = 4;
}
export class AnonymizationSettings
{
/** Defines the parameters of card number anonymization. */
cardNumberAnonymizationSettings = new CardNumberAnonymizationSettings();
/** Defines the mode of card number prefix anonymization. */
cardNumberPrefixAnonymizationMode = AnonymizationMode.None;
/** Defines the mode of CVV anonymization. */
cvvAnonymizationMode = AnonymizationMode.None;
/** Defines the mode of IBAN anonymization. */
ibanAnonymizationMode = AnonymizationMode.ImageOnly;
/** Defines the mode of owner anonymization. */
ownerAnonymizationMode = AnonymizationMode.None;
/** If true, anonymization is applied on all fields of the image if extraction is uncertain. */
fallbackAnonymization = false;
}
/**
* A settings object that is used for configuring the BlinkCardRecognizer.
*/
export class BlinkCardRecognizerSettings implements FullDocumentImageOptions,
RecognizerSettings
{
/**
* Whether blured frames filtering is allowed
*/
allowBlurFilter = true;
/** Whether sensitive data should be redacted from the result */
anonymizationSettings = new AnonymizationSettings();
/**
* Padding is a minimum distance from the edge of the frame and is defined
* as a percentage of the frame width. Default value is 0.0f and in that case
* padding edge and image edge are the same.
* If padding edge is needed, recommended value is 0.02f.
*/
paddingEdge = 0.0;
/**
* Whether invalid card number is accepted
*/
allowInvalidCardNumber = false;
/**
* Whether to return CVV in recognizer results
*/
extractCvv = true;
/**
* Whether to return expiry date in recognizer results
*/
extractExpiryDate = true;
/**
* Whether to return IBAN in recognizer results
*/
extractIban = true;
/**
* Whether to return owner in recognizer results
*/
extractOwner = true;
// implementation of the FullDocumentImageOptions interface
returnFullDocumentImage = true;
returnEncodedFullDocumentImage = false;
/**
* Hand scale is calculated as a ratio between area of hand mask and document mask. `handScaleThreshold` is
* the minimal value for hand to be accepted as significant.
*/
handScaleThreshold = 0.15;
/**
* This parameter is used to adjust heuristics that eliminate cases when the hand is present in
the input but it is not holding the document. `handDocumentOverlapThreshold` is the minimal ratio of hand
pixels inside the frame surrounding the document and area of that frame. Only pixels inside that frame are
used to ignore false-positive hand segmentations inside the document (e.g. profile photo on documents)
*/
handDocumentOverlapThreshold = 0.05;
/**
* Screen analysis match level - higher is stricter.
*/
screenAnalysisMatchLevel = MatchLevel.Level5;
/**
* Photocopy analysis match level - higher is stricter.
*/
photocopyAnalysisMatchLevel = MatchLevel.Level5;
/**
* Called when document verification status is available.
*/
documentLivenessCallback: DocumentLivenessCallback | null = null;
private _fullDocumentImageDpi = 250;
get fullDocumentImageDpi(): number { return this._fullDocumentImageDpi; }
set fullDocumentImageDpi( value: number )
{
validateDpi( value );
this._fullDocumentImageDpi = value;
}
fullDocumentImageExtensionFactors = new ExtensionFactors();
}
/**
* The result of image recognition when using the BlinkCardRecognizer.
*/
export interface BlinkCardRecognizerResult extends RecognizerResult
{
/**
* The payment card number.
*/
readonly cardNumber: string;
/**
* The payment card number prefix.
*/
readonly cardNumberPrefix: string;
/**
* The payment card number.
*/
readonly cardNumberValid: boolean;
/**
* Payment card's security code/value.
*/
readonly cvv: string;
/**
* The payment card's expiry date.
*/
readonly expiryDate: MBDate;
/**
* Wheater the first scanned side is blurred.
*/
readonly firstSideBlurred: boolean;
/**
* Full image of the payment card from first side recognition.
*/
readonly firstSideFullDocumentImage: ImageResult;
/**
* Payment card's IBAN.
*/
readonly iban: string;
/**
* Payment card's issuing network
*/
readonly issuer: CardIssuer;
/**
* Information about the payment card owner.
*/
readonly owner: string;
/**
* Status of the last recognition process.
*/
readonly processingStatus: ProcessingStatus;
/**
* Whether the scanning of the first side is finished.
*/
readonly scanningFirstSideDone: boolean;
/**
* Wheater the second scanned side is blurred.
*/
readonly secondSideBlurred: boolean;
/**
* Full image of the payment card from second side recognition.
*/
readonly secondSideFullDocumentImage: ImageResult;
/**
* Is front side anonymized.
*/
readonly firstSideAnonymized: boolean;
/**
* Is back side anonymized.
*/
readonly secondSideAnonymized: boolean;
/**
* Document liveness check for both sides (screen, photocopy, hand presence);
*/
readonly documentLivenessCheck: DocumentLivenessCheck;
}
/**
* The Blink Card Recognizer is used for scanning Blink Card.
*/
export interface BlinkCardRecognizer extends Recognizer
{
/** Returns the currently applied BlinkCardRecognizerSettings. */
currentSettings(): Promise< BlinkCardRecognizerSettings >
/** Applies new settings to the recognizer. */
updateSettings( newSettings: BlinkCardRecognizerSettings ): Promise< void >;
/** Returns the current result of the recognition. */
getResult(): Promise< BlinkCardRecognizerResult >;
}
/**
* This function is used to create a new instance of `BlinkCardRecognizer`.
* @param wasmSDK Instance of WasmSDK which will be used to communicate with the WebAssembly module.
*/
export async function createBlinkCardRecognizer( wasmSDK: WasmSDK ): Promise< BlinkCardRecognizer >
{
return wasmSDK.mbWasmModule.newRecognizer( "BlinkCardRecognizer" ) as Promise< BlinkCardRecognizer >;
}
/**
* Payment Card Issuer defines supported issuing institutions that issued the card to the card holder.
*/
export enum CardIssuer
{
/* Unidentified Card */
Other = 0,
/* The American Express Company Card */
AmericanExpress,
/* China UnionPay Card */
ChinaUnionPay,
/* Diners Club International Card */
Diners,
/* Discover Card */
DiscoverCard,
/* Elo card association */
Elo,
/* The JCB Company Card */
Jcb,
/* Maestro Debit Card */
Maestro,
/* Mastercard Inc. Card */
Mastercard,
/* RuPay */
RuPay,
/* Interswitch Verve Card */
Verve,
/* Visa Inc. Card */
Visa,
/* V Pay */
VPay,
/* Enum count */
Count
}
/**
* Detailed information about the recognition process.
*/
export enum ProcessingStatus
{
/** Recognition was successful. */
Success = 0,
/** Detection of the document failed. */
DetectionFailed,
/** Preprocessing of the input image has failed. */
ImagePreprocessingFailed,
/** Recognizer has inconsistent results. */
StabilityTestFailed,
/** Wrong side of the document has been scanned. */
ScanningWrongSide,
/** Identification of the fields present on the card has failed. */
FieldIdentificationFailed,
/** Failed to return a requested image. */
ImageReturnFailed,
/** Payment card currently not supported by the recognizer. */
UnsupportedCard,
/** Number of possible processing statuses. */
Count
}