add image editing

This commit is contained in:
Oleksandr Bezdieniezhnykh
2024-08-07 13:22:17 +03:00
parent c72f7fc265
commit a81a6f881d
11 changed files with 146 additions and 57 deletions
+42 -14
View File
@@ -34,8 +34,7 @@ public partial class MainWindow
private readonly TimeSpan _thresholdAfter = TimeSpan.FromMilliseconds(300);
private readonly Config _config;
public Dictionary<TimeSpan, List<YoloLabel>> AnnotationsDict { get; set; } = new();
private IntervalTree<TimeSpan, List<YoloLabel>> Annotations { get; set; } = new();
public IntervalTree<TimeSpan, List<YoloLabel>> Annotations { get; set; } = new();
public MainWindow(LibVLC libVLC, MediaPlayer mediaPlayer,
IMediator mediator,
@@ -113,12 +112,21 @@ public partial class MainWindow
{
VideoView.MediaPlayer = _mediaPlayer;
_mediaPlayer.Playing += (sender, args) =>
_mediaPlayer.Playing += async (sender, args) =>
{
uint vw = 0, vh = 0;
_mediaPlayer.Size(0, ref vw, ref vh);
_formState.CurrentVideoSize = new Size(vw, vh);
_formState.CurrentVideoLength = TimeSpan.FromMilliseconds(_mediaPlayer.Length);
await Dispatcher.Invoke(async () => await ReloadAnnotations());
if (_formState.CurrentMedia?.MediaType != MediaTypes.Image)
return;
//if image show annotations, give 100ms to load the frame and set on pause
await Task.Delay(100);
ShowCurrentAnnotations();
_mediaPlayer.SetPause(true);
};
LvFiles.MouseDoubleClick += async (_, _) => await _mediator.Publish(new PlaybackControlEvent(PlaybackControlEnum.Play));
@@ -165,6 +173,8 @@ public partial class MainWindow
saveConfigFn.Debounce(TimeSpan.FromSeconds(5)).Invoke();
}
public void ShowCurrentAnnotations() => ShowTimeAnnotations(TimeSpan.FromMilliseconds(_mediaPlayer.Time));
private void ShowTimeAnnotations(TimeSpan time)
{
Dispatcher.Invoke(() => VideoSlider.Value = _mediaPlayer.Position * VideoSlider.Maximum);
@@ -181,11 +191,11 @@ public partial class MainWindow
}
}
public void ReloadAnnotations()
public async Task ReloadAnnotations()
{
_formState.AnnotationResults.Clear();
AnnotationsDict.Clear();
Annotations.Clear();
Editor.RemoveAllAnns();
var labelDir = new DirectoryInfo(_config.LabelsDirectory);
if (!labelDir.Exists)
@@ -197,20 +207,27 @@ public partial class MainWindow
var name = Path.GetFileNameWithoutExtension(file.Name);
var time = _formState.GetTime(name)!.Value;
var str = File.ReadAllText(file.FullName);
var str = await File.ReadAllTextAsync(file.FullName);
var annotations = str.Split(Environment.NewLine).Select(YoloLabel.Parse)
.Where(ann => ann != null)
.ToList();
AddAnnotation(time, annotations!);
await AddAnnotation(time, annotations!);
}
}
public async Task AddAnnotation(TimeSpan time, List<YoloLabel> annotations)
{
var fName = _formState.GetTimeName(time);
AnnotationsDict.Add(time, annotations!);
var previousAnnotations = Annotations.Query(time);
Annotations.Remove(previousAnnotations);
Annotations.Add(time.Subtract(_thresholdBefore), time.Add(_thresholdAfter), annotations);
var existingResult = _formState.AnnotationResults.FirstOrDefault(x => x.Time == time);
if (existingResult != null)
_formState.AnnotationResults.Remove(existingResult);
_formState.AnnotationResults.Add(new AnnotationResult(time, fName, annotations, _config));
await File.WriteAllTextAsync($"{_config.ResultsDirectory}/{fName}.json", JsonConvert.SerializeObject(_formState.AnnotationResults));
}
@@ -226,25 +243,35 @@ public partial class MainWindow
.GroupBy(x => x)
.Select(gr => gr.Key)
.ToDictionary(x => x);
var files = dir.GetFiles("mp4", "mov").Select(x =>
var videoFiles = dir.GetFiles(_config.VideoFormats.ToArray()).Select(x =>
{
var media = new Media(_libVLC, x.FullName);
media.Parse();
var fInfo = new VideoFileInfo
var fInfo = new MediaFileInfo
{
Name = x.Name,
Path = x.FullName,
MediaType = MediaTypes.Video,
HasAnnotations = labelNames.ContainsKey(Path.GetFileNameWithoutExtension(x.Name).Replace(" ", ""))
};
media.ParsedChanged += (_, _) => fInfo.Duration = TimeSpan.FromMilliseconds(media.Duration);
return fInfo;
}).ToList();
LvFiles.ItemsSource = new ObservableCollection<VideoFileInfo>(files);
var imageFiles = dir.GetFiles(_config.ImageFormats.ToArray()).Select(x => new MediaFileInfo
{
Name = x.Name,
Path = x.FullName,
MediaType = MediaTypes.Image,
HasAnnotations = labelNames.ContainsKey(Path.GetFileNameWithoutExtension(x.Name).Replace(" ", ""))
});
var mediaFiles = videoFiles.Concat(imageFiles).ToList();
LvFiles.ItemsSource = new ObservableCollection<MediaFileInfo>(mediaFiles);
TbFolder.Text = _config.VideosDirectory;
BlinkHelp(files.Count == 0
BlinkHelp(mediaFiles.Count == 0
? HelpTexts.HelpTextsDict[HelpTextEnum.Initial]
: HelpTexts.HelpTextsDict[HelpTextEnum.PlayVideo]);
}
@@ -289,7 +316,7 @@ public partial class MainWindow
_mediator.Publish(new PlaybackControlEvent(_mediaPlayer.CanPause ? PlaybackControlEnum.Pause : PlaybackControlEnum.Play));
}
private void PauseClick(object sender, RoutedEventArgs e)=> _mediator.Publish(new PlaybackControlEvent(PlaybackControlEnum.Pause));
private void PauseClick(object sender, RoutedEventArgs e) => _mediator.Publish(new PlaybackControlEvent(PlaybackControlEnum.Pause));
private void StopClick(object sender, RoutedEventArgs e) => _mediator.Publish(new PlaybackControlEvent(PlaybackControlEnum.Stop));
private void PreviousFrameClick(object sender, RoutedEventArgs e) => _mediator.Publish(new PlaybackControlEvent(PlaybackControlEnum.PreviousFrame));
@@ -321,3 +348,4 @@ public partial class MainWindow
};
}
}