fix detection label

fix schema migrator for enums
This commit is contained in:
Oleksandr Bezdieniezhnykh
2025-08-13 10:12:25 +03:00
parent 16e5853d67
commit 4780e8c61c
7 changed files with 45 additions and 14 deletions
+3 -2
View File
@@ -5,7 +5,7 @@ using System.Windows.Media;
using System.Windows.Shapes; using System.Windows.Shapes;
using Azaion.Common.DTO; using Azaion.Common.DTO;
using Azaion.Common.Extensions; using Azaion.Common.Extensions;
using Label = System.Windows.Controls.Label; using Annotation = Azaion.Common.Database.Annotation;
namespace Azaion.Common.Controls; namespace Azaion.Common.Controls;
@@ -94,7 +94,8 @@ public class DetectionControl : Border
}; };
_detectionLabelPanel = new DetectionLabelPanel _detectionLabelPanel = new DetectionLabelPanel
{ {
Confidence = canvasLabel.Confidence Confidence = canvasLabel.Confidence,
DetectionClass = Annotation.DetectionClassesDict[canvasLabel.ClassNumber]
}; };
DetectionLabelContainer.Children.Add(_detectionLabelPanel); DetectionLabelContainer.Children.Add(_detectionLabelPanel);
@@ -47,13 +47,13 @@
</DrawingImage> </DrawingImage>
</UserControl.Resources> </UserControl.Resources>
<Grid> <Grid x:Name="DetectionGrid">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="28"></ColumnDefinition> <ColumnDefinition Width="2"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition> <ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Image Grid.Column="0" x:Name="AffiliationImage"> <Image Grid.Column="0" x:Name="AffiliationImage">
</Image> </Image>
<Label Grid.Column="1" FontSize="16"></Label> <Label Grid.Column="1" x:Name="DetectionClassName" FontSize="16"></Label>
</Grid> </Grid>
</UserControl> </UserControl>
@@ -1,12 +1,12 @@
using System.Windows.Media; using System.Windows.Media;
using Azaion.Common.DTO; using Azaion.Common.DTO;
using Azaion.Common.Extensions;
namespace Azaion.Common.Controls namespace Azaion.Common.Controls
{ {
public partial class DetectionLabelPanel public partial class DetectionLabelPanel
{ {
private AffiliationEnum _affiliation = AffiliationEnum.None; private AffiliationEnum _affiliation = AffiliationEnum.None;
private double _confidence;
public AffiliationEnum Affiliation 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 public double Confidence
{ {
get => _confidence; get => _confidence;
set set
{ {
_confidence = value; _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() public DetectionLabelPanel()
{ {
InitializeComponent(); InitializeComponent();
+1
View File
@@ -225,6 +225,7 @@ public class Detection : YoloLabel
[JsonProperty(PropertyName = "an")][Key("an")] public string AnnotationName { get; set; } = null!; [JsonProperty(PropertyName = "an")][Key("an")] public string AnnotationName { get; set; } = null!;
[JsonProperty(PropertyName = "p")][Key("p")] public double Confidence { get; set; } [JsonProperty(PropertyName = "p")][Key("p")] public double Confidence { get; set; }
[JsonProperty(PropertyName = "dn")][Key("dn")] public string Description { 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 //For db & serialization
public Detection(){} public Detection(){}
+5 -6
View File
@@ -1,7 +1,6 @@
using System.IO; using System.IO;
using System.Windows.Media; using System.Windows.Media;
using Azaion.Common.DTO; using Azaion.Common.DTO;
using Azaion.Common.DTO.Config;
using Azaion.Common.DTO.Queue; using Azaion.Common.DTO.Queue;
using MessagePack; using MessagePack;
@@ -13,14 +12,14 @@ public class Annotation
private static string _labelsDir = null!; private static string _labelsDir = null!;
private static string _imagesDir = null!; private static string _imagesDir = null!;
private static string _thumbDir = null!; private static string _thumbDir = null!;
private static Dictionary<int, DetectionClass> _detectionClassesDict; public static Dictionary<int, DetectionClass> DetectionClassesDict = null!;
public static void Init(DirectoriesConfig config, Dictionary<int, DetectionClass> detectionClassesDict) public static void Init(DirectoriesConfig config, Dictionary<int, DetectionClass> detectionClassesDict)
{ {
_labelsDir = config.LabelsDirectory; _labelsDir = config.LabelsDirectory;
_imagesDir = config.ImagesDirectory; _imagesDir = config.ImagesDirectory;
_thumbDir = config.ThumbnailsDirectory; _thumbDir = config.ThumbnailsDirectory;
_detectionClassesDict = detectionClassesDict; DetectionClassesDict = detectionClassesDict;
} }
[Key("n")] public string Name { get; set; } = null!; [Key("n")] public string Name { get; set; } = null!;
@@ -76,7 +75,7 @@ public class Annotation
private List<(Color Color, double Confidence)>? _colors; private List<(Color Color, double Confidence)>? _colors;
[IgnoreMember] public List<(Color Color, double Confidence)> Colors => _colors ??= Detections [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(); .ToList();
private string _className; private string _className;
@@ -88,8 +87,8 @@ public class Annotation
{ {
var detectionClasses = Detections.Select(x => x.ClassNumber).Distinct().ToList(); var detectionClasses = Detections.Select(x => x.ClassNumber).Distinct().ToList();
_className = detectionClasses.Count > 1 _className = detectionClasses.Count > 1
? string.Join(", ", detectionClasses.Select(x => _detectionClassesDict[x].UIName)) ? string.Join(", ", detectionClasses.Select(x => DetectionClassesDict[x].UIName))
: _detectionClassesDict[detectionClasses.FirstOrDefault()].UIName; : DetectionClassesDict[detectionClasses.FirstOrDefault()].UIName;
} }
return _className; return _className;
} }
+3
View File
@@ -86,6 +86,9 @@ public static class SchemaMigrator
if (underlyingType == typeof(bool)) if (underlyingType == typeof(bool))
return $"NOT NULL DEFAULT {(Convert.ToBoolean(defaultValue) ? 1 : 0)}"; 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) if (underlyingType.IsValueType && defaultValue is IFormattable f)
return $"NOT NULL DEFAULT {f.ToString(null, System.Globalization.CultureInfo.InvariantCulture)}"; return $"NOT NULL DEFAULT {f.ToString(null, System.Globalization.CultureInfo.InvariantCulture)}";
@@ -0,0 +1,12 @@
namespace Azaion.Common.Extensions;
public static class EnumExtensions
{
public static T GetValueOrDefault<T>(this string value, T defaultValue) where T : struct
{
if (string.IsNullOrEmpty(value))
return defaultValue;
return Enum.TryParse(value, true, out T result) ? result : defaultValue;
}
}