Add GPU validation and configuration support in NumberRecognitionEngine
This commit is contained in:
parent
f4f8a58646
commit
44af20ead5
1 changed files with 57 additions and 0 deletions
|
|
@ -106,6 +106,11 @@ namespace AIFotoONLUS.Core
|
||||||
{
|
{
|
||||||
// Ignore if not supported by OpenCvSharp build
|
// Ignore if not supported by OpenCvSharp build
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_cfg.UseGpu)
|
||||||
|
{
|
||||||
|
ValidateGpuRuntime();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|
@ -117,6 +122,38 @@ namespace AIFotoONLUS.Core
|
||||||
GC.SuppressFinalize(this);
|
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)
|
private static string SanitizeFileName(string name)
|
||||||
{
|
{
|
||||||
foreach (var c in Path.GetInvalidFileNameChars()) name = name.Replace(c, '_');
|
foreach (var c in Path.GetInvalidFileNameChars()) name = name.Replace(c, '_');
|
||||||
|
|
@ -138,6 +175,26 @@ namespace AIFotoONLUS.Core
|
||||||
net.SetPreferableTarget(Target.CPU);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Detect text regions in the supplied image using the detection network.
|
/// Detect text regions in the supplied image using the detection network.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue