Skip to main content

flutter_ondevice_kyc 1.2.0 copy "flutter_ondevice_kyc: ^1.2.0" to clipboard
flutter_ondevice_kyc: ^1.2.0 copied to clipboard

PlatformAndroid

On-device KYC scanning for MRZ and card numbers. The scan path makes no network call.

flutter_ondevice_kyc #

On-device KYC for an identity-card MRZ and a payment-card number. The scan path does not open a socket and does not write the frame.

Does: finds the card, reads the MRZ or the PAN, and publishes a capture only when ICAO 9303, the Turkish national id check, or Luhn accepts it. Does not: read the CVV, match faces, test liveness, or call a server. Platforms: Android is the exercised target. iOS has no implementation.

dependencies:
  flutter_ondevice_kyc: ^1.2.0

A miss is a rejected frame, not an exception. The name line has no check digit, so a lock never means the name matched. The green lock means those check digits held; it is not an identity decision. SECURITY.md says what that does and does not prove.

Screenshots #

These frames are the example app on an Android emulator. The locked fields are the ICAO 9303 specimen, not a real document.

Paste Lock Scan
Validation tab Checksum lock Scan guide

Example recording

Scope #

Android is the platform exercised by the example. iOS has not been tested on a device. The plugin does not declare an iOS implementation, so pub.dev should not be read as "iOS supported." Web and desktop are out of scope.

This package does not read a chip on iOS, compare faces, or run liveness. Passive liveness is not planned. It stays out until a weight has a documented training origin this package can ship, or a consented attack set large enough to measure one exists. Neither is in this repository.

The NFC reader is opt-in: import package:flutter_ondevice_kyc/nfc.dart. The plugin merges no NFC permission, and the Android BAC path is unverified on a passport. Read ChipRead.status, not the documentation, when deciding whether a chip answered. A PACE-only identity card is reported as rejected, not as a match.

A later server verifier, if one is written, will be Dart so these check digits stay the only implementation. This release has no verifier.

SECURITY.md, doc/threat-model.md, DATA_POLICY.md, and MODEL_PROVENANCE.md are the boundaries. doc/consent-template.md is a blank volunteer form. THIRD_PARTY_NOTICES.md covers the model licences. None of these is a legal opinion.

How the checks, the repair, and the measurement work is in doc/how-it-works.md. The Turkish process notes are in doc/on-device-kyc.tr.md. What is next is ROADMAP.md.

What it reads #

Document Shape Gate
Identity card back, including the Turkish card TD1, 3×30 ICAO check digits. An 11-digit run in the optional data of a TUR document must also pass the TCKN test
Passport TD3, 2×44 ICAO check digits
Older booklet TD2, 2×36 ICAO check digits
Payment card 13–19 digits Luhn. The CVV is not read

Names come out in MRZ Latin. Turkish dotted letters are not in those lines.

The detector was fine-tuned on synthetic cards. A real card is still read when the MRZ lines sit in the frame. This repository does not contain photographs of real identity documents.

The TCKN gate is scoped to the issuing state. An 11-digit run in a foreign optional-data field is a serial number, not a Turkish national id, and it does not reject the frame.

Pipeline #

flowchart TD
  camera[Camera frame]
  focus[Focus the MRZ band, then lock]
  throttle[At most one frame per 200 ms, checked before the copy]
  orient[Rotate upright, crop only if cropToGuide]
  detect[YOLOX-Nano ONNX]
  warp[Homography]
  lines[Split MRZ lines]
  ocr[PP-OCRv4 per line]
  gate[ICAO 9303, TCKN, or Luhn]
  emit[KycCapture]
  camera --> focus --> throttle --> orient --> detect --> warp --> lines --> ocr --> gate
  gate -->|accepts| emit
  gate -->|rejects| throttle

Runtime is flutter_onnxruntime 1.9. The default provider is CPU. XNNPACK and CoreML are opt-in through KycExecutionProvider; CoreML throws an ArgumentError on Android instead of reaching an undefined native path. NNAPI is not a default: it is slow on these small graphs and it is deprecated on Android 15.

Integrate #

The host app owns the model files. Copy example/assets/models/ into the host assets and list the three files in the host pubspec.yaml. Digests are in assets/models/README.md. The weights are Apache-2.0, not MIT; ship THIRD_PARTY_NOTICES.md with them.

dependencies:
  flutter_ondevice_kyc: ^1.2.0

flutter:
  assets:
    - assets/models/card_detector.onnx
    - assets/models/text_rec.onnx
    - assets/models/en_dict.txt
