From 4780e8c61ce896005c1efe971cdf49dd50664a9a Mon Sep 17 00:00:00 2001 From: Oleksandr Bezdieniezhnykh Date: Wed, 13 Aug 2025 10:12:25 +0300 Subject: [PATCH] fix detection label fix schema migrator for enums --- Azaion.Common/Controls/DetectionControl.cs | 5 +++-- .../Controls/DetectionLabelPanel.xaml | 6 +++--- .../Controls/DetectionLabelPanel.xaml.cs | 21 ++++++++++++++++--- Azaion.Common/DTO/Label.cs | 1 + Azaion.Common/Database/Annotation.cs | 11 +++++----- Azaion.Common/Database/SchemaMigrator.cs | 3 +++ Azaion.Common/Extensions/EnumExtensions.cs | 12 +++++++++++ 7 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 Azaion.Common/Extensions/EnumExtensions.cs diff --git a/Azaion.Common/Controls/DetectionControl.cs b/Azaion.Common/Controls/DetectionControl.cs index 39c8340..e3bf6a1 100644 --- a/Azaion.Common/Controls/DetectionControl.cs +++ b/Azaion.Common/Controls/DetectionControl.cs @@ -5,7 +5,7 @@ using System.Windows.Media; using System.Windows.Shapes; using Azaion.Common.DTO; using Azaion.Common.Extensions; -using Label = System.Windows.Controls.Label; +using Annotation = Azaion.Common.Database.Annotation; namespace Azaion.Common.Controls; @@ -94,7 +94,8 @@ public class DetectionControl : Border }; _detectionLabelPanel = new DetectionLabelPanel { - Confidence = canvasLabel.Confidence + Confidence = canvasLabel.Confidence, + DetectionClass = Annotation.DetectionClassesDict[canvasLabel.ClassNumber] }; DetectionLabelContainer.Children.Add(_detectionLabelPanel); diff --git a/Azaion.Common/Controls/DetectionLabelPanel.xaml b/Azaion.Common/Controls/DetectionLabelPanel.xaml index 629bac4..a05cd4a 100644 --- a/Azaion.Common/Controls/DetectionLabelPanel.xaml +++ b/Azaion.Common/Controls/DetectionLabelPanel.xaml @@ -47,13 +47,13 @@ - + - + - + \ No newline at end of file diff --git a/Azaion.Common/Controls/DetectionLabelPanel.xaml.cs b/Azaion.Common/Controls/DetectionLabelPanel.xaml.cs index cb596fb..0be8679 100644 --- a/Azaion.Common/Controls/DetectionLabelPanel.xaml.cs +++ b/Azaion.Common/Controls/DetectionLabelPanel.xaml.cs @@ -1,12 +1,12 @@ using System.Windows.Media; using Azaion.Common.DTO; +using Azaion.Common.Extensions; namespace Azaion.Common.Controls { public partial class DetectionLabelPanel { private AffiliationEnum _affiliation = AffiliationEnum.None; - private double _confidence; public AffiliationEnum Affiliation { @@ -18,18 +18,33 @@ namespace Azaion.Common.Controls } } - public DetectionClass DetectionClass { get; set; } + private DetectionClass _detectionClass = new(); + public DetectionClass DetectionClass { + get => _detectionClass; + set + { + _detectionClass = value; + SetClassName(); + } + } + private double _confidence; public double Confidence { get => _confidence; set { _confidence = value; - + SetClassName(); } } + private void SetClassName() + { + DetectionClassName.Content = _confidence >= 0.995 ? _detectionClass.UIName : $"{_detectionClass.UIName}: {_confidence * 100:F0}%"; + DetectionGrid.Background = new SolidColorBrush(_detectionClass.Color.ToConfidenceColor(_confidence)); + } + public DetectionLabelPanel() { InitializeComponent(); diff --git a/Azaion.Common/DTO/Label.cs b/Azaion.Common/DTO/Label.cs index 0a6130b..4e486c5 100644 --- a/Azaion.Common/DTO/Label.cs +++ b/Azaion.Common/DTO/Label.cs @@ -225,6 +225,7 @@ public class Detection : YoloLabel [JsonProperty(PropertyName = "an")][Key("an")] public string AnnotationName { get; set; } = null!; [JsonProperty(PropertyName = "p")][Key("p")] public double Confidence { get; set; } [JsonProperty(PropertyName = "dn")][Key("dn")] public string Description { get; set; } + [JsonProperty(PropertyName = "af")][Key("af")] public AffiliationEnum Affiliation { get; set; } //For db & serialization public Detection(){} diff --git a/Azaion.Common/Database/Annotation.cs b/Azaion.Common/Database/Annotation.cs index be7970e..9927d1c 100644 --- a/Azaion.Common/Database/Annotation.cs +++ b/Azaion.Common/Database/Annotation.cs @@ -1,7 +1,6 @@ using System.IO; using System.Windows.Media; using Azaion.Common.DTO; -using Azaion.Common.DTO.Config; using Azaion.Common.DTO.Queue; using MessagePack; @@ -13,14 +12,14 @@ public class Annotation private static string _labelsDir = null!; private static string _imagesDir = null!; private static string _thumbDir = null!; - private static Dictionary _detectionClassesDict; + public static Dictionary DetectionClassesDict = null!; public static void Init(DirectoriesConfig config, Dictionary detectionClassesDict) { _labelsDir = config.LabelsDirectory; _imagesDir = config.ImagesDirectory; _thumbDir = config.ThumbnailsDirectory; - _detectionClassesDict = detectionClassesDict; + DetectionClassesDict = detectionClassesDict; } [Key("n")] public string Name { get; set; } = null!; @@ -76,7 +75,7 @@ public class Annotation private List<(Color Color, double Confidence)>? _colors; [IgnoreMember] public List<(Color Color, double Confidence)> Colors => _colors ??= Detections - .Select(d => (_detectionClassesDict[d.ClassNumber].Color, d.Confidence)) + .Select(d => (DetectionClassesDict[d.ClassNumber].Color, d.Confidence)) .ToList(); private string _className; @@ -88,8 +87,8 @@ public class Annotation { var detectionClasses = Detections.Select(x => x.ClassNumber).Distinct().ToList(); _className = detectionClasses.Count > 1 - ? string.Join(", ", detectionClasses.Select(x => _detectionClassesDict[x].UIName)) - : _detectionClassesDict[detectionClasses.FirstOrDefault()].UIName; + ? string.Join(", ", detectionClasses.Select(x => DetectionClassesDict[x].UIName)) + : DetectionClassesDict[detectionClasses.FirstOrDefault()].UIName; } return _className; } diff --git a/Azaion.Common/Database/SchemaMigrator.cs b/Azaion.Common/Database/SchemaMigrator.cs index 1b34996..eb043fb 100644 --- a/Azaion.Common/Database/SchemaMigrator.cs +++ b/Azaion.Common/Database/SchemaMigrator.cs @@ -85,6 +85,9 @@ public static class SchemaMigrator if (underlyingType == typeof(bool)) return $"NOT NULL DEFAULT {(Convert.ToBoolean(defaultValue) ? 1 : 0)}"; + + if (underlyingType.IsEnum) + return $"NOT NULL DEFAULT {(int)defaultValue}"; if (underlyingType.IsValueType && defaultValue is IFormattable f) return $"NOT NULL DEFAULT {f.ToString(null, System.Globalization.CultureInfo.InvariantCulture)}"; diff --git a/Azaion.Common/Extensions/EnumExtensions.cs b/Azaion.Common/Extensions/EnumExtensions.cs new file mode 100644 index 0000000..9b8b32f --- /dev/null +++ b/Azaion.Common/Extensions/EnumExtensions.cs @@ -0,0 +1,12 @@ +namespace Azaion.Common.Extensions; + +public static class EnumExtensions +{ + public static T GetValueOrDefault(this string value, T defaultValue) where T : struct + { + if (string.IsNullOrEmpty(value)) + return defaultValue; + + return Enum.TryParse(value, true, out T result) ? result : defaultValue; + } +} \ No newline at end of file