Make on-device OCR a pluggable local service so it runs locally on every platform (not just Windows), aimed at GoodNotes/Notability-class handwriting on low-power hardware (e.g. Zen2 APU, CPU/iGPU). - New OcrBackend abstraction (lib/services/ocr/): selector prefers an embedded ONNX recognition backend, falling back to the OS-native backend (Windows WinRT), and to a clean no-op when neither is available. - OnnxRecognitionBackend: flutter_onnxruntime session from a bundled asset, dart:ui preprocessing (resize to 48px, CHW float32, normalized), pure-Dart CTC greedy decode. Fully guarded — absent model/dict is a no-op; never throws. - ocr_engine.dart kept as a thin facade (recognizeImage) delegating to the selector, so ocr_service.dart is unchanged. - CtcDecoder unit-tested (6 tests). flutter analyze clean; all tests pass. - Model is not committed; tool/fetch_ocr_model.sh + assets/models/ocr/README.md document fetching PP-OCRv4 rec + dict on the dev machine. - CI: forward HTTPS_PROXY to the Windows build so CMake can fetch the ONNX Runtime native lib behind the GFW; README documents the system-install alternative. PP-OCR geometry/blank assumptions documented for on-device tuning. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
76 lines
2.6 KiB
Dart
76 lines
2.6 KiB
Dart
import 'package:badnote/services/ocr/ctc_decoder.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
/// Build a one-hot-ish logits row of [numClasses] with the max at [maxIndex].
|
|
List<double> _row(int numClasses, int maxIndex) {
|
|
return List<double>.generate(numClasses, (i) => i == maxIndex ? 1.0 : 0.0);
|
|
}
|
|
|
|
void main() {
|
|
group('CtcDecoder.decode', () {
|
|
test('collapses consecutive repeats and drops blanks (+1 shift)', () {
|
|
// charset indices: 1->'a', 2->'b', 3->'c' (blank at 0, shifted by one).
|
|
final decoder = CtcDecoder(['a', 'b', 'c'], blankIndex: 0);
|
|
const numClasses = 4; // blank + 3 chars
|
|
final logits = <List<double>>[
|
|
_row(numClasses, 1), // a
|
|
_row(numClasses, 1), // a (collapsed)
|
|
_row(numClasses, 0), // blank
|
|
_row(numClasses, 2), // b
|
|
_row(numClasses, 2), // b (collapsed)
|
|
_row(numClasses, 3), // c
|
|
];
|
|
expect(decoder.decode(logits), 'abc');
|
|
});
|
|
|
|
test('empty input yields empty string', () {
|
|
final decoder = CtcDecoder(['a', 'b', 'c'], blankIndex: 0);
|
|
expect(decoder.decode(<List<double>>[]), '');
|
|
});
|
|
|
|
test('all-blank input yields empty string', () {
|
|
final decoder = CtcDecoder(['a', 'b', 'c'], blankIndex: 0);
|
|
const numClasses = 4;
|
|
final logits = <List<double>>[
|
|
_row(numClasses, 0),
|
|
_row(numClasses, 0),
|
|
_row(numClasses, 0),
|
|
];
|
|
expect(decoder.decode(logits), '');
|
|
});
|
|
|
|
test('out-of-range indices are skipped', () {
|
|
// charset has 2 entries -> valid class indices are 1 and 2. Class index 3
|
|
// maps to charset[2] which is out of range and must be skipped.
|
|
final decoder = CtcDecoder(['a', 'b'], blankIndex: 0);
|
|
const numClasses = 4;
|
|
final logits = <List<double>>[
|
|
_row(numClasses, 1), // a
|
|
_row(numClasses, 3), // out of range -> skipped
|
|
_row(numClasses, 2), // b
|
|
];
|
|
expect(decoder.decode(logits), 'ab');
|
|
});
|
|
});
|
|
|
|
group('CtcDecoder.decodeFlat', () {
|
|
test('reshapes a flat row-major list and decodes it', () {
|
|
final decoder = CtcDecoder(['a', 'b', 'c'], blankIndex: 0);
|
|
const numClasses = 4;
|
|
const timeSteps = 3;
|
|
final flat = <double>[
|
|
..._row(numClasses, 1), // a
|
|
..._row(numClasses, 0), // blank
|
|
..._row(numClasses, 2), // b
|
|
];
|
|
expect(decoder.decodeFlat(flat, timeSteps, numClasses), 'ab');
|
|
});
|
|
|
|
test('returns empty for non-positive dimensions', () {
|
|
final decoder = CtcDecoder(['a'], blankIndex: 0);
|
|
expect(decoder.decodeFlat(<double>[1, 0], 0, 2), '');
|
|
expect(decoder.decodeFlat(<double>[1, 0], 2, 0), '');
|
|
});
|
|
});
|
|
}
|