mirror of
https://github.com/azaion/annotations.git
synced 2026-04-23 00:46:31 +00:00
Add annotationResult and put them into json
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Windows;
|
||||
using Azaion.Annotator.DTO;
|
||||
using Azaion.Annotator.Extensions;
|
||||
using LibVLCSharp.Shared;
|
||||
using MediatR;
|
||||
using Microsoft.WindowsAPICodePack.Dialogs;
|
||||
using Newtonsoft.Json;
|
||||
using Point = System.Windows.Point;
|
||||
using Size = System.Windows.Size;
|
||||
|
||||
@@ -22,12 +21,12 @@ public partial class MainWindow
|
||||
|
||||
private readonly Config _config;
|
||||
private readonly HelpWindow _helpWindow;
|
||||
private readonly TimeSpan _annotationTime = TimeSpan.FromSeconds(1);
|
||||
|
||||
public ObservableCollection<AnnotationClass> AnnotationClasses { get; set; } = new();
|
||||
private bool _suspendLayout;
|
||||
|
||||
public Dictionary<string, List<AnnotationInfo>> Annotations { get; set; } = new();
|
||||
|
||||
public Dictionary<TimeSpan, List<YoloLabel>> Annotations { get; set; } = new();
|
||||
public Dictionary<TimeSpan, List<AnnotationResult>> AnnotationResults { get; set; } = new();
|
||||
|
||||
public MainWindow(LibVLC libVLC, MediaPlayer mediaPlayer,
|
||||
IMediator mediator,
|
||||
@@ -115,38 +114,17 @@ public partial class MainWindow
|
||||
Dispatcher.Invoke(() => VideoSlider.Value = _mediaPlayer.Position * VideoSlider.Maximum);
|
||||
Dispatcher.Invoke(() => StatusClock.Text = $"{TimeSpan.FromMilliseconds(_mediaPlayer.Time):mm\\:ss} / {_formState.CurrentVideoLength:mm\\:ss}");
|
||||
|
||||
var curTime = _formState.GetTimeName(TimeSpan.FromMilliseconds(_mediaPlayer.Time));
|
||||
if (!Annotations.TryGetValue(curTime, out var annotationInfos))
|
||||
var time = TimeSpan.FromMilliseconds(_mediaPlayer.Time);
|
||||
if (!Annotations.TryGetValue(time, out var annotations))
|
||||
return;
|
||||
|
||||
var annotations = annotationInfos.Select(info =>
|
||||
foreach (var ann in annotations)
|
||||
{
|
||||
var annClass = _config.AnnotationClasses[info.ClassNumber];
|
||||
var annInfo = info.ToCanvasCoordinates(Editor.RenderSize, _formState.CurrentVideoSize);
|
||||
var annotation = Dispatcher.Invoke(() => Editor.CreateAnnotation(annClass, annInfo));
|
||||
return annotation;
|
||||
}).ToList();
|
||||
|
||||
//remove annotations: either in 1 sec, either earlier if there is next annotation in a dictionary
|
||||
var timeStr = curTime.Split("_").LastOrDefault();
|
||||
if (!int.TryParse(timeStr, out var time))
|
||||
return;
|
||||
|
||||
var ts = TimeSpan.FromMilliseconds(time * 100);
|
||||
var timeSpanRemove = Enumerable.Range(1, (int)_annotationTime.TotalMilliseconds / 100 - 1)
|
||||
.Select(x =>
|
||||
{
|
||||
var timeNext = TimeSpan.FromMilliseconds(x * 100);
|
||||
var fName = _formState.GetTimeName(ts.Add(timeNext));
|
||||
return Annotations.ContainsKey(fName) ? timeNext : (TimeSpan?)null;
|
||||
}).FirstOrDefault(x => x != null) ?? _annotationTime;
|
||||
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(timeSpanRemove);
|
||||
Dispatcher.Invoke(() => Editor.RemoveAnnotations(annotations));
|
||||
});
|
||||
|
||||
var annClass = _config.AnnotationClasses[ann.ClassNumber];
|
||||
var annInfo = new CanvasLabel(ann, Editor.RenderSize, _formState.CurrentVideoSize);
|
||||
Dispatcher.Invoke(() => Editor.CreateAnnotation(annClass, time, annInfo));
|
||||
}
|
||||
Dispatcher.Invoke(() => Editor.ClearExpiredAnnotations(time));
|
||||
};
|
||||
|
||||
VideoSlider.ValueChanged += (value, newValue) =>
|
||||
@@ -173,26 +151,61 @@ public partial class MainWindow
|
||||
|
||||
public void LoadExistingAnnotations()
|
||||
{
|
||||
var dirInfo = new DirectoryInfo(_config.LabelsDirectory);
|
||||
if (!dirInfo.Exists)
|
||||
return;
|
||||
Annotations = LoadAnnotations();
|
||||
_formState.AnnotationResults = LoadAnnotationResults();
|
||||
}
|
||||
|
||||
var files = dirInfo.GetFiles($"{_formState.VideoName}_*");
|
||||
Annotations = files.ToDictionary(f => Path.GetFileNameWithoutExtension(f.Name), f =>
|
||||
private Dictionary<TimeSpan, List<YoloLabel>> LoadAnnotations()
|
||||
{
|
||||
var labelDir = new DirectoryInfo(_config.LabelsDirectory);
|
||||
if (!labelDir.Exists)
|
||||
return new Dictionary<TimeSpan, List<YoloLabel>>();
|
||||
|
||||
var labelFiles = labelDir.GetFiles($"{_formState.VideoName}_*");
|
||||
return labelFiles.Select(x =>
|
||||
{
|
||||
var str = File.ReadAllText(f.FullName);
|
||||
return str.Split(Environment.NewLine).Select(AnnotationInfo.Parse)
|
||||
var name = Path.GetFileNameWithoutExtension(x.Name);
|
||||
return new
|
||||
{
|
||||
Name = x.FullName,
|
||||
Time = _formState.GetTime(name)
|
||||
};
|
||||
}).ToDictionary(f => f.Time!.Value, f =>
|
||||
{
|
||||
var str = File.ReadAllText(f.Name);
|
||||
return str.Split(Environment.NewLine).Select(YoloLabel.Parse)
|
||||
.Where(x => x != null)
|
||||
.ToList();
|
||||
})!;
|
||||
}
|
||||
|
||||
private List<AnnotationResult> LoadAnnotationResults()
|
||||
{
|
||||
var resultDir = new DirectoryInfo(_config!.ResultsDirectory);
|
||||
if (!resultDir.Exists)
|
||||
Directory.CreateDirectory(_config!.ResultsDirectory);
|
||||
|
||||
var resultsFile = resultDir.GetFiles($"{_formState!.CurrentFile}*").FirstOrDefault();
|
||||
|
||||
List<AnnotationResult> results;
|
||||
if (resultsFile == null)
|
||||
{
|
||||
results = Annotations.Select(anns => new AnnotationResult(anns.Key, _formState.GetTimeName(anns.Key), anns.Value))
|
||||
.ToList();
|
||||
File.WriteAllText($"{_config.ResultsDirectory}/{_formState.VideoName}.json", JsonConvert.SerializeObject(results, Formatting.Indented));
|
||||
}
|
||||
else
|
||||
results = JsonConvert.DeserializeObject<List<AnnotationResult>>(File.ReadAllText(File.ReadAllText(resultsFile.FullName)))!;
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
private void ReloadFiles()
|
||||
{
|
||||
var dir = new DirectoryInfo(_config.VideosDirectory);
|
||||
if (!dir.Exists)
|
||||
return;
|
||||
|
||||
|
||||
var files = dir.GetFiles("mp4", "mov").Select(x =>
|
||||
{
|
||||
_mediaPlayer.Media = new Media(_libVLC, x.FullName);
|
||||
|
||||
Reference in New Issue
Block a user