final scanner = await KycScanner.onnx(
  detectorAsset: 'assets/models/card_detector.onnx',
  recognizerAsset: 'assets/models/text_rec.onnx',
  dictionaryAsset: 'assets/models/en_dict.txt',
);

KycScanView(
  scanner: scanner,
  strings: KycScanStrings.turkish, // or .english, or your own set
  onLocked: (capture) {},
);

KycScanView opens the back camera, draws the guide and the MRZ lane, locks focus on that band, stops the stream on a lock, and pauses the stream while the app is backgrounded. It has a torch toggle and pinch zoom; light and distance are the two reasons a frame misses. onError tells a denied permission apart from a missing camera.

Android: the plugin manifest adds CAMERA, and consumer-proguard.pro keeps ai.onnxruntime.** so a minified release build does not rename the ONNX Runtime classes. If you use nfc.dart, declare android.permission.NFC in the host manifest yourself.

Bring your own model or storage #

// From files the host already has, for a download instead of a 16 MB asset.
final scanner = await KycScanner.fromFiles(
  detectorPath: detectorPath,
  recognizerPath: recognizerPath,
  dictionaryText: await File(dictionaryPath).readAsString(),
);

// Or a whole engine of your own. Both interfaces are exported.
final scanner = KycScanner(detector: myDetector, recognizer: myRecognizer);

getTemporaryDirectory and File sit in the host. The scan path still opens no file and no socket.

A passport page #

The default guide is ID-1. A passport data page is ID-3:

KycScanView(
  scanner: scanner,
  documentShape: KycDocumentShape.passport,
);

This moves the guide, the MRZ lane, the focus band, and the track's aspect window. It does not change the gate. A held passport page on the live path is not measured; the row is in ROADMAP.md.

Run #

Flutter 3.47 or newer (Dart 3.13). The example is a host app: its models live in example/assets/models/.

flutter pub get
flutter test
cd example
flutter pub get
flutter test
flutter run

The scan tab embeds KycScanView. Tap the image to focus again. The validation tab locks pasted text. That lock did not come from a document.

API #

final scanner = await KycScanner.onnx(
  detectorAsset: 'assets/models/card_detector.onnx',
  recognizerAsset: 'assets/models/text_rec.onnx',
  dictionaryAsset: 'assets/models/en_dict.txt',
);
final decision = await scanner.scanFrame(frame); // camera
final decision = await scanner.scanStill(image); // gallery
if (decision is ScanLocked) {
  final capture = decision.capture;
}
await scanner.dispose();

KycScanner.fixture() locks the ICAO specimen when the weight files are absent. ScanRejected is not an exception. ScanSkipped means the frame gate was busy and is not a failure. checkManual is for pasted text. redactPan is for logs. The full number lives on KycCapture.cardNumber, and the host should not store it.

ScanSession is the state a scan surface keeps: the quad to follow, whether the card has settled enough to read text, and how long a lock stays on screen. Build your own UI on it if KycScanView does not fit.

Measure #

Lock rate, false-lock rate, and p95 are defined in eval/README.md. Frames and the manifest stay outside git. The fixture test checks the arithmetic. A model measurement points KYC_EVAL_DIR at a private folder.

Device model, operating system, lighting, on-device lock rate, and on-device p95 are unmeasured. The device table in that file says ölçülmedi. A seeded single-character trial on synthetic MRZ text separates checksum collisions from false locks the repair added, for a uniform edit and an OCR-weighted edit. It is not the on-device rate. The numbers are in doc/how-it-works.md.

Models #

Weights live in example/assets/models/. Digests are in assets/models/README.md.

File Source Size Licence
card_detector.onnx YOLOX-Nano, 3 classes, 416, FP32 8.6 MB Apache-2.0
text_rec.onnx en_PP-OCRv4_mobile_rec, opset 11, FP32 7.3 MB Apache-2.0
en_dict.txt PaddleOCR English dictionary, including < 190 B Apache-2.0

Training scripts are in training/. The Colab notebook is training/colab_yolox.ipynb. It draws a held-out synthetic set and fine-tunes YOLOX-Nano. The checked-in detector is that fine-tune. Checkpoints and generated images stay out of git. Detector INT8 is only worth doing with a calibration set that keeps detection quality. The recognizer stayed FP32.

Privacy #

lib/ does not import dart:io or an HTTP client. A frame stays in memory. Logs do not contain a PAN, an MRZ line, or a TCKN. The example Android manifest has no internet permission. What is stored, who can read it, and what a lock does not prove are in DATA_POLICY.md, SECURITY.md, and doc/threat-model.md. This is not a legal opinion.