diff --git a/src/AIFotoONLUS.Core/AIFotoONLUS.Core.xml b/src/AIFotoONLUS.Core/AIFotoONLUS.Core.xml index 4b6c8f2..39fc26a 100644 --- a/src/AIFotoONLUS.Core/AIFotoONLUS.Core.xml +++ b/src/AIFotoONLUS.Core/AIFotoONLUS.Core.xml @@ -139,12 +139,6 @@ must match the class ordering used by the trained recognition network. - - - When enabled, request OpenCV DNN CUDA backend/target for inference. - The installed OpenCV runtime must have CUDA support or model loading/forwarding may fail. - - When enabled, recognition crops will be saved to disk under diff --git a/src/AIFotoONLUS.Core/ModelConfiguration.cs b/src/AIFotoONLUS.Core/ModelConfiguration.cs index 51d7ac7..0b0b553 100644 --- a/src/AIFotoONLUS.Core/ModelConfiguration.cs +++ b/src/AIFotoONLUS.Core/ModelConfiguration.cs @@ -55,12 +55,6 @@ namespace AIFotoONLUS.Core /// public string[] NumberClasses { get; set; } = new[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; - /// - /// When enabled, request OpenCV DNN CUDA backend/target for inference. - /// The installed OpenCV runtime must have CUDA support or model loading/forwarding may fail. - /// - public bool UseGpu { get; set; } = false; - /// /// When enabled, recognition crops will be saved to disk under /// "logs/crops" for diagnostic inspection. Disabled by default. diff --git a/src/AIFotoONLUS.Core/NumberRecognitionEngine.cs b/src/AIFotoONLUS.Core/NumberRecognitionEngine.cs index d6b2d25..b9b9284 100644 --- a/src/AIFotoONLUS.Core/NumberRecognitionEngine.cs +++ b/src/AIFotoONLUS.Core/NumberRecognitionEngine.cs @@ -95,8 +95,10 @@ namespace AIFotoONLUS.Core _detectionNet = CvDnn.ReadNetFromDarknet(_cfg.DetectionCfg, _cfg.DetectionWeights); _recognitionNet = CvDnn.ReadNetFromDarknet(_cfg.RecognitionCfg, _cfg.RecognitionWeights); - ConfigureNetRuntime(_detectionNet, _cfg.UseGpu); - ConfigureNetRuntime(_recognitionNet, _cfg.UseGpu); + _detectionNet.SetPreferableBackend(Backend.OPENCV); + _detectionNet.SetPreferableTarget(Target.CPU); + _recognitionNet.SetPreferableBackend(Backend.OPENCV); + _recognitionNet.SetPreferableTarget(Target.CPU); // Let OpenCV use multiple threads internally (use number of logical processors) try { @@ -106,11 +108,6 @@ namespace AIFotoONLUS.Core { // Ignore if not supported by OpenCvSharp build } - - if (_cfg.UseGpu) - { - ValidateGpuRuntime(); - } } public void Dispose() @@ -122,38 +119,6 @@ namespace AIFotoONLUS.Core GC.SuppressFinalize(this); } - public static bool TryValidateGpuRuntime(ModelConfiguration cfg, ILogger? logger, out string? failureMessage) - { - if (cfg is null) throw new ArgumentNullException(nameof(cfg)); - - var probeConfiguration = new ModelConfiguration - { - DetectionCfg = cfg.DetectionCfg, - DetectionWeights = cfg.DetectionWeights, - RecognitionCfg = cfg.RecognitionCfg, - RecognitionWeights = cfg.RecognitionWeights, - ConfidenceThreshold = cfg.ConfidenceThreshold, - NmsThreshold = cfg.NmsThreshold, - DetectionInputSize = cfg.DetectionInputSize, - RecognitionInputSize = cfg.RecognitionInputSize, - NumberClasses = cfg.NumberClasses, - EnableCropSaving = cfg.EnableCropSaving, - UseGpu = true - }; - - try - { - using var engine = new NumberRecognitionEngine(probeConfiguration, logger); - failureMessage = null; - return true; - } - catch (Exception ex) - { - failureMessage = ex.GetBaseException().Message; - return false; - } - } - private static string SanitizeFileName(string name) { foreach (var c in Path.GetInvalidFileNameChars()) name = name.Replace(c, '_'); @@ -162,39 +127,6 @@ namespace AIFotoONLUS.Core private string[] GetOutputLayerNames(Net net) => net.GetUnconnectedOutLayersNames(); - private static void ConfigureNetRuntime(Net net, bool useGpu) - { - if (useGpu) - { - net.SetPreferableBackend(Backend.CUDA); - net.SetPreferableTarget(Target.CUDA); - return; - } - - net.SetPreferableBackend(Backend.OPENCV); - net.SetPreferableTarget(Target.CPU); - } - - private void ValidateGpuRuntime() - { - try - { - using var detectionProbe = new Mat(_cfg.DetectionInputSize.Height, _cfg.DetectionInputSize.Width, MatType.CV_8UC3, Scalar.All(0)); - _ = DetectTextRegions(_detectionNet, detectionProbe).Take(1).ToArray(); - - using var recognitionProbe = new Mat(_cfg.RecognitionInputSize.Height, _cfg.RecognitionInputSize.Width, MatType.CV_8UC3, Scalar.All(0)); - using var blob = CvDnn.BlobFromImage(recognitionProbe, 0.00392, _cfg.RecognitionInputSize, new Scalar(0, 0, 0), true, false); - _recognitionNet.SetInput(blob); - using var output = _recognitionNet.Forward(); - } - catch (Exception ex) - { - throw new InvalidOperationException( - "OpenCV DNN CUDA runtime validation failed. Disable number AI GPU mode or use an OpenCV runtime built with CUDA DNN support.", - ex); - } - } - /// /// Detect text regions in the supplied image using the detection network. /// @@ -220,7 +152,7 @@ namespace AIFotoONLUS.Core var outNames = GetOutputLayerNames(detectionNet); var outsList = new List(); detectionNet.Forward(outsList, outNames); - + Mat[] outs = outsList.ToArray(); if (outs.Length == 0) { @@ -230,15 +162,15 @@ namespace AIFotoONLUS.Core var fallback = new List(); for (int on = 0; on < outNames.Length; on++) { - try - { - var single = detectionNet.Forward(outNames[on]); - fallback.Add(single); - } - catch (Exception ex) - { - _logger?.LogError(ex, "Fallback Forward failed for {name}", outNames[on]); - } + try + { + var single = detectionNet.Forward(outNames[on]); + fallback.Add(single); + } + catch (Exception ex) + { + _logger?.LogError(ex, "Fallback Forward failed for {name}", outNames[on]); + } } if (fallback.Count > 0) { @@ -289,21 +221,21 @@ namespace AIFotoONLUS.Core } if (maxScore > _cfg.ConfidenceThreshold) - { - int x = (int)Math.Max(0, Math.Round(cx - w / 2)); - int y = (int)Math.Max(0, Math.Round(cy - h / 2)); - var rect = new Rect(x, y, (int)Math.Round(w), (int)Math.Round(h)); - boxes.Add(rect); - confidences.Add(maxScore); - classIds.Add(bestClass); - centerXList.Add(cx); - } + { + int x = (int)Math.Max(0, Math.Round(cx - w / 2)); + int y = (int)Math.Max(0, Math.Round(cy - h / 2)); + var rect = new Rect(x, y, (int)Math.Round(w), (int)Math.Round(h)); + boxes.Add(rect); + confidences.Add(maxScore); + classIds.Add(bestClass); + centerXList.Add(cx); + } } } if (boxes.Count == 0) return Enumerable.Empty(); - + CvDnn.NMSBoxes(boxes, confidences, (float)_cfg.ConfidenceThreshold, (float)_cfg.NmsThreshold, out int[] indices); @@ -554,8 +486,10 @@ namespace AIFotoONLUS.Core { var det = CvDnn.ReadNetFromDarknet(_cfg.DetectionCfg, _cfg.DetectionWeights); var rec = CvDnn.ReadNetFromDarknet(_cfg.RecognitionCfg, _cfg.RecognitionWeights); - ConfigureNetRuntime(det, _cfg.UseGpu); - ConfigureNetRuntime(rec, _cfg.UseGpu); + det.SetPreferableBackend(Backend.OPENCV); + det.SetPreferableTarget(Target.CPU); + rec.SetPreferableBackend(Backend.OPENCV); + rec.SetPreferableTarget(Target.CPU); netsBag.Add((det, rec)); return (det, rec); }); @@ -591,7 +525,8 @@ namespace AIFotoONLUS.Core try { using var tempRec = CvDnn.ReadNetFromDarknet(_cfg.RecognitionCfg, _cfg.RecognitionWeights); - ConfigureNetRuntime(tempRec, _cfg.UseGpu); + tempRec.SetPreferableBackend(Backend.OPENCV); + tempRec.SetPreferableTarget(Target.CPU); var alt = RecognizeDigits(crop, tempRec, ctx); if (!string.IsNullOrEmpty(alt)) txt = alt; }