Files
annotations/Azaion.Dataset/DatasetExplorerEventHandler.cs
T
Alex Bezdieniezhnykh 83ae6a0ae9 move detection classes and other system values from local config to remote
forbid non validators to read from queue
create better visualization in detector control
make colors for detection classes more distinguishable
fix bug with removing detection (probably completely)
2025-04-02 19:53:03 +03:00

143 lines
6.0 KiB
C#

using System.IO;
using System.Windows;
using System.Windows.Input;
using Azaion.Common.DTO;
using Azaion.Common.DTO.Queue;
using Azaion.Common.Events;
using Azaion.Common.Services;
using MediatR;
namespace Azaion.Dataset;
public class DatasetExplorerEventHandler(
DatasetExplorer datasetExplorer,
AnnotationService annotationService) :
INotificationHandler<KeyEvent>,
INotificationHandler<DatasetExplorerControlEvent>,
INotificationHandler<AnnotationCreatedEvent>,
INotificationHandler<AnnotationsDeletedEvent>
{
private readonly Dictionary<Key, PlaybackControlEnum> _keysControlEnumDict = new()
{
{ Key.Enter, PlaybackControlEnum.SaveAnnotations },
{ Key.Delete, PlaybackControlEnum.RemoveSelectedAnns },
{ Key.X, PlaybackControlEnum.RemoveAllAnns },
{ Key.Escape, PlaybackControlEnum.Close },
{ Key.Down, PlaybackControlEnum.Next },
{ Key.Up, PlaybackControlEnum.Previous },
{ Key.V, PlaybackControlEnum.ValidateAnnotations},
};
public async Task Handle(DatasetExplorerControlEvent notification, CancellationToken cancellationToken)
{
await HandleControl(notification.PlaybackControl, cancellationToken);
}
public async Task Handle(KeyEvent keyEvent, CancellationToken cancellationToken)
{
if (keyEvent.WindowEnum != WindowEnum.DatasetExplorer)
return;
var key = keyEvent.Args.Key;
var keyNumber = (int?)null;
if ((int)key >= (int)Key.D1 && (int)key <= (int)Key.D9) keyNumber = key - Key.D1;
if ((int)key >= (int)Key.NumPad1 && (int)key <= (int)Key.NumPad9) keyNumber = key - Key.NumPad1;
if (keyNumber.HasValue)
datasetExplorer.LvClasses.SelectNum(keyNumber.Value);
else
{
if (datasetExplorer.Switcher.SelectedIndex == 1 && _keysControlEnumDict.TryGetValue(key, out var value))
await HandleControl(value, cancellationToken);
}
}
private async Task HandleControl(PlaybackControlEnum controlEnum, CancellationToken cancellationToken = default)
{
switch (controlEnum)
{
case PlaybackControlEnum.SaveAnnotations:
if (datasetExplorer.ThumbnailLoading)
return;
var a = datasetExplorer.CurrentAnnotation!.Annotation;
var detections = datasetExplorer.ExplorerEditor.CurrentDetections
.Select(x => new Detection(a.Name, x.GetLabel(datasetExplorer.ExplorerEditor.RenderSize)))
.ToList();
var index = datasetExplorer.ThumbnailsView.SelectedIndex;
await annotationService.SaveAnnotation(a.OriginalMediaName, a.Time, detections, token: cancellationToken);
await datasetExplorer.EditAnnotation(index + 1);
break;
case PlaybackControlEnum.RemoveSelectedAnns:
if (datasetExplorer.ExplorerEditor.CurrentDetections.Any(x => x.IsSelected))
datasetExplorer.ExplorerEditor.RemoveSelectedAnns();
else
{
await datasetExplorer.DeleteAnnotations();
await datasetExplorer.EditAnnotation(datasetExplorer.ThumbnailsView.SelectedIndex);
}
break;
case PlaybackControlEnum.RemoveAllAnns:
datasetExplorer.ExplorerEditor.RemoveAllAnns();
break;
case PlaybackControlEnum.Next:
await datasetExplorer.EditAnnotation(datasetExplorer.ThumbnailsView.SelectedIndex + 1);
break;
case PlaybackControlEnum.Previous:
await datasetExplorer.EditAnnotation(datasetExplorer.ThumbnailsView.SelectedIndex - 1);
break;
case PlaybackControlEnum.Close:
datasetExplorer.SwitchTab(toEditor: false);
break;
case PlaybackControlEnum.ValidateAnnotations:
var annotations = datasetExplorer.ThumbnailsView.SelectedItems.Cast<AnnotationThumbnail>()
.Select(x => x.Annotation)
.ToList();
foreach (var annotation in annotations)
await annotationService.ValidateAnnotation(annotation, cancellationToken);
break;
}
}
public async Task Handle(AnnotationCreatedEvent notification, CancellationToken cancellationToken)
{
var annotation = notification.Annotation;
var selectedClass = datasetExplorer.LvClasses.CurrentClassNumber;
//TODO: For editing existing need to handle updates
datasetExplorer.AddAnnotationToDict(annotation);
if (annotation.Classes.Contains(selectedClass) || selectedClass == -1)
{
var annThumb = new AnnotationThumbnail(annotation);
if (datasetExplorer.SelectedAnnotationDict.ContainsKey(annThumb.Annotation.Name))
{
datasetExplorer.SelectedAnnotationDict.Remove(annThumb.Annotation.Name);
var ann = datasetExplorer.SelectedAnnotations.FirstOrDefault(x => x.Annotation.Name == annThumb.Annotation.Name);
if (ann != null)
datasetExplorer.SelectedAnnotations.Remove(ann);
}
datasetExplorer.SelectedAnnotations.Insert(0, annThumb);
datasetExplorer.SelectedAnnotationDict.Add(annThumb.Annotation.Name, annThumb);
}
await Task.CompletedTask;
}
public async Task Handle(AnnotationsDeletedEvent notification, CancellationToken cancellationToken)
{
var names = notification.Annotations.Select(x => x.Name).ToList();
var annThumbs = datasetExplorer.SelectedAnnotationDict
.Where(x => names.Contains(x.Key))
.Select(x => x.Value)
.ToList();
foreach (var annThumb in annThumbs)
{
datasetExplorer.SelectedAnnotations.Remove(annThumb);
datasetExplorer.SelectedAnnotationDict.Remove(annThumb.Annotation.Name);
}
await Task.CompletedTask;
}
}