add MediaHash. Step1

This commit is contained in:
Oleksandr Bezdieniezhnykh
2025-11-17 07:46:05 +02:00
parent d355f81c63
commit fd95d2ba2c
27 changed files with 421 additions and 288 deletions
+22 -31
View File
@@ -178,7 +178,7 @@ public class AnnotatorEventHandler(
var focusedElement = FocusManager.GetFocusedElement(mainWindow);
if (focusedElement is ListViewItem item)
{
if (item.DataContext is not MediaFileInfo mediaFileInfo)
if (item.DataContext is not MediaFile mediaFileInfo)
return;
mainWindow.DeleteMedia(mediaFileInfo);
}
@@ -245,7 +245,7 @@ public class AnnotatorEventHandler(
{
if (mainWindow.LvFiles.SelectedItem == null)
return;
var mediaInfo = (MediaFileInfo)mainWindow.LvFiles.SelectedItem;
var mediaInfo = (MediaFile)mainWindow.LvFiles.SelectedItem;
if (formState.CurrentMedia == mediaInfo)
return; //already loaded
@@ -261,12 +261,12 @@ public class AnnotatorEventHandler(
//need to wait a bit for correct VLC playback event handling
await Task.Delay(100, ct);
mediaPlayer.Stop();
mediaPlayer.Play(new Media(libVlc, mediaInfo.Path));
mediaPlayer.Play(new Media(libVlc, mediaInfo.MediaUrl));
}
else
{
formState.BackgroundTime = TimeSpan.Zero;
var image = await mediaInfo.Path.OpenImage();
var image = await mediaInfo.MediaUrl.OpenImage();
formState.CurrentMediaSize = new Size(image.PixelWidth, image.PixelHeight);
mainWindow.Editor.SetBackground(image);
mediaPlayer.Stop();
@@ -281,11 +281,10 @@ public class AnnotatorEventHandler(
return;
var time = formState.BackgroundTime ?? TimeSpan.FromMilliseconds(mediaPlayer.Time);
var timeName = formState.MediaName.ToTimeName(time);
var timeName = formState.CurrentMedia.Name.ToTimeName(time);
var isVideo = formState.CurrentMedia.MediaType == MediaTypes.Video;
var imgPath = Path.Combine(dirConfig.Value.ImagesDirectory, $"{timeName}{Constants.JPG_EXT}");
formState.CurrentMedia.HasAnnotations = mainWindow.Editor.CurrentDetections.Count != 0;
var annotations = await SaveAnnotationInner(imgPath, cancellationToken);
if (isVideo)
{
@@ -297,7 +296,7 @@ public class AnnotatorEventHandler(
// var annGrid = mainWindow.DgAnnotations;
// annGrid.SelectedIndex = Math.Min(annGrid.Items.Count, annGrid.SelectedIndex + 1);
// mainWindow.OpenAnnotationResult((AnnotationResult)annGrid.SelectedItem);
mainWindow.Editor.SetBackground(null);
formState.BackgroundTime = null;
}
@@ -310,17 +309,19 @@ public class AnnotatorEventHandler(
mainWindow.Editor.RemoveAllAnns();
}
private async Task<List<Annotation>> SaveAnnotationInner(string imgPath, CancellationToken cancellationToken = default)
private async Task<List<Annotation>> SaveAnnotationInner(string imgPath, CancellationToken ct = default)
{
var canvasDetections = mainWindow.Editor.CurrentDetections.Select(x => x.ToCanvasLabel()).ToList();
var source = (mainWindow.Editor.BackgroundImage.Source as BitmapSource)!;
var mediaSize = new Size(source.PixelWidth, source.PixelHeight);
var annotationsResult = new List<Annotation>();
var time = formState.BackgroundTime ?? TimeSpan.FromMilliseconds(mediaPlayer.Time);
if (!File.Exists(imgPath))
{
if (mediaSize.FitSizeForAI())
await source.SaveImage(imgPath, cancellationToken);
await source.SaveImage(imgPath, ct);
else
{
//Tiling
@@ -331,17 +332,16 @@ public class AnnotatorEventHandler(
.ToList();
//2. Split to frames
var results = TileProcessor.Split(formState.CurrentMediaSize, detectionCoords, cancellationToken);
var results = TileProcessor.Split(formState.CurrentMediaSize, detectionCoords, ct);
//3. Save each frame as a separate annotation
foreach (var res in results)
{
var time = TimeSpan.Zero;
var annotationName = $"{formState.MediaName}{Constants.SPLIT_SUFFIX}{res.Tile.Width}_{res.Tile.Left:0000}_{res.Tile.Top:0000}!".ToTimeName(time);
var annotationName = $"{formState.CurrentMedia?.Name ?? ""}{Constants.SPLIT_SUFFIX}{res.Tile.Width}_{res.Tile.Left:0000}_{res.Tile.Top:0000}!".ToTimeName(time);
var tileImgPath = Path.Combine(dirConfig.Value.ImagesDirectory, $"{annotationName}{Constants.JPG_EXT}");
var bitmap = new CroppedBitmap(source, new Int32Rect((int)res.Tile.Left, (int)res.Tile.Top, (int)res.Tile.Width, (int)res.Tile.Height));
await bitmap.SaveImage(tileImgPath, cancellationToken);
await bitmap.SaveImage(tileImgPath, ct);
var frameSize = new Size(res.Tile.Width, res.Tile.Height);
var detections = res.Detections
@@ -349,18 +349,17 @@ public class AnnotatorEventHandler(
.Select(x => new Detection(annotationName, new YoloLabel(x, frameSize)))
.ToList();
annotationsResult.Add(await annotationService.SaveAnnotation(formState.MediaName, annotationName, time, detections, token: cancellationToken));
annotationsResult.Add(await annotationService.SaveAnnotation(formState.CurrentMediaHash, formState.CurrentMediaName, annotationName, time, detections, token: ct));
}
return annotationsResult;
}
}
var timeImg = formState.BackgroundTime ?? TimeSpan.FromMilliseconds(mediaPlayer.Time);
var annName = formState.MediaName.ToTimeName(timeImg);
var annName = (formState.CurrentMedia?.Name ?? "").ToTimeName(time);
var currentDetections = canvasDetections.Select(x =>
new Detection(annName, new YoloLabel(x, mainWindow.Editor.RenderSize, mediaSize)))
.ToList();
var annotation = await annotationService.SaveAnnotation(formState.MediaName, annName, timeImg, currentDetections, token: cancellationToken);
var annotation = await annotationService.SaveAnnotation(formState.CurrentMediaHash, formState.CurrentMediaName, annName, time, currentDetections, token: ct);
return [annotation];
}
@@ -383,15 +382,7 @@ public class AnnotatorEventHandler(
.Select(x => x.Value).ToList();
mainWindow.TimedAnnotations.Remove(timedAnnotationsToRemove);
if (formState.AnnotationResults.Count == 0)
{
var media = mainWindow.AllMediaFiles.FirstOrDefault(x => x.Name == formState.CurrentMedia?.Name);
if (media != null)
{
media.HasAnnotations = false;
mainWindow.LvFiles.Items.Refresh();
}
}
mainWindow.ReloadFilesThrottled();
});
await dbFactory.DeleteAnnotations(notification.AnnotationNames, ct);
@@ -412,7 +403,8 @@ public class AnnotatorEventHandler(
}
//Only validators can send Delete to the queue
if (!notification.FromQueue && api.CurrentUser.Role.IsValidator())
var currentUser = await api.GetCurrentUserAsync();
if (!notification.FromQueue && currentUser.Role.IsValidator())
await producer.SendToInnerQueue(notification.AnnotationNames, AnnotationStatus.Deleted, ct);
}
catch (Exception e)
@@ -426,9 +418,8 @@ public class AnnotatorEventHandler(
{
mainWindow.Dispatcher.Invoke(() =>
{
var mediaInfo = (MediaFileInfo)mainWindow.LvFiles.SelectedItem;
if ((mediaInfo?.FName ?? "") == e.Annotation.OriginalMediaName)
var mediaInfo = (MediaFile)mainWindow.LvFiles.SelectedItem;
if ((mediaInfo?.Name ?? "") == e.Annotation.OriginalMediaName)
mainWindow.AddAnnotation(e.Annotation);
var log = string.Join(Environment.NewLine, e.Annotation.Detections.Select(det =>
@@ -441,7 +432,7 @@ public class AnnotatorEventHandler(
var media = mainWindow.MediaFilesDict.GetValueOrDefault(e.Annotation.OriginalMediaName);
if (media != null)
media.HasAnnotations = true;
mainWindow.ReloadFilesThrottled();
mainWindow.LvFiles.Items.Refresh();
mainWindow.StatusHelp.Text = log;