Harden API memory and CPU usage across Phases 19–22.
Bound list/PDF payloads, stream MinIO I/O with cancellation, paginate history and FHIR $everything, tighten OCR lifetimes/buffers, and add phase tracking docs.
This commit is contained in:
@@ -2,7 +2,7 @@ using System.Text.RegularExpressions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Tesseract;
|
||||
|
||||
public class TesseractOcrService : IOcrService
|
||||
public class TesseractOcrService : IOcrService, IDisposable
|
||||
{
|
||||
private static readonly (Regex Pattern, string FieldName)[] ClinicalPatterns =
|
||||
[
|
||||
@@ -25,6 +25,9 @@ public class TesseractOcrService : IOcrService
|
||||
private readonly TesseractOcrOptions _options;
|
||||
private readonly ImagePreprocessor _preprocessor;
|
||||
private readonly ILogger<TesseractOcrService> _logger;
|
||||
private readonly object _engineLock = new();
|
||||
private TesseractEngine? _engine;
|
||||
private bool _disposed;
|
||||
|
||||
public TesseractOcrService(
|
||||
IOptions<OcrOptions> options,
|
||||
@@ -37,28 +40,57 @@ public class TesseractOcrService : IOcrService
|
||||
}
|
||||
|
||||
public async Task<OcrExtractionResult> ExtractAsync(
|
||||
Stream documentStream, string contentType)
|
||||
Stream documentStream, string contentType, CancellationToken ct = default)
|
||||
{
|
||||
var sw = System.Diagnostics.Stopwatch.StartNew();
|
||||
|
||||
using var preprocessed = _preprocessor.Preprocess(documentStream, contentType);
|
||||
using var memStream = new MemoryStream();
|
||||
await preprocessed.CopyToAsync(memStream);
|
||||
var imageBytes = memStream.ToArray();
|
||||
await using var preprocessed = await _preprocessor.PreprocessAsync(
|
||||
documentStream, contentType, ct);
|
||||
|
||||
using var engine = new TesseractEngine(
|
||||
_options.DataPath, _options.Language, EngineMode.Default);
|
||||
using var pix = Pix.LoadFromMemory(imageBytes);
|
||||
using var page = engine.Process(pix);
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
byte[] imageBytes;
|
||||
if (preprocessed is MemoryStream ms)
|
||||
{
|
||||
imageBytes = ms.ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
using var copy = new MemoryStream();
|
||||
await preprocessed.CopyToAsync(copy, ct);
|
||||
imageBytes = copy.ToArray();
|
||||
}
|
||||
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
string rawText;
|
||||
float meanConfidence;
|
||||
lock (_engineLock)
|
||||
{
|
||||
var engine = GetEngine();
|
||||
using var pix = Pix.LoadFromMemory(imageBytes);
|
||||
using var page = engine.Process(pix);
|
||||
rawText = page.GetText();
|
||||
meanConfidence = page.GetMeanConfidence();
|
||||
}
|
||||
|
||||
var rawText = page.GetText();
|
||||
var meanConfidence = page.GetMeanConfidence();
|
||||
var fields = ParseClinicalText(rawText, meanConfidence);
|
||||
|
||||
sw.Stop();
|
||||
_logger.LogDebug(
|
||||
"Tesseract OCR finished in {DurationMs}ms with {FieldCount} fields",
|
||||
sw.ElapsedMilliseconds, fields.Count);
|
||||
|
||||
return new OcrExtractionResult(fields, rawText, (int)sw.ElapsedMilliseconds);
|
||||
}
|
||||
|
||||
private TesseractEngine GetEngine()
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
return _engine ??= new TesseractEngine(
|
||||
_options.DataPath, _options.Language, EngineMode.Default);
|
||||
}
|
||||
|
||||
private static List<OcrExtractedField> ParseClinicalText(string text, float meanConfidence)
|
||||
{
|
||||
var fields = new List<OcrExtractedField>();
|
||||
@@ -87,4 +119,13 @@ public class TesseractOcrService : IOcrService
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
return;
|
||||
_disposed = true;
|
||||
_engine?.Dispose();
|
||||
_engine = null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user