Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mobile][rust] Remove OCR request cancellation

laurenspriem59b7c41c6762
diff --git a/mobile/apps/photos/lib/services/machine_learning/ocr/ocr_backend.dart b/mobile/apps/photos/lib/services/machine_learning/ocr/ocr_backend.dart
index 3eeb4b2629..81006d46b2 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ocr/ocr_backend.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ocr/ocr_backend.dart
@@ -10,15 +10,11 @@ abstract class OcrBackend {
Future<TextDetectionResult> detectText({
required String imagePath,
bool includeAllConfidenceScores = false,
- String? requestId,
});
Future<TextRegionDetectionResult> detectTextRegions({
required String imagePath,
- String? requestId,
});
- Future<void> cancelRequest(String requestId);
-
Future<String> ensureDisplayablePath(String imagePath);
}
diff --git a/mobile/apps/photos/lib/services/machine_learning/ocr/rust_ocr_backend.dart b/mobile/apps/photos/lib/services/machine_learning/ocr/rust_ocr_backend.dart
index 43a9fe339c..7082bcc3a5 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ocr/rust_ocr_backend.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ocr/rust_ocr_backend.dart
@@ -93,14 +93,12 @@ class RustOcrBackend implements OcrBackend {
Future<TextDetectionResult> detectText({
required String imagePath,
bool includeAllConfidenceScores = false,
- String? requestId,
}) async {
final engine = await _recognitionEngine();
try {
final result = await engine.detectText(
imagePath: imagePath,
includeAllConfidenceScores: includeAllConfidenceScores,
- requestId: requestId,
);
return textDetectionResultFromRust(result);
} on RustOcrError catch (error) {
@@ -115,25 +113,16 @@ class RustOcrBackend implements OcrBackend {
@override
Future<TextRegionDetectionResult> detectTextRegions({
required String imagePath,
- String? requestId,
}) async {
final engine = await _detectionEngine();
try {
- final result = await engine.detectTextRegions(
- imagePath: imagePath,
- requestId: requestId,
- );
+ final result = await engine.detectTextRegions(imagePath: imagePath);
return textRegionDetectionResultFromRust(result);
} on RustOcrError catch (error) {
throw _failure(error, imagePath: imagePath, otherCode: "DETECTION_ERROR");
}
}
- @override
- Future<void> cancelRequest(String requestId) async {
- _engine?.cancel(requestId: requestId);
- }
-
@override
Future<String> ensureDisplayablePath(String imagePath) {
return Future.value(imagePath);
@@ -263,10 +252,6 @@ OcrException ocrExceptionFromRustError(
code: "IMAGE_DECODE_ERROR",
message: "Failed to decode image: $message",
),
- RustOcrError_Cancelled() => OcrException(
- code: "CANCELLED",
- message: message,
- ),
RustOcrError_CorruptModel() => OcrException(
code: "MODEL_PREP_ERROR",
message: message,
@@ -276,7 +261,6 @@ OcrException ocrExceptionFromRustError(
}
String _rustOcrErrorMessage(RustOcrError error) => switch (error) {
- RustOcrError_Cancelled() => "OCR request was cancelled",
RustOcrError_ImageNotFound(:final message) ||
RustOcrError_InvalidImage(:final message) ||
RustOcrError_CorruptModel(:final message) ||
diff --git a/mobile/apps/photos/lib/services/machine_learning/ocr/vision_ocr_backend.dart b/mobile/apps/photos/lib/services/machine_learning/ocr/vision_ocr_backend.dart
index b7d0ddec71..425421c62d 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ocr/vision_ocr_backend.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ocr/vision_ocr_backend.dart
@@ -29,13 +29,11 @@ class VisionOcrBackend implements OcrBackend {
Future<TextDetectionResult> detectText({
required String imagePath,
bool includeAllConfidenceScores = false,
- String? requestId,
}) {
return _translate(() async {
final result = await _recognizer.detectText(
imagePath: imagePath,
includeAllConfidenceScores: includeAllConfidenceScores,
- requestId: requestId,
);
return TextDetectionResult.fromMap(result);
});
@@ -44,22 +42,13 @@ class VisionOcrBackend implements OcrBackend {
@override
Future<TextRegionDetectionResult> detectTextRegions({
required String imagePath,
- String? requestId,
}) {
return _translate(() async {
- final result = await _recognizer.detectTextRegions(
- imagePath: imagePath,
- requestId: requestId,
- );
+ final result = await _recognizer.detectTextRegions(imagePath: imagePath);
return TextRegionDetectionResult.fromMap(result);
});
}
- @override
- Future<void> cancelRequest(String requestId) {
- return _translate(() => _recognizer.cancelRequest(requestId));
- }
-
@override
Future<String> ensureDisplayablePath(String imagePath) async {
return imagePath;
diff --git a/mobile/apps/photos/lib/services/machine_learning/ocr_service.dart b/mobile/apps/photos/lib/services/machine_learning/ocr_service.dart
index 3de747b969..e4270d8841 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ocr_service.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ocr_service.dart
@@ -180,33 +180,21 @@ class OcrService with WidgetsBindingObserver {
Future<TextDetectionResult> detectText({
required String imagePath,
bool includeAllConfidenceScores = false,
- String? requestId,
}) async {
_ensureImageExists(imagePath);
return _withModels(
() => _backend.detectText(
imagePath: imagePath,
includeAllConfidenceScores: includeAllConfidenceScores,
- requestId: requestId,
),
);
}
Future<TextRegionDetectionResult> detectTextRegions({
required String imagePath,
- String? requestId,
}) async {
_ensureImageExists(imagePath);
- return _withModels(
- () => _backend.detectTextRegions(
- imagePath: imagePath,
- requestId: requestId,
- ),
- );
- }
-
- Future<void> cancelRequest(String requestId) {
- return _backend.cancelRequest(requestId);
+ return _withModels(() => _backend.detectTextRegions(imagePath: imagePath));
}
Future<String> ensureDisplayablePath(String imagePath) {
diff --git a/mobile/apps/photos/lib/ui/viewer/file/ocr/inline_text_detection.dart b/mobile/apps/photos/lib/ui/viewer/file/ocr/inline_text_detection.dart
index 41188e6574..191aaf56ef 100644
--- a/mobile/apps/photos/lib/ui/viewer/file/ocr/inline_text_detection.dart
+++ b/mobile/apps/photos/lib/ui/viewer/file/ocr/inline_text_detection.dart
@@ -72,7 +72,6 @@ class _InlineTextDetectionState extends State<InlineTextDetection> {
String? _localFilePath;
int _evaluationGeneration = 0;
int _regionDetectionAttempt = 0;
- String? _activeRegionRequestId;
Timer? _regionRetryTimer;
TextRegionDetectionResult? _detectedRegions;
bool _overlayActive = false;
@@ -155,7 +154,6 @@ class _InlineTextDetectionState extends State<InlineTextDetection> {
void dispose() {
_ocrService.onViewerClosed();
_routeAnimation?.removeStatusListener(_onRouteAnimationStatus);
- _cancelActiveRegionRequest();
_regionRetryTimer?.cancel();
_zoomSettleTimer?.cancel();
_globalLongPressTimer?.cancel();
@@ -169,7 +167,6 @@ class _InlineTextDetectionState extends State<InlineTextDetection> {
void _resetState() {
_evaluationGeneration++;
- _cancelActiveRegionRequest();
_regionRetryTimer?.cancel();
_regionRetryTimer = null;
_regionDetectionAttempt = 0;
@@ -216,14 +213,6 @@ class _InlineTextDetectionState extends State<InlineTextDetection> {
bool get _requiresRegionRouting => widget.file.isLiveOrMotionPhoto;
- void _cancelActiveRegionRequest() {
- final requestId = _activeRegionRequestId;
- _activeRegionRequestId = null;
- if (requestId != null) {
- unawaited(_ocrService.cancelRequest(requestId).catchError((_) {}));
- }
- }
-
Future<void> _evaluateFile({bool isRetry = false}) async {
final bool isEligible = _isFileEligible(widget.file);
final int generation = isRetry
@@ -292,24 +281,9 @@ class _InlineTextDetectionState extends State<InlineTextDetection> {
throw StateError("OCR detector is not ready");
}
- final regionRequestId = "photos-$cacheKey-$generation";
- _activeRegionRequestId = regionRequestId;
- late final TextRegionDetectionResult result;
- try {
- result = await _ocrService
- .detectTextRegions(
- imagePath: localFile.path,
- requestId: regionRequestId,
- )
- .timeout(_regionDetectionTimeout);
- } on TimeoutException {
- await _ocrService.cancelRequest(regionRequestId);
- rethrow;
- } finally {
- if (_activeRegionRequestId == regionRequestId) {
- _activeRegionRequestId = null;
- }
- }
+ final result = await _ocrService
+ .detectTextRegions(imagePath: localFile.path)
+ .timeout(_regionDetectionTimeout);
if (!mounted || generation != _evaluationGeneration) return;
_cacheRegionResult(
diff --git a/mobile/apps/photos/lib/ui/viewer/file/ocr/text_detector_widget.dart b/mobile/apps/photos/lib/ui/viewer/file/ocr/text_detector_widget.dart
index 72617f0ec2..9fb4d91d63 100644
--- a/mobile/apps/photos/lib/ui/viewer/file/ocr/text_detector_widget.dart
+++ b/mobile/apps/photos/lib/ui/viewer/file/ocr/text_detector_widget.dart
@@ -102,8 +102,6 @@ class _TextDetectorWidgetState extends State<TextDetectorWidget> {
Size? _imageSize;
bool _userAttemptedInteraction = false;
Offset? _pendingSelectionPosition;
- int _detectionRequestSequence = 0;
- String? _activeDetectionRequestId;
bool get _hasSelectableText =>
_detectedTextBlocks != null && _detectedTextBlocks!.isNotEmpty;
@@ -124,7 +122,6 @@ class _TextDetectorWidgetState extends State<TextDetectorWidget> {
@override
void dispose() {
- _cancelActiveDetection();
widget.controller._detach(this);
super.dispose();
}
@@ -173,14 +170,6 @@ class _TextDetectorWidgetState extends State<TextDetectorWidget> {
}
}
- void _cancelActiveDetection() {
- final requestId = _activeDetectionRequestId;
- _activeDetectionRequestId = null;
- if (requestId != null) {
- unawaited(_ocr.cancelRequest(requestId).catchError((_) {}));
- }
- }
-
Future<void> _ensureModelsReady() async {
if (_modelsReady) return;
@@ -246,7 +235,6 @@ class _TextDetectorWidgetState extends State<TextDetectorWidget> {
_notifyController();
}
- String? nativeRequestId;
try {
await _ensureModelsReady();
if (_errorMessage != null) {
@@ -257,14 +245,7 @@ class _TextDetectorWidgetState extends State<TextDetectorWidget> {
return;
}
- final requestId =
- 'text-detector-${identityHashCode(this)}-${++_detectionRequestSequence}';
- nativeRequestId = requestId;
- _activeDetectionRequestId = requestId;
- final result = await _ocr.detectText(
- imagePath: imagePath,
- requestId: requestId,
- );
+ final result = await _ocr.detectText(imagePath: imagePath);
if (mounted && widget.imagePath == requestedPath) {
final pendingPos = _pendingSelectionPosition;
setState(() {
@@ -297,9 +278,6 @@ class _TextDetectorWidgetState extends State<TextDetectorWidget> {
_notifyController();
}
} finally {
- if (_activeDetectionRequestId == nativeRequestId) {
- _activeDetectionRequestId = null;
- }
if (mounted && widget.imagePath == requestedPath) {
setState(() {
_isProcessing = false;
diff --git a/mobile/apps/photos/plugins/ente_vision/ios/Flutter/TextRecognitionChannelAdapter.swift b/mobile/apps/photos/plugins/ente_vision/ios/Flutter/TextRecognitionChannelAdapter.swift
index 76d2a1c6d7..a3cad112b6 100644
--- a/mobile/apps/photos/plugins/ente_vision/ios/Flutter/TextRecognitionChannelAdapter.swift
+++ b/mobile/apps/photos/plugins/ente_vision/ios/Flutter/TextRecognitionChannelAdapter.swift
@@ -20,13 +20,12 @@ final class TextRecognitionChannelAdapter {
func detach() {
guard isAttached else { return }
isAttached = false
- recognizer.cancelAll()
channel.setMethodCallHandler(nil)
}
private func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
guard isAttached else {
- result(TextRecognitionError.cancelled.flutterError)
+ result(FlutterError(code: "UNAVAILABLE", message: "Text recognition plugin is detached", details: nil))
return
}
switch call.method {
@@ -34,8 +33,6 @@ final class TextRecognitionChannelAdapter {
handleDetectText(call, result: result)
case "textRecognition.detectTextRegions":
handleDetectTextRegions(call, result: result)
- case "textRecognition.cancelRequest":
- handleCancelRequest(call, result: result)
default:
result(FlutterMethodNotImplemented)
}
@@ -50,8 +47,7 @@ final class TextRecognitionChannelAdapter {
}
recognizer.recognizeText(
imagePath: imagePath,
- includeAllConfidenceScores: arguments["includeAllConfidenceScores"] as? Bool ?? false,
- requestId: arguments["requestId"] as? String
+ includeAllConfidenceScores: arguments["includeAllConfidenceScores"] as? Bool ?? false
) { outcome in
deliver(outcome, to: result, channelValue: \.channelValue)
}
@@ -65,24 +61,12 @@ final class TextRecognitionChannelAdapter {
return
}
recognizer.detectTextRegions(
- imagePath: imagePath,
- requestId: arguments["requestId"] as? String
+ imagePath: imagePath
) { outcome in
deliver(outcome, to: result, channelValue: \.channelValue)
}
}
- private func handleCancelRequest(_ call: FlutterMethodCall, result: FlutterResult) {
- guard let arguments = call.arguments as? [String: Any],
- let requestId = arguments["requestId"] as? String, !requestId.isEmpty
- else {
- result(invalidArgument("requestId is required"))
- return
- }
- recognizer.cancel(requestId: requestId)
- result(nil)
- }
-
private static let channelName = "io.ente.photos.vision/text_recognition"
}
@@ -114,8 +98,6 @@ extension TextRecognitionError {
FlutterError(code: "IMAGE_DECODE_ERROR", message: "Failed to load image from path", details: nil)
case .imageBitmapUnavailable:
FlutterError(code: "IMAGE_DECODE_ERROR", message: "Failed to get CGImage", details: nil)
- case .cancelled:
- FlutterError(code: "CANCELLED", message: "OCR request was cancelled", details: nil)
case .detectionFailed(let error):
FlutterError(
code: "DETECTION_ERROR",
diff --git a/mobile/apps/photos/plugins/ente_vision/ios/Sources/EnteVisionCore/TextRecognizer.swift b/mobile/apps/photos/plugins/ente_vision/ios/Sources/EnteVisionCore/TextRecognizer.swift
index 75db29e61c..dc9ebc0b2d 100644
--- a/mobile/apps/photos/plugins/ente_vision/ios/Sources/EnteVisionCore/TextRecognizer.swift
+++ b/mobile/apps/photos/plugins/ente_vision/ios/Sources/EnteVisionCore/TextRecognizer.swift
@@ -36,28 +36,22 @@ enum TextRecognitionError: Error {
case imageNotFound
case imageUndecodable
case imageBitmapUnavailable
- case cancelled
case detectionFailed(Error)
case recognitionFailed(Error)
}
-final class TextRecognizer: @unchecked Sendable {
- private let lock = NSLock()
- private var activeRequests: [RequestKey: RequestState] = [:]
+final class TextRecognizer: Sendable {
private let queue = DispatchQueue.global(qos: .userInitiated)
func recognizeText(
imagePath: String,
includeAllConfidenceScores: Bool,
- requestId: String?,
completion: @escaping @Sendable (Result<TextRecognitionResult, TextRecognitionError>) -> Void
) {
let minimumConfidence: Float = includeAllConfidenceScores ? 0 : Self.minimumConfidence
- run(requestId: requestId, foreignError: { .recognitionFailed($0) }, completion: completion) {
- [self] state in
- let image = try loadOrientedImage(atPath: imagePath, state: state)
+ run(foreignError: { .recognitionFailed($0) }, completion: completion) {
+ let image = try Self.loadOrientedImage(atPath: imagePath)
let request = Self.makeRecognizeTextRequest()
- try attach(request, to: state)
try VNImageRequestHandler(cgImage: image.bitmap, options: [:]).perform([request])
let size = image.pixelSize
let blocks = (request.results ?? []).compactMap { observation -> RecognizedTextBlock? in
@@ -83,17 +77,14 @@ final class TextRecognizer: @unchecked Sendable {
func detectTextRegions(
imagePath: String,
- requestId: String?,
completion: @escaping @Sendable (Result<TextRegionsResult, TextRecognitionError>) -> Void
) {
- run(requestId: requestId, foreignError: { .detectionFailed($0) }, completion: completion) {
- [self] state in
- let image = try loadOrientedImage(atPath: imagePath, state: state)
+ run(foreignError: { .detectionFailed($0) }, completion: completion) {
+ let image = try Self.loadOrientedImage(atPath: imagePath)
let detectionBitmap =
Self.downscaled(image.image, longestSide: Self.regionDetectionLongestSide) ?? image.bitmap
let request = VNDetectTextRectanglesRequest()
request.reportCharacterBoxes = false
- try attach(request, to: state)
try VNImageRequestHandler(cgImage: detectionBitmap, options: [:]).perform([request])
let size = image.pixelSize
let regions = (request.results ?? []).map { observation in
@@ -107,90 +98,23 @@ final class TextRecognizer: @unchecked Sendable {
}
}
- func cancel(requestId: String) {
- lock.lock()
- let state = activeRequests.removeValue(forKey: .client(requestId))
- state?.isCancelled = true
- let request = state?.request
- lock.unlock()
- request?.cancel()
- }
-
- func cancelAll() {
- lock.lock()
- let states = Array(activeRequests.values)
- activeRequests.removeAll()
- for state in states {
- state.isCancelled = true
- }
- let requests = states.compactMap(\.request)
- lock.unlock()
- for request in requests {
- request.cancel()
- }
- }
-
private func run<Value>(
- requestId: String?,
foreignError: @escaping @Sendable (Error) -> TextRecognitionError,
completion: @escaping @Sendable (Result<Value, TextRecognitionError>) -> Void,
- _ work: @escaping @Sendable (RequestState) throws -> Value
+ _ work: @escaping @Sendable () throws -> Value
) {
- let state = begin(requestId: requestId)
queue.async {
- let outcome = Result { try work(state) }
+ let outcome = Result { try work() }
.mapError { $0 as? TextRecognitionError ?? foreignError($0) }
- completion(self.finish(state, outcome))
- }
- }
-
- private func begin(requestId: String?) -> RequestState {
- let state = RequestState(key: requestId.map(RequestKey.client) ?? .anonymous(UUID()))
- lock.lock()
- let previous = activeRequests.updateValue(state, forKey: state.key)
- previous?.isCancelled = true
- let previousRequest = previous?.request
- lock.unlock()
- previousRequest?.cancel()
- return state
- }
-
- private func attach(_ request: VNRequest, to state: RequestState) throws {
- lock.lock()
- defer { lock.unlock() }
- guard !state.isCancelled else { throw TextRecognitionError.cancelled }
- state.request = request
- }
-
- private func checkNotCancelled(_ state: RequestState) throws {
- lock.lock()
- defer { lock.unlock() }
- if state.isCancelled {
- throw TextRecognitionError.cancelled
- }
- }
-
- private func finish<Value>(
- _ state: RequestState,
- _ outcome: Result<Value, TextRecognitionError>
- ) -> Result<Value, TextRecognitionError> {
- lock.lock()
- defer { lock.unlock() }
- if activeRequests[state.key] === state {
- activeRequests.removeValue(forKey: state.key)
+ completion(outcome)
}
- state.request = nil
- return state.isCancelled ? .failure(.cancelled) : outcome
}
- private func loadOrientedImage(atPath path: String, state: RequestState) throws -> OrientedImage {
- try checkNotCancelled(state)
+ private static func loadOrientedImage(atPath path: String) throws -> OrientedImage {
guard FileManager.default.fileExists(atPath: path) else { throw TextRecognitionError.imageNotFound }
guard let image = UIImage(contentsOfFile: path) else { throw TextRecognitionError.imageUndecodable }
- try checkNotCancelled(state)
let oriented = Self.orientedUp(image)
guard let bitmap = oriented.cgImage else { throw TextRecognitionError.imageBitmapUnavailable }
- try checkNotCancelled(state)
return OrientedImage(image: oriented, bitmap: bitmap)
}
@@ -264,21 +188,6 @@ final class TextRecognizer: @unchecked Sendable {
private static let regionDetectionLongestSide: CGFloat = 1024
}
-private enum RequestKey: Hashable {
- case client(String)
- case anonymous(UUID)
-}
-
-private final class RequestState: @unchecked Sendable {
- let key: RequestKey
- var request: VNRequest?
- var isCancelled = false
-
- init(key: RequestKey) {
- self.key = key
- }
-}
-
private struct OrientedImage {
let image: UIImage
let bitmap: CGImage
diff --git a/mobile/apps/photos/plugins/ente_vision/lib/src/vision_text_recognizer.dart b/mobile/apps/photos/plugins/ente_vision/lib/src/vision_text_recognizer.dart
index 5e081d7391..b8d15606b4 100644
--- a/mobile/apps/photos/plugins/ente_vision/lib/src/vision_text_recognizer.dart
+++ b/mobile/apps/photos/plugins/ente_vision/lib/src/vision_text_recognizer.dart
@@ -12,31 +12,22 @@ class VisionTextRecognizer {
Future<Map<dynamic, dynamic>> detectText({
required String imagePath,
bool includeAllConfidenceScores = false,
- String? requestId,
}) async {
final result = await _methodChannel
.invokeMapMethod<dynamic, dynamic>('textRecognition.detectText', {
'imagePath': imagePath,
'includeAllConfidenceScores': includeAllConfidenceScores,
- 'requestId': requestId,
});
return result ?? const {};
}
Future<Map<dynamic, dynamic>> detectTextRegions({
required String imagePath,
- String? requestId,
}) async {
final result = await _methodChannel.invokeMapMethod<dynamic, dynamic>(
'textRecognition.detectTextRegions',
- {'imagePath': imagePath, 'requestId': requestId},
+ {'imagePath': imagePath},
);
return result ?? const {};
}
-
- Future<void> cancelRequest(String requestId) {
- return _methodChannel.invokeMethod<void>('textRecognition.cancelRequest', {
- 'requestId': requestId,
- });
- }
}
diff --git a/rust/bindings/frb/photos/src/api/ocr_api.rs b/rust/bindings/frb/photos/src/api/ocr_api.rs
index abf1fee8de..482056d542 100644
--- a/rust/bindings/frb/photos/src/api/ocr_api.rs
+++ b/rust/bindings/frb/photos/src/api/ocr_api.rs
@@ -50,7 +50,6 @@ pub struct RustTextRegionDetectionResult {
pub enum RustOcrError {
ImageNotFound { message: String },
InvalidImage { message: String },
- Cancelled,
CorruptModel { message: String },
Other { message: String },
}
@@ -100,12 +99,10 @@ impl OcrEngine {
&self,
image_path: String,
include_all_confidence_scores: bool,
- request_id: Option<String>,
) -> Result<RustTextDetectionResult, RustOcrError> {
let request = ocr::DetectTextRequest {
image_path,
include_all_confidence_scores,
- request_id,
};
self.inner
.detect_text(&request)
@@ -116,30 +113,19 @@ impl OcrEngine {
pub fn detect_text_regions(
&self,
image_path: String,
- request_id: Option<String>,
) -> Result<RustTextRegionDetectionResult, RustOcrError> {
- let request = ocr::DetectRegionsRequest {
- image_path,
- request_id,
- };
+ let request = ocr::DetectRegionsRequest { image_path };
self.inner
.detect_text_regions(&request)
.map(to_api_text_region_detection_result)
.map_err(|error| logged_failure("detect_text_regions", &request.image_path, error))
}
-
- #[frb(sync)]
- pub fn cancel(&self, request_id: String) {
- self.inner.cancel(&request_id);
- }
}
impl RustOcrError {
fn log_level(&self) -> log::Level {
match self {
- Self::ImageNotFound { .. } | Self::InvalidImage { .. } | Self::Cancelled => {
- log::Level::Warn
- }
+ Self::ImageNotFound { .. } | Self::InvalidImage { .. } => log::Level::Warn,
Self::CorruptModel { .. } | Self::Other { .. } => log::Level::Error,
}
}
@@ -148,7 +134,6 @@ impl RustOcrError {
match self {
Self::ImageNotFound { .. } => "image not found".to_string(),
Self::InvalidImage { message } => format!("invalid image: {message}"),
- Self::Cancelled => "cancelled".to_string(),
Self::CorruptModel { message } => format!("corrupt model: {message}"),
Self::Other { message } => message.clone(),
}
@@ -161,7 +146,6 @@ impl From<ocr::OcrError> for RustOcrError {
error @ ocr::OcrError::ImageNotFound(_) => Self::ImageNotFound {
message: error.to_string(),
},
- ocr::OcrError::Cancelled => Self::Cancelled,
ocr::OcrError::Ml(MlError::Decode(message) | MlError::Image(message)) => {
Self::InvalidImage { message }
}
diff --git a/rust/crates/ml/examples/ocr.rs b/rust/crates/ml/examples/ocr.rs
index 284c09915c..16364c0957 100644
--- a/rust/crates/ml/examples/ocr.rs
+++ b/rust/crates/ml/examples/ocr.rs
@@ -125,7 +125,6 @@ fn detect_text(
let request = DetectTextRequest {
image_path: image_path.to_string(),
include_all_confidence_scores: all_confidences,
- request_id: None,
};
let started = Instant::now();
match dump_dir {
@@ -171,7 +170,6 @@ fn detect_regions(engine: &OcrEngine, image_path: &str, dump_dir: Option<&Path>)
let started = Instant::now();
let debug = engine.detect_text_regions_debug(&DetectRegionsRequest {
image_path: image_path.to_string(),
- request_id: None,
})?;
let elapsed_ms = started.elapsed().as_millis();
let result = &debug.result;
diff --git a/rust/crates/ml/src/ocr/cancel.rs b/rust/crates/ml/src/ocr/cancel.rs
deleted file mode 100644
index 37cbb9100e..0000000000
--- a/rust/crates/ml/src/ocr/cancel.rs
+++ /dev/null
@@ -1,129 +0,0 @@
-use std::collections::HashMap;
-use std::sync::atomic::{AtomicBool, Ordering};
-use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
-
-use super::OcrError;
-
-type Flags = HashMap<String, Arc<AtomicBool>>;
-
-#[derive(Default)]
-pub(crate) struct RequestRegistry {
- flags: Mutex<Flags>,
-}
-
-impl RequestRegistry {
- pub(crate) fn begin(&self, request_id: Option<&str>) -> RequestGuard<'_> {
- let flag = Arc::new(AtomicBool::new(false));
- if let Some(id) = request_id
- && let Some(replaced) = self.flags().insert(id.to_string(), Arc::clone(&flag))
- {
- replaced.store(true, Ordering::SeqCst);
- }
- RequestGuard {
- registry: self,
- request_id: request_id.map(str::to_string),
- flag,
- }
- }
-
- pub(crate) fn cancel(&self, request_id: &str) {
- if let Some(flag) = self.flags().get(request_id) {
- flag.store(true, Ordering::SeqCst);
- }
- }
-
- fn finish(&self, request_id: &str, flag: &Arc<AtomicBool>) {
- let mut flags = self.flags();
- if flags
- .get(request_id)
- .is_some_and(|current| Arc::ptr_eq(current, flag))
- {
- flags.remove(request_id);
- }
- }
-
- fn flags(&self) -> MutexGuard<'_, Flags> {
- self.flags.lock().unwrap_or_else(PoisonError::into_inner)
- }
-}
-
-pub(crate) struct RequestGuard<'a> {
- registry: &'a RequestRegistry,
- request_id: Option<String>,
- flag: Arc<AtomicBool>,
-}
-
-impl RequestGuard<'_> {
- pub(crate) fn check(&self) -> Result<(), OcrError> {
- if self.flag.load(Ordering::SeqCst) {
- Err(OcrError::Cancelled)
- } else {
- Ok(())
- }
- }
-}
-
-impl Drop for RequestGuard<'_> {
- fn drop(&mut self) {
- if let Some(request_id) = &self.request_id {
- self.registry.finish(request_id, &self.flag);
- }
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- fn is_cancelled(guard: &RequestGuard<'_>) -> bool {
- matches!(guard.check(), Err(OcrError::Cancelled))
- }
-
- #[test]
- fn cancel_marks_the_registered_request() {
- let registry = RequestRegistry::default();
- let guard = registry.begin(Some("req-1"));
- assert!(guard.check().is_ok());
-
- registry.cancel("req-1");
-
- assert!(is_cancelled(&guard));
- }
-
- #[test]
- fn cancelling_an_unknown_or_anonymous_request_is_a_no_op() {
- let registry = RequestRegistry::default();
- let anonymous = registry.begin(None);
-
- registry.cancel("missing");
-
- assert!(anonymous.check().is_ok());
- assert!(registry.flags().is_empty());
- }
-
- #[test]
- fn a_new_request_with_the_same_id_cancels_and_replaces_the_older_one() {
- let registry = RequestRegistry::default();
- let first = registry.begin(Some("shared"));
- let second = registry.begin(Some("shared"));
-
- assert!(is_cancelled(&first));
- assert!(second.check().is_ok());
-
- drop(first);
- assert_eq!(registry.flags().len(), 1);
- registry.cancel("shared");
- assert!(is_cancelled(&second));
- }
-
- #[test]
- fn finishing_a_request_removes_its_flag() {
- let registry = RequestRegistry::default();
- let guard = registry.begin(Some("done"));
- assert_eq!(registry.flags().len(), 1);
-
- drop(guard);
-
- assert!(registry.flags().is_empty());
- }
-}
diff --git a/rust/crates/ml/src/ocr/mod.rs b/rust/crates/ml/src/ocr/mod.rs
index 06f139bf6e..cb95a98b54 100644
--- a/rust/crates/ml/src/ocr/mod.rs
+++ b/rust/crates/ml/src/ocr/mod.rs
@@ -1,5 +1,4 @@
pub mod assets;
-mod cancel;
mod characters;
mod classify;
mod context;
@@ -20,7 +19,6 @@ use std::time::Instant;
use thiserror::Error;
-use cancel::{RequestGuard, RequestRegistry};
use characters::character_boxes;
use classify::AngleClassifier;
use crop::{TextCrop, crop_text};
@@ -51,13 +49,11 @@ pub struct OcrModelPaths {
pub struct DetectTextRequest {
pub image_path: String,
pub include_all_confidence_scores: bool,
- pub request_id: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DetectRegionsRequest {
pub image_path: String,
- pub request_id: Option<String>,
}
#[derive(Clone, Debug, PartialEq)]
@@ -127,8 +123,6 @@ pub struct TextDetectionDebug {
pub enum OcrError {
#[error("image not found: {0}")]
ImageNotFound(String),
- #[error("cancelled")]
- Cancelled,
#[error(transparent)]
Ml(#[from] MlError),
}
@@ -138,7 +132,6 @@ pub struct OcrEngine {
classifier: Option<AngleClassifier>,
recognizer: Option<TextRecognizer>,
missing_text_model_paths: Vec<&'static str>,
- requests: RequestRegistry,
}
impl OcrEngine {
@@ -157,7 +150,6 @@ impl OcrEngine {
classifier,
recognizer,
missing_text_model_paths: missing_text_model_paths(&paths),
- requests: RequestRegistry::default(),
})
}
@@ -208,21 +200,15 @@ impl OcrEngine {
fn run_text_pipeline(&self, req: &DetectTextRequest) -> Result<TextPipeline, OcrError> {
let (classifier, recognizer) = self.text_models()?;
let started = Instant::now();
- let request = self.requests.begin(req.request_id.as_deref());
let mut source = load_source(&req.image_path, FULL_TEXT_CAP)?;
- request.check()?;
let detection_started = Instant::now();
let detection = self.detector.detect(&source.working)?;
let detection_ms = detection_started.elapsed().as_millis();
- request.check()?;
let crops = crop_candidates(&mut source.working, &detection.candidates)?;
- request.check()?;
let recognition_started = Instant::now();
- let mut recognized = recognize_crops(recognizer, crops, &request)?;
- request.check()?;
- let retries = retry_low_confidence(classifier, recognizer, &mut recognized, &request)?;
+ let mut recognized = recognize_crops(recognizer, crops)?;
+ let retries = retry_low_confidence(classifier, recognizer, &mut recognized)?;
let recognition_ms = recognition_started.elapsed().as_millis();
- request.check()?;
let blocks = text_blocks(
&detection.candidates,
&recognized,
@@ -264,13 +250,10 @@ impl OcrEngine {
req: &DetectRegionsRequest,
) -> Result<RegionDetectionDebug, OcrError> {
let started = Instant::now();
- let request = self.requests.begin(req.request_id.as_deref());
let source = load_source(&req.image_path, REGIONS_CAP)?;
- request.check()?;
let detection_started = Instant::now();
let detection = self.detector.detect(&source.working)?;
let detection_ms = detection_started.elapsed().as_millis();
- request.check()?;
let regions = regions_in_decoded_pixels(&detection.candidates, &source);
log::info!(
"ocr detect_text_regions: {}x{} -> {}x{}, det {} boxes in {detection_ms}ms, kept {}, total {}ms",
@@ -293,10 +276,6 @@ impl OcrEngine {
probability_map: detection.probability_map,
})
}
-
- pub fn cancel(&self, request_id: &str) {
- self.requests.cancel(request_id);
- }
}
fn missing_text_model_paths(paths: &OcrModelPaths) -> Vec<&'static str> {
@@ -448,8 +427,7 @@ fn crop_candidates(
fn recognize_crops(
recognizer: &TextRecognizer,
crops: Vec<TextCrop>,
- request: &RequestGuard<'_>,
-) -> Result<Vec<RecognizedCrop>, OcrError> {
+) -> MlResult<Vec<RecognizedCrop>> {
let flipped = crops
.iter()
.map(flipped_if_vertical)
@@ -459,7 +437,7 @@ fn recognize_crops(
.zip(&flipped)
.flat_map(|(crop, flipped)| std::iter::once(&crop.image).chain(flipped))
.collect();
- let mut results = recognizer.recognize(&inputs, request)?.into_iter();
+ let mut results = recognizer.recognize(&inputs)?.into_iter();
crops
.into_iter()
.zip(flipped)
@@ -492,8 +470,7 @@ fn retry_low_confidence(
classifier: &AngleClassifier,
recognizer: &TextRecognizer,
crops: &mut [RecognizedCrop],
- request: &RequestGuard<'_>,
-) -> Result<usize, OcrError> {
+) -> MlResult<usize> {
let indices: Vec<usize> = crops
.iter()
.enumerate()
@@ -508,7 +485,6 @@ fn retry_low_confidence(
.map(|&index| crops[index].image.clone())
.collect();
let decisions = classifier.classify(&mut images)?;
- request.check()?;
let flipped: Vec<(usize, ImageU8)> = indices
.iter()
.copied()
@@ -518,7 +494,7 @@ fn retry_low_confidence(
.map(|(flipped, _)| flipped)
.collect();
let inputs: Vec<&ImageU8> = flipped.iter().map(|(_, image)| image).collect();
- let results = recognizer.recognize(&inputs, request)?;
+ let results = recognizer.recognize(&inputs)?;
for ((index, image), retry) in flipped.into_iter().zip(results) {
crops[index].prefer_rotated(image, retry);
}
@@ -684,7 +660,6 @@ mod tests {
let error = detector_only_engine()
.detect_text_regions(&DetectRegionsRequest {
image_path: "missing/image.jpg".to_string(),
- request_id: Some("r1".to_string()),
})
.unwrap_err();
assert!(matches!(error, OcrError::ImageNotFound(_)), "{error}");
@@ -703,7 +678,6 @@ mod tests {
.detect_text(&DetectTextRequest {
image_path: "missing/image.jpg".to_string(),
include_all_confidence_scores: false,
- request_id: None,
})
.unwrap_err();
let OcrError::Ml(MlError::InvalidRequest(message)) = error else {
diff --git a/rust/crates/ml/src/ocr/recognize.rs b/rust/crates/ml/src/ocr/recognize.rs
index c1018122df..4b865dfad3 100644
--- a/rust/crates/ml/src/ocr/recognize.rs
+++ b/rust/crates/ml/src/ocr/recognize.rs
@@ -1,8 +1,6 @@
use std::path::Path;
use std::sync::{Mutex, OnceLock, PoisonError};
-use super::OcrError;
-use super::cancel::RequestGuard;
use super::dictionary::load_dictionary;
use super::tensor::{BgrNormalization, prepare_crops, write_bgr_planes};
use crate::cv;
@@ -85,18 +83,14 @@ impl TextRecognizer {
.unload();
}
- pub(crate) fn recognize(
- &self,
- crops: &[&ImageU8],
- request: &RequestGuard<'_>,
- ) -> Result<Vec<Recognition>, OcrError> {
+ pub(crate) fn recognize(&self, crops: &[&ImageU8]) -> MlResult<Vec<Recognition>> {
if crops.is_empty() {
return Ok(Vec::new());
}
let dictionary = self.dictionary.entries()?;
let mut lines = Vec::with_capacity(crops.len());
let mut crop_indices = Vec::with_capacity(crops.len());
- for batch in plan_batches(crops, request)? {
+ for batch in plan_batches(crops)? {
for (&index, &content_width) in batch.indices.iter().zip(&batch.layout.content_widths) {
crop_indices.push(index);
lines.push(super::context::Line {
@@ -110,7 +104,6 @@ impl TextRecognizer {
let mut pending: Vec<usize> = (0..lines.len()).collect();
pending.sort_by_key(|&i| std::cmp::Reverse(lines[i].width));
while !pending.is_empty() {
- request.check()?;
let width = choose_packed_width(&pending, &lines);
let slots = width / 336;
let mut selected = Vec::new();
@@ -126,9 +119,9 @@ impl TextRecognizer {
}
});
if selected.is_empty() {
- return Err(
- MlError::Preprocess("line exceeds fixed packed canvas".to_owned()).into(),
- );
+ return Err(MlError::Preprocess(
+ "line exceeds fixed packed canvas".to_owned(),
+ ));
}
prepare_crops(|| {
for &index in &selected {
@@ -207,13 +200,9 @@ struct PlannedBatch {
layout: BatchLayout,
}
-fn plan_batches(
- crops: &[&ImageU8],
- request: &RequestGuard<'_>,
-) -> Result<Vec<PlannedBatch>, OcrError> {
+fn plan_batches(crops: &[&ImageU8]) -> MlResult<Vec<PlannedBatch>> {
let mut batches = Vec::new();
for indices in ascending_aspect_order(crops).chunks(REC_BATCH_SIZE) {
- request.check()?;
let batch: Vec<&ImageU8> = indices.iter().map(|&index| crops[index]).collect();
let layout = BatchLayout::new(&batch)?;
let initial_width = if batch.len() * layout.target_width as usize <= REC_BATCH_COLUMNS {
@@ -223,7 +212,6 @@ fn plan_batches(
};
let mut start = 0;
while start < batch.len() {
- request.check()?;
let mut end = start;
let mut target_width = initial_width;
for &content_width in &layout.content_widths[start..] {
@@ -474,7 +462,6 @@ fn choose_packed_width(pending: &[usize], lines: &[super::context::Line]) -> usi
#[cfg(test)]
mod tests {
use super::*;
- use crate::ocr::cancel::RequestRegistry;
use crate::onnx::FloatTensorData;
#[derive(Debug)]
struct SequenceOutput<'a, T> {
@@ -590,12 +577,10 @@ mod tests {
fn run_planned_batches(
crops: &[&ImageU8],
- request: &RequestGuard<'_>,
mut infer_batch: impl FnMut(&[&ImageU8], &BatchLayout) -> MlResult<Vec<Recognition>>,
- ) -> Result<Vec<Recognition>, OcrError> {
+ ) -> MlResult<Vec<Recognition>> {
let mut results = vec![Recognition::default(); crops.len()];
- for batch in plan_batches(crops, request)? {
- request.check()?;
+ for batch in plan_batches(crops)? {
let images: Vec<_> = batch.indices.iter().map(|&index| crops[index]).collect();
let recognized = infer_batch(&images, &batch.layout)?;
assert_eq!(recognized.len(), images.len());
@@ -916,22 +901,19 @@ mod tests {
fn crops_are_batched_by_ascending_aspect_and_restored_to_input_order() {
let widths = [80, 10, 60, 30, 70, 20, 50, 40];
let crops: Vec<ImageU8> = widths.iter().map(|&w| solid(w, 10, [0; 3])).collect();
- let registry = RequestRegistry::default();
- let request = registry.begin(None);
let mut batches = Vec::new();
- let results =
- run_planned_batches(&crops.iter().collect::<Vec<_>>(), &request, |batch, _| {
- batches.push(batch.iter().map(|crop| crop.width).collect::<Vec<_>>());
- Ok(batch
- .iter()
- .map(|crop| Recognition {
- text: crop.width.to_string(),
- ..Recognition::default()
- })
- .collect())
- })
- .unwrap();
+ let results = run_planned_batches(&crops.iter().collect::<Vec<_>>(), |batch, _| {
+ batches.push(batch.iter().map(|crop| crop.width).collect::<Vec<_>>());
+ Ok(batch
+ .iter()
+ .map(|crop| Recognition {
+ text: crop.width.to_string(),
+ ..Recognition::default()
+ })
+ .collect())
+ })
+ .unwrap();
assert_eq!(batches, [vec![10, 20, 30, 40, 50, 60], vec![70, 80]]);
let texts: Vec<String> = results.into_iter().map(|r| r.text).collect();
@@ -939,18 +921,6 @@ mod tests {
assert_eq!(texts, expected);
}
- #[test]
- fn a_cancelled_request_stops_before_the_first_batch() {
- let crop = solid(4, 4, [0; 3]);
- let registry = RequestRegistry::default();
- let request = registry.begin(Some("cancelled"));
- registry.cancel("cancelled");
-
- let error = run_planned_batches(&[&crop], &request, |_, _| unreachable!()).unwrap_err();
-
- assert!(matches!(error, OcrError::Cancelled), "{error}");
- }
-
fn legacy_tensor(batch: &[&ImageU8]) -> Vec<f32> {
let ratio = batch
.iter()
@@ -1036,10 +1006,8 @@ mod tests {
let original_layout = BatchLayout::new(&sorted).unwrap();
let original_width = original_layout.target_width as usize;
let mut expected_rows = expected.chunks_exact(original_width);
- let registry = RequestRegistry::default();
- let request = registry.begin(None);
let mut shapes = Vec::new();
- let results = run_planned_batches(&references, &request, |batch, layout| {
+ let results = run_planned_batches(&references, |batch, layout| {
assert!(batch.len() <= REC_BATCH_SIZE);
assert!(batch.len() * layout.target_width as usize <= REC_BATCH_COLUMNS);
shapes.push((batch.len(), layout.target_width));
@@ -1074,33 +1042,21 @@ mod tests {
}
#[test]
- fn oversized_outlier_does_not_expand_narrow_crops_or_delay_cancellation() {
+ fn oversized_outlier_does_not_expand_narrow_crops() {
let narrow = solid(320, 48, [127; 3]);
let outlier = solid(4096, 1, [0; 3]);
let crops = [&outlier, &narrow, &narrow, &narrow, &narrow, &narrow];
- for cancel in [false, true] {
- let registry = RequestRegistry::default();
- let request = registry.begin(Some("mixed"));
- let mut shapes = Vec::new();
- let result = run_planned_batches(&crops, &request, |batch, layout| {
- shapes.push((batch.len(), layout.target_width));
- assert_eq!(
- layout.tensor(batch)?.len(),
- batch.len() * 3 * 48 * layout.target_width as usize
- );
- if cancel {
- registry.cancel("mixed");
- }
- Ok(vec![Recognition::default(); batch.len()])
- });
- if cancel {
- assert!(matches!(result, Err(OcrError::Cancelled)));
- assert_eq!(shapes, [(5, 320)]);
- } else {
- assert_eq!(result.unwrap().len(), crops.len());
- assert_eq!(shapes, [(5, 320), (1, 7168)]);
- }
- }
+ let mut shapes = Vec::new();
+ let result = run_planned_batches(&crops, |batch, layout| {
+ shapes.push((batch.len(), layout.target_width));
+ assert_eq!(
+ layout.tensor(batch)?.len(),
+ batch.len() * 3 * 48 * layout.target_width as usize
+ );
+ Ok(vec![Recognition::default(); batch.len()])
+ });
+ assert_eq!(result.unwrap().len(), crops.len());
+ assert_eq!(shapes, [(5, 320), (1, 7168)]);
}
#[test]
@@ -1213,15 +1169,13 @@ mod tests {
}
#[test]
- fn extreme_aspect_batches_are_bounded_and_cancellation_stops_the_next_split() {
+ fn extreme_aspect_batches_are_bounded() {
let crop = solid(4096, 1, [0; 3]);
let crops = [&crop; 6];
let layout = BatchLayout::new(&crops).unwrap();
assert!(matches!(layout.tensor(&crops), Err(MlError::Preprocess(_))));
- let registry = RequestRegistry::default();
- let request = registry.begin(None);
let mut calls = 0;
- run_planned_batches(&crops, &request, |batch, layout| {
+ run_planned_batches(&crops, |batch, layout| {
calls += 1;
assert_eq!(batch.len(), 1);
assert_eq!(layout.tensor(batch)?.len(), 3 * 48 * 7168);
@@ -1229,16 +1183,5 @@ mod tests {
})
.unwrap();
assert_eq!(calls, 6);
-
- let request = registry.begin(Some("split"));
- calls = 0;
- let error = run_planned_batches(&crops, &request, |_, _| {
- calls += 1;
- registry.cancel("split");
- Ok(vec![Recognition::default()])
- })
- .unwrap_err();
- assert_eq!(calls, 1);
- assert!(matches!(error, OcrError::Cancelled));
}
}