use nms in the model itself, simplify and make postprocess faster.

make inference in batches, fix c# handling, add overlap handling
This commit is contained in:
Alex Bezdieniezhnykh
2025-02-10 14:55:00 +02:00
parent ba3e3b4a55
commit c1b5b5fee2
19 changed files with 259 additions and 140 deletions
+30 -21
View File
@@ -7,6 +7,7 @@ using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using Azaion.Annotator.DTO;
using Azaion.Common;
using Azaion.Common.Database;
using Azaion.Common.DTO;
using Azaion.Common.DTO.Config;
@@ -39,11 +40,12 @@ public partial class Annotator
private readonly AnnotationService _annotationService;
private readonly IDbFactory _dbFactory;
private readonly IInferenceService _inferenceService;
private readonly CancellationTokenSource _ctSource = new();
private ObservableCollection<DetectionClass> AnnotationClasses { get; set; } = new();
private bool _suspendLayout;
public readonly CancellationTokenSource MainCancellationSource = new();
public CancellationTokenSource DetectionCancellationSource = new();
public bool FollowAI = false;
public bool IsInferenceNow = false;
@@ -310,7 +312,7 @@ public partial class Annotator
var annotations = await _dbFactory.Run(async db =>
await db.Annotations.LoadWith(x => x.Detections)
.Where(x => x.OriginalMediaName == _formState.VideoName)
.ToListAsync(token: _ctSource.Token));
.ToListAsync(token: MainCancellationSource.Token));
TimedAnnotations.Clear();
_formState.AnnotationResults.Clear();
@@ -395,6 +397,8 @@ public partial class Annotator
private void OnFormClosed(object? sender, EventArgs e)
{
MainCancellationSource.Cancel();
DetectionCancellationSource.Cancel();
_mediaPlayer.Stop();
_mediaPlayer.Dispose();
_libVLC.Dispose();
@@ -490,6 +494,20 @@ public partial class Annotator
private (TimeSpan Time, List<Detection> Detections)? _previousDetection;
private List<string> GetLvFiles()
{
return Dispatcher.Invoke(() =>
{
var source = LvFiles.ItemsSource as IEnumerable<MediaFileInfo>;
var items = source?.Skip(LvFiles.SelectedIndex)
.Take(Constants.DETECTION_BATCH_SIZE)
.Select(x => x.Path)
.ToList();
return items ?? new List<string>();
});
}
public void AutoDetect(object sender, RoutedEventArgs e)
{
if (IsInferenceNow)
@@ -503,36 +521,25 @@ public partial class Annotator
if (LvFiles.SelectedIndex == -1)
LvFiles.SelectedIndex = 0;
var mct = new CancellationTokenSource();
var token = mct.Token;
Dispatcher.Invoke(() => Editor.ResetBackground());
IsInferenceNow = true;
FollowAI = true;
DetectionCancellationSource = new CancellationTokenSource();
var ct = DetectionCancellationSource.Token;
_ = Task.Run(async () =>
{
var mediaInfo = Dispatcher.Invoke(() => (MediaFileInfo)LvFiles.SelectedItem);
while (mediaInfo != null && !token.IsCancellationRequested)
var files = GetLvFiles();
while (files.Any() && !ct.IsCancellationRequested)
{
await Dispatcher.Invoke(async () =>
{
await _mediator.Publish(new AnnotatorControlEvent(PlaybackControlEnum.Play), token);
await _mediator.Publish(new AnnotatorControlEvent(PlaybackControlEnum.Play), ct);
await ReloadAnnotations();
});
await _inferenceService.RunInference(mediaInfo.Path, async annotationImage =>
{
annotationImage.OriginalMediaName = mediaInfo.FName;
await ProcessDetection(annotationImage);
});
mediaInfo = Dispatcher.Invoke(() =>
{
if (LvFiles.SelectedIndex == LvFiles.Items.Count - 1)
return null;
LvFiles.SelectedIndex += 1;
return (MediaFileInfo)LvFiles.SelectedItem;
});
await _inferenceService.RunInference(files, async annotationImage => await ProcessDetection(annotationImage), ct);
files = GetLvFiles();
Dispatcher.Invoke(() => LvFiles.Items.Refresh());
}
Dispatcher.Invoke(() =>
@@ -541,7 +548,7 @@ public partial class Annotator
IsInferenceNow = false;
FollowAI = false;
});
}, token);
});
}
private async Task ProcessDetection(AnnotationImage annotationImage)
@@ -551,6 +558,8 @@ public partial class Annotator
try
{
var annotation = await _annotationService.SaveAnnotation(annotationImage);
if (annotation.OriginalMediaName != _formState.CurrentMedia.FName)
return;
AddAnnotation(annotation);
if (FollowAI)