Enhance GPU runtime validation and fallback handling in NumberRecognitionEngine

This commit is contained in:
MaddoScientisto 2026-05-09 19:15:06 +02:00
commit 20d8803f54

View file

@ -43,6 +43,7 @@ namespace AIFotoONLUS.Core
public class NumberRecognitionEngine : IDisposable public class NumberRecognitionEngine : IDisposable
{ {
private static readonly ConcurrentDictionary<string, byte> ValidatedGpuRuntimeCache = new(StringComparer.OrdinalIgnoreCase);
private readonly Net _detectionNet; private readonly Net _detectionNet;
private readonly Net _recognitionNet; private readonly Net _recognitionNet;
private readonly object _detectionLock = new(); private readonly object _detectionLock = new();
@ -100,7 +101,7 @@ namespace AIFotoONLUS.Core
// Let OpenCV use multiple threads internally (use number of logical processors) // Let OpenCV use multiple threads internally (use number of logical processors)
try try
{ {
Cv2.SetNumThreads(Environment.ProcessorCount); Cv2.SetNumThreads(_cfg.UseGpu ? 1 : Environment.ProcessorCount);
} }
catch catch
{ {
@ -109,7 +110,7 @@ namespace AIFotoONLUS.Core
if (_cfg.UseGpu) if (_cfg.UseGpu)
{ {
ValidateGpuRuntime(); EnsureGpuRuntimeValidated();
} }
} }
@ -167,7 +168,14 @@ namespace AIFotoONLUS.Core
if (useGpu) if (useGpu)
{ {
net.SetPreferableBackend(Net.Backend.CUDA); net.SetPreferableBackend(Net.Backend.CUDA);
try
{
net.SetPreferableTarget(Net.Target.CUDA_FP16);
}
catch
{
net.SetPreferableTarget(Net.Target.CUDA); net.SetPreferableTarget(Net.Target.CUDA);
}
return; return;
} }
@ -175,6 +183,28 @@ namespace AIFotoONLUS.Core
net.SetPreferableTarget(Net.Target.CPU); 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() private void ValidateGpuRuntime()
{ {
try try
@ -511,12 +541,7 @@ namespace AIFotoONLUS.Core
var txt = RecognizeDigits(crop, ctx); var txt = RecognizeDigits(crop, ctx);
if (!string.IsNullOrEmpty(txt)) texts.Add(txt); if (!string.IsNullOrEmpty(txt)) texts.Add(txt);
} }
var result = new ImageResult(Path.GetFileName(filePath), string.Join(",", texts), filePath); return 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;
} }
/// <summary> /// <summary>
@ -553,6 +578,7 @@ namespace AIFotoONLUS.Core
var total = files.Length; var total = files.Length;
var processed = 0; var processed = 0;
var sw = System.Diagnostics.Stopwatch.StartNew(); var sw = System.Diagnostics.Stopwatch.StartNew();
var enableRecoveryFallbacks = !_cfg.UseGpu;
// Per-thread nets (each worker gets its own pair) to allow parallel forward calls // Per-thread nets (each worker gets its own pair) to allow parallel forward calls
var netsBag = new ConcurrentBag<(Net detNet, Net recNet)>(); var netsBag = new ConcurrentBag<(Net detNet, Net recNet)>();
@ -589,7 +615,7 @@ namespace AIFotoONLUS.Core
using var crop = new Mat(image, r.BoundingBox); 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 ctx = $"{filename}_{r.BoundingBox.X}_{r.BoundingBox.Y}_{r.BoundingBox.Width}x{r.BoundingBox.Height}";
var txt = RecognizeDigits(crop, nets.recNet, ctx); var txt = RecognizeDigits(crop, nets.recNet, ctx);
if (string.IsNullOrEmpty(txt)) if (enableRecoveryFallbacks && string.IsNullOrEmpty(txt))
{ {
try try
{ {
@ -604,7 +630,7 @@ namespace AIFotoONLUS.Core
if (!string.IsNullOrEmpty(txt)) texts.Add(txt); if (!string.IsNullOrEmpty(txt)) texts.Add(txt);
} }
if (texts.Count == 0) if (enableRecoveryFallbacks && texts.Count == 0)
{ {
try try
{ {