mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 20:36:30 +00:00
big refactoring. get rid of static properties and coupled architecture. prepare system for integration tests
This commit is contained in:
@@ -9,19 +9,6 @@ namespace Azaion.Common.Database;
|
||||
[MessagePackObject]
|
||||
public class Annotation
|
||||
{
|
||||
private static string _labelsDir = null!;
|
||||
private static string _imagesDir = null!;
|
||||
private static string _thumbDir = null!;
|
||||
public static Dictionary<int, DetectionClass> DetectionClassesDict = null!;
|
||||
|
||||
public static void Init(DirectoriesConfig config, Dictionary<int, DetectionClass> detectionClassesDict)
|
||||
{
|
||||
_labelsDir = config.LabelsDirectory;
|
||||
_imagesDir = config.ImagesDirectory;
|
||||
_thumbDir = config.ThumbnailsDirectory;
|
||||
DetectionClassesDict = detectionClassesDict;
|
||||
}
|
||||
|
||||
[Key("n")] public string Name { get; set; } = null!;
|
||||
[Key("hash")] public string MediaHash { get; set; } = null!;
|
||||
[Key("mn")] public string OriginalMediaName { get; set; } = null!;
|
||||
@@ -44,9 +31,6 @@ public class Annotation
|
||||
|
||||
#region Calculated
|
||||
[IgnoreMember] public List<int> Classes => Detections.Select(x => x.ClassNumber).ToList();
|
||||
[IgnoreMember] public string ImagePath => Path.Combine(_imagesDir, $"{Name}{ImageExtension}");
|
||||
[IgnoreMember] public string LabelPath => Path.Combine(_labelsDir, $"{Name}.txt");
|
||||
[IgnoreMember] public string ThumbPath => Path.Combine(_thumbDir, $"{Name}{Constants.THUMBNAIL_PREFIX}.jpg");
|
||||
[IgnoreMember] public bool IsSplit => Name.Contains(Constants.SPLIT_SUFFIX);
|
||||
|
||||
private CanvasLabel? _splitTile;
|
||||
@@ -73,30 +57,7 @@ public class Annotation
|
||||
}
|
||||
|
||||
[IgnoreMember] public string TimeStr => $"{Time:h\\:mm\\:ss}";
|
||||
|
||||
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))
|
||||
.ToList();
|
||||
|
||||
private string? _className;
|
||||
[IgnoreMember] public string ClassName
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrEmpty(_className))
|
||||
{
|
||||
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;
|
||||
}
|
||||
return _className;
|
||||
}
|
||||
}
|
||||
#endregion Calculated
|
||||
|
||||
public override string ToString() => $"Annotation: {Name}{TimeStr}: {ClassName}";
|
||||
}
|
||||
|
||||
[MessagePackObject]
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
using LinqToDB;
|
||||
|
||||
namespace Azaion.Common.Database;
|
||||
|
||||
public class AnnotationRepository : IAnnotationRepository
|
||||
{
|
||||
private readonly IDbFactory _dbFactory;
|
||||
|
||||
public AnnotationRepository(IDbFactory dbFactory)
|
||||
{
|
||||
_dbFactory = dbFactory;
|
||||
}
|
||||
|
||||
public async Task<List<Annotation>> GetByMediaHashAsync(string hash, CancellationToken ct = default)
|
||||
{
|
||||
return await _dbFactory.Run(async db =>
|
||||
await db.GetTable<Annotation>()
|
||||
.Where(x => x.MediaHash == hash)
|
||||
.ToListAsync(ct));
|
||||
}
|
||||
|
||||
public async Task<Annotation?> GetByNameAsync(string name, CancellationToken ct = default)
|
||||
{
|
||||
return await _dbFactory.Run(async db =>
|
||||
await db.GetTable<Annotation>()
|
||||
.FirstOrDefaultAsync(x => x.Name == name, ct));
|
||||
}
|
||||
|
||||
public async Task<List<Annotation>> GetAllAsync(CancellationToken ct = default)
|
||||
{
|
||||
return await _dbFactory.Run(async db =>
|
||||
await db.GetTable<Annotation>().ToListAsync(ct));
|
||||
}
|
||||
|
||||
public async Task SaveAsync(Annotation annotation, CancellationToken ct = default)
|
||||
{
|
||||
await _dbFactory.RunWrite(async db =>
|
||||
{
|
||||
await db.InsertOrReplaceAsync(annotation, token: ct);
|
||||
});
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(List<string> names, CancellationToken ct = default)
|
||||
{
|
||||
await _dbFactory.DeleteAnnotations(names, ct);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,11 +24,6 @@ public static class AnnotationsDbSchemaHolder
|
||||
annotationBuilder
|
||||
.Ignore(x => x.Milliseconds)
|
||||
.Ignore(x => x.Classes)
|
||||
.Ignore(x => x.ImagePath)
|
||||
.Ignore(x => x.LabelPath)
|
||||
.Ignore(x => x.ThumbPath)
|
||||
.Ignore(x => x.ClassName)
|
||||
.Ignore(x => x.Colors)
|
||||
.Ignore(x => x.SplitTile)
|
||||
.Ignore(x => x.IsSplit)
|
||||
.Ignore(x => x.TimeStr);
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace Azaion.Common.Database;
|
||||
|
||||
public interface IAnnotationRepository
|
||||
{
|
||||
Task<List<Annotation>> GetByMediaHashAsync(string hash, CancellationToken ct = default);
|
||||
Task<Annotation?> GetByNameAsync(string name, CancellationToken ct = default);
|
||||
Task<List<Annotation>> GetAllAsync(CancellationToken ct = default);
|
||||
Task SaveAsync(Annotation annotation, CancellationToken ct = default);
|
||||
Task DeleteAsync(List<string> names, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user