mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 12:36:31 +00:00
queue + local sqlite WIP
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using Azaion.Annotator.DTO;
|
||||
using Azaion.Common;
|
||||
using Azaion.Common.DTO;
|
||||
using Azaion.Common.DTO.Config;
|
||||
using Azaion.Common.DTO.Queue;
|
||||
using Azaion.Common.Services;
|
||||
using LibVLCSharp.Shared;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -19,16 +20,16 @@ public class AnnotatorEventHandler(
|
||||
MediaPlayer mediaPlayer,
|
||||
Annotator mainWindow,
|
||||
FormState formState,
|
||||
IOptions<DirectoriesConfig> directoriesConfig,
|
||||
AnnotationService annotationService,
|
||||
IMediator mediator,
|
||||
ILogger<AnnotatorEventHandler> logger)
|
||||
ILogger<AnnotatorEventHandler> logger,
|
||||
IOptions<DirectoriesConfig> dirConfig)
|
||||
:
|
||||
INotificationHandler<KeyEvent>,
|
||||
INotificationHandler<AnnClassSelectedEvent>,
|
||||
INotificationHandler<PlaybackControlEvent>,
|
||||
INotificationHandler<VolumeChangedEvent>
|
||||
{
|
||||
private readonly DirectoriesConfig _directoriesConfig = directoriesConfig.Value;
|
||||
private const int STEP = 20;
|
||||
private const int LARGE_STEP = 5000;
|
||||
private const int RESULT_WIDTH = 1280;
|
||||
@@ -59,7 +60,7 @@ public class AnnotatorEventHandler(
|
||||
mainWindow.LvClasses.SelectedIndex = annClass.Id;
|
||||
}
|
||||
|
||||
public async Task Handle(KeyEvent keyEvent, CancellationToken cancellationToken)
|
||||
public async Task Handle(KeyEvent keyEvent, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (keyEvent.WindowEnum != WindowEnum.Annotator)
|
||||
return;
|
||||
@@ -75,23 +76,19 @@ public class AnnotatorEventHandler(
|
||||
SelectClass((AnnotationClass)mainWindow.LvClasses.Items[keyNumber.Value]!);
|
||||
|
||||
if (_keysControlEnumDict.TryGetValue(key, out var value))
|
||||
await ControlPlayback(value);
|
||||
await ControlPlayback(value, cancellationToken);
|
||||
|
||||
if (key == Key.A)
|
||||
mainWindow.AutoDetect(null!, null!);
|
||||
|
||||
await VolumeControl(key);
|
||||
}
|
||||
|
||||
private async Task VolumeControl(Key key)
|
||||
{
|
||||
#region Volume
|
||||
switch (key)
|
||||
{
|
||||
case Key.VolumeMute when mediaPlayer.Volume == 0:
|
||||
await ControlPlayback(PlaybackControlEnum.TurnOnVolume);
|
||||
await ControlPlayback(PlaybackControlEnum.TurnOnVolume, cancellationToken);
|
||||
break;
|
||||
case Key.VolumeMute:
|
||||
await ControlPlayback(PlaybackControlEnum.TurnOffVolume);
|
||||
await ControlPlayback(PlaybackControlEnum.TurnOffVolume, cancellationToken);
|
||||
break;
|
||||
case Key.Up:
|
||||
case Key.VolumeUp:
|
||||
@@ -106,15 +103,16 @@ public class AnnotatorEventHandler(
|
||||
mainWindow.Volume.Value = vDown;
|
||||
break;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
public async Task Handle(PlaybackControlEvent notification, CancellationToken cancellationToken)
|
||||
public async Task Handle(PlaybackControlEvent notification, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await ControlPlayback(notification.PlaybackControl);
|
||||
await ControlPlayback(notification.PlaybackControl, cancellationToken);
|
||||
mainWindow.VideoView.Focus();
|
||||
}
|
||||
|
||||
private async Task ControlPlayback(PlaybackControlEnum controlEnum)
|
||||
private async Task ControlPlayback(PlaybackControlEnum controlEnum, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -222,10 +220,9 @@ public class AnnotatorEventHandler(
|
||||
mediaPlayer.Play(new Media(libVLC, mediaInfo.Path));
|
||||
}
|
||||
|
||||
private async Task SaveAnnotations()
|
||||
//SAVE: MANUAL
|
||||
private async Task SaveAnnotations(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var annGridSelectedIndex = mainWindow.DgAnnotations.SelectedIndex;
|
||||
|
||||
if (formState.CurrentMedia == null)
|
||||
return;
|
||||
|
||||
@@ -236,16 +233,15 @@ public class AnnotatorEventHandler(
|
||||
.Select(x => new YoloLabel(x.Info, mainWindow.Editor.RenderSize, formState.BackgroundTime.HasValue ? mainWindow.Editor.RenderSize : formState.CurrentVideoSize))
|
||||
.ToList();
|
||||
|
||||
await YoloLabel.WriteToFile(currentAnns, Path.Combine(_directoriesConfig.LabelsDirectory, $"{fName}.txt"));
|
||||
await mainWindow.AddAnnotations(time, currentAnns);
|
||||
await mainWindow.AddAnnotations(time, currentAnns, cancellationToken);
|
||||
|
||||
formState.CurrentMedia.HasAnnotations = mainWindow.Annotations.Count != 0;
|
||||
mainWindow.LvFiles.Items.Refresh();
|
||||
mainWindow.Editor.RemoveAllAnns();
|
||||
|
||||
var isVideo = formState.CurrentMedia.MediaType == MediaTypes.Video;
|
||||
var destinationPath = Path.Combine(_directoriesConfig.ImagesDirectory, $"{fName}{(isVideo ? ".jpg" : Path.GetExtension(formState.CurrentMedia.Path))}");
|
||||
var imgPath = Path.Combine(dirConfig.Value.ImagesDirectory, $"{fName}{(isVideo ? ".jpg" : Path.GetExtension(formState.CurrentMedia.Path))}");
|
||||
|
||||
mainWindow.Editor.RemoveAllAnns();
|
||||
if (isVideo)
|
||||
{
|
||||
if (formState.BackgroundTime.HasValue)
|
||||
@@ -256,22 +252,23 @@ public class AnnotatorEventHandler(
|
||||
|
||||
//next item
|
||||
var annGrid = mainWindow.DgAnnotations;
|
||||
annGrid.SelectedIndex = Math.Min(annGrid.Items.Count, annGridSelectedIndex + 1);
|
||||
annGrid.SelectedIndex = Math.Min(annGrid.Items.Count, annGrid.SelectedIndex + 1);
|
||||
mainWindow.OpenAnnotationResult((AnnotationResult)annGrid.SelectedItem);
|
||||
}
|
||||
else
|
||||
{
|
||||
var resultHeight = (uint)Math.Round(RESULT_WIDTH / formState.CurrentVideoSize.Width * formState.CurrentVideoSize.Height);
|
||||
mediaPlayer.TakeSnapshot(0, destinationPath, RESULT_WIDTH, resultHeight);
|
||||
mediaPlayer.TakeSnapshot(0, imgPath, RESULT_WIDTH, resultHeight);
|
||||
mediaPlayer.Play();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
File.Copy(formState.CurrentMedia.Path, destinationPath, overwrite: true);
|
||||
File.Copy(formState.CurrentMedia.Path, imgPath, overwrite: true);
|
||||
NextMedia();
|
||||
}
|
||||
await annotationService.SaveAnnotation(fName, currentAnns, SourceEnum.Manual, token: cancellationToken);
|
||||
|
||||
await mediator.Publish(new ImageCreatedEvent(destinationPath));
|
||||
await mediator.Publish(new ImageCreatedEvent(imgPath), cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user