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>
46 lines
1.2 KiB
Dart
46 lines
1.2 KiB
Dart
import 'dart:typed_data';
|
|
|
|
import 'native_ocr_backend.dart';
|
|
import 'ocr_backend.dart';
|
|
import 'onnx_recognition_backend.dart';
|
|
|
|
/// Selects and caches the active local OCR backend.
|
|
///
|
|
/// Preference order: the embedded ONNX recognition backend if its model is
|
|
/// bundled and loads, otherwise the native platform backend, otherwise none.
|
|
class OcrBackends {
|
|
OcrBackends._();
|
|
|
|
static OcrBackend? _active;
|
|
static bool _resolved = false;
|
|
|
|
/// Resolve (once) and return the preferred available backend, or null when
|
|
/// no backend is available on this device/build.
|
|
static Future<OcrBackend?> active() async {
|
|
if (_resolved) return _active;
|
|
|
|
final candidates = <OcrBackend>[
|
|
OnnxRecognitionBackend(),
|
|
NativeOcrBackend(),
|
|
];
|
|
|
|
for (final backend in candidates) {
|
|
if (await backend.isAvailable()) {
|
|
_active = backend;
|
|
break;
|
|
}
|
|
}
|
|
|
|
_resolved = true;
|
|
return _active;
|
|
}
|
|
|
|
/// Recognize text using the active backend. Returns null when no backend is
|
|
/// available or nothing was recognized.
|
|
static Future<String?> recognize(Uint8List png) async {
|
|
final backend = await active();
|
|
if (backend == null) return null;
|
|
return backend.recognize(png);
|
|
}
|
|
}
|