From 20d8803f54ba9e99ae8cedb8f9a3ff787937e7e2 Mon Sep 17 00:00:00 2001 From: MaddoScientisto Date: Sat, 9 May 2026 19:15:06 +0200 Subject: [PATCH] Enhance GPU runtime validation and fallback handling in NumberRecognitionEngine --- .../NumberRecognitionEngine.cs | 48 ++++++++++++++----- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/src/AIFotoONLUS.Core/NumberRecognitionEngine.cs b/src/AIFotoONLUS.Core/NumberRecognitionEngine.cs index 0c1ac43..26a3642 100644 --- a/src/AIFotoONLUS.Core/NumberRecognitionEngine.cs +++ b/src/AIFotoONLUS.Core/NumberRecognitionEngine.cs @@ -43,6 +43,7 @@ namespace AIFotoONLUS.Core public class NumberRecognitionEngine : IDisposable { + private static readonly ConcurrentDictionary ValidatedGpuRuntimeCache = new(StringComparer.OrdinalIgnoreCase); private readonly Net _detectionNet; private readonly Net _recognitionNet; private readonly object _detectionLock = new(); @@ -100,7 +101,7 @@ namespace AIFotoONLUS.Core // Let OpenCV use multiple threads internally (use number of logical processors) try { - Cv2.SetNumThreads(Environment.ProcessorCount); + Cv2.SetNumThreads(_cfg.UseGpu ? 1 : Environment.ProcessorCount); } catch { @@ -109,7 +110,7 @@ namespace AIFotoONLUS.Core if (_cfg.UseGpu) { - ValidateGpuRuntime(); + EnsureGpuRuntimeValidated(); } } @@ -167,7 +168,14 @@ namespace AIFotoONLUS.Core if (useGpu) { net.SetPreferableBackend(Net.Backend.CUDA); - net.SetPreferableTarget(Net.Target.CUDA); + try + { + net.SetPreferableTarget(Net.Target.CUDA_FP16); + } + catch + { + net.SetPreferableTarget(Net.Target.CUDA); + } return; } @@ -175,6 +183,28 @@ namespace AIFotoONLUS.Core net.SetPreferableTarget(Net.Target.CPU); } + private void EnsureGpuRuntimeValidated() + { + var cacheKey = string.Join( + "|", + _cfg.DetectionCfg, + _cfg.DetectionWeights, + _cfg.RecognitionCfg, + _cfg.RecognitionWeights, + _cfg.DetectionInputSize.Width, + _cfg.DetectionInputSize.Height, + _cfg.RecognitionInputSize.Width, + _cfg.RecognitionInputSize.Height); + + if (ValidatedGpuRuntimeCache.ContainsKey(cacheKey)) + { + return; + } + + ValidateGpuRuntime(); + ValidatedGpuRuntimeCache.TryAdd(cacheKey, 0); + } + private void ValidateGpuRuntime() { try @@ -511,12 +541,7 @@ namespace AIFotoONLUS.Core var txt = RecognizeDigits(crop, ctx); if (!string.IsNullOrEmpty(txt)) texts.Add(txt); } - var result = new ImageResult(Path.GetFileName(filePath), string.Join(",", texts), filePath); - if (!string.IsNullOrEmpty(result.Text)) - _logger?.LogInformation("Processed image {file} -> {text}", result.FileName, result.Text); - else - _logger?.LogDebug("Processed image {file} -> (no text)", result.FileName); - return result; + return new ImageResult(Path.GetFileName(filePath), string.Join(",", texts), filePath); } /// @@ -553,6 +578,7 @@ namespace AIFotoONLUS.Core var total = files.Length; var processed = 0; var sw = System.Diagnostics.Stopwatch.StartNew(); + var enableRecoveryFallbacks = !_cfg.UseGpu; // Per-thread nets (each worker gets its own pair) to allow parallel forward calls var netsBag = new ConcurrentBag<(Net detNet, Net recNet)>(); @@ -589,7 +615,7 @@ namespace AIFotoONLUS.Core using var crop = new Mat(image, r.BoundingBox); var ctx = $"{filename}_{r.BoundingBox.X}_{r.BoundingBox.Y}_{r.BoundingBox.Width}x{r.BoundingBox.Height}"; var txt = RecognizeDigits(crop, nets.recNet, ctx); - if (string.IsNullOrEmpty(txt)) + if (enableRecoveryFallbacks && string.IsNullOrEmpty(txt)) { try { @@ -604,7 +630,7 @@ namespace AIFotoONLUS.Core if (!string.IsNullOrEmpty(txt)) texts.Add(txt); } - if (texts.Count == 0) + if (enableRecoveryFallbacks && texts.Count == 0) { try {