add nth frame to ai recognition to config

This commit is contained in:
Alex Bezdieniezhnykh
2024-12-03 11:36:20 +02:00
parent 0b38d9b24c
commit 3944df8efe
12 changed files with 81 additions and 29 deletions
+59 -13
View File
@@ -7,6 +7,7 @@ using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Azaion.Annotator.DTO;
using Azaion.Annotator.Extensions;
@@ -187,10 +188,25 @@ public partial class Annotator
OpenAnnotationResult((AnnotationResult)DgAnnotations.SelectedItem);
break;
case Key.Delete:
var result = MessageBox.Show("Чи дійсно видалити аннотації?","Підтвердження видалення", MessageBoxButton.OKCancel, MessageBoxImage.Question);
var result = MessageBox.Show(Application.Current.MainWindow, "Чи дійсно видалити аннотації?","Підтвердження видалення", MessageBoxButton.OKCancel, MessageBoxImage.Question);
if (result != MessageBoxResult.OK)
return;
// var allWindows = Application.Current.Windows.Cast<Window>();
// try
// {
// foreach (var window in allWindows)
// window.IsEnabled = false;
//
// }
// finally
// {
// foreach (var window in allWindows)
// {
// window.IsEnabled = true;
// }
// }
var res = DgAnnotations.SelectedItems.Cast<AnnotationResult>().ToList();
foreach (var annotationResult in res)
{
@@ -506,12 +522,14 @@ public partial class Annotator
private (TimeSpan Time, List<Detection> Detections)? _previousDetection;
public void AutoDetect(object sender, RoutedEventArgs e)
public async void AutoDetect(object sender, RoutedEventArgs e)
{
if (LvFiles.SelectedItem == null)
if (LvFiles.Items.IsEmpty)
return;
if (LvFiles.SelectedIndex == -1)
LvFiles.SelectedIndex = 0;
_mediator.Publish(new PlaybackControlEvent(PlaybackControlEnum.Play));
await _mediator.Publish(new PlaybackControlEvent(PlaybackControlEnum.Play));
_mediaPlayer.SetPause(true);
var manualCancellationSource = new CancellationTokenSource();
@@ -528,6 +546,7 @@ public partial class Annotator
_mediaPlayer.SeekTo(TimeSpan.Zero);
Editor.RemoveAllAnns();
};
_autoDetectDialog.Top = Height - _autoDetectDialog.Height - 80;
_autoDetectDialog.Left = 5;
@@ -535,33 +554,38 @@ public partial class Annotator
_ = Task.Run(async () =>
{
MediaFileInfo mediaInfo = null!;
Dispatcher.Invoke(() =>
{
mediaInfo = (MediaFileInfo)LvFiles.SelectedItem;
});
var mediaInfo = Dispatcher.Invoke(() => (MediaFileInfo)LvFiles.SelectedItem);
while (mediaInfo != null)
{
_formState.CurrentMedia = mediaInfo;
await Dispatcher.Invoke(async () => await ReloadAnnotations(token));
if (mediaInfo.MediaType == MediaTypes.Image)
{
await DetectImage(mediaInfo, manualCancellationSource, token);
await Task.Delay(70, token);
}
else
await DetectVideo(mediaInfo, manualCancellationSource, token);
mediaInfo = Dispatcher.Invoke(() =>
{
if (LvFiles.SelectedIndex == LvFiles.Items.Count - 1)
return null;
LvFiles.SelectedIndex += 1;
return (MediaFileInfo)LvFiles.SelectedItem;
});
}
Dispatcher.Invoke(() => _autoDetectDialog.Close());
Dispatcher.Invoke(() =>
{
_autoDetectDialog.Close();
_mediaPlayer.Stop();
LvFiles.Items.Refresh();
});
}, token);
_autoDetectDialog.ShowDialog();
Dispatcher.Invoke(() => Editor.Background = new SolidColorBrush(Color.FromArgb(1, 0, 0, 0)));
Dispatcher.Invoke(() => Editor.ResetBackground());
}
private async Task DetectImage(MediaFileInfo mediaInfo, CancellationTokenSource manualCancellationSource, CancellationToken token)
@@ -571,6 +595,8 @@ public partial class Annotator
var stream = new FileStream(mediaInfo.Path, FileMode.Open);
var detections = await _aiDetector.Detect(stream, token);
await ProcessDetection((TimeSpan.FromMilliseconds(0), stream), detections, token);
if (detections.Count != 0)
mediaInfo.HasAnnotations = true;
}
catch (Exception e)
{
@@ -584,18 +610,38 @@ public partial class Annotator
var prevSeekTime = 0.0;
await foreach (var timeframe in _vlcFrameExtractor.ExtractFrames(mediaInfo.Path, token))
{
Console.WriteLine($"Detect time: {timeframe.Time}");
try
{
var detections = await _aiDetector.Detect(timeframe.Stream, token);
var isValid = IsValidDetection(timeframe.Time, detections);
if (timeframe.Time.TotalSeconds > prevSeekTime + 1)
{
Dispatcher.Invoke(() => SeekTo(timeframe.Time));
prevSeekTime = timeframe.Time.TotalSeconds;
if (!isValid) //Show frame anyway
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
timeframe.Stream.Seek(0, SeekOrigin.Begin);
bitmap.StreamSource = timeframe.Stream;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
bitmap.Freeze();
Dispatcher.Invoke(() =>
{
Editor.RemoveAllAnns();
Editor.Background = new ImageBrush { ImageSource = bitmap };
});
}
}
if (!IsValidDetection(timeframe.Time, detections))
if (!isValid)
continue;
mediaInfo.HasAnnotations = true;
await ProcessDetection(timeframe, detections, token);
}
catch (Exception ex)