Skip to content

Commit

Permalink
Merge pull request #45 from lacmus-foundation/dev
Browse files Browse the repository at this point in the history
close #40
  • Loading branch information
gosha20777 authored Mar 21, 2020
2 parents d639e0d + 4f99702 commit 381135a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Models/Photo/Photo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace RescuerLaApp.Models.Photo
public class Photo : IPhoto
{
public ImageBrush ImageBrush { get; set; }
public Attribute Attribute { get; set; }
public Attribute Attribute { get; set; } = Attribute.NotProcessed;
public IReadOnlyList<Directory> MetaDataDirectories { get; set; }
}
}
11 changes: 11 additions & 0 deletions ViewModels/FilterViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using ReactiveUI;
using ReactiveUI.Fody.Helpers;

namespace RescuerLaApp.ViewModels
{
public class FilterViewModel : ReactiveObject
{
[Reactive] public int FilterIndex { get; set; } = 0;
[Reactive] public int CurrentPage { get; set; } = 0;
}
}
91 changes: 58 additions & 33 deletions ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,24 @@ public class MainWindowViewModel : ReactiveObject
private readonly string _mlConfigPath = Path.Join("conf", "mlConfig.json");
private int itemPerPage = 500;
private int itemcount;
private int _currentPageIndex;
private int _totalPages;
SourceList<PhotoViewModel> _photos { get; set; } = new SourceList<PhotoViewModel>();
private ReadOnlyObservableCollection<PhotoViewModel> _photoCollection;

public MainWindowViewModel(Window window)
{
_window = window;
var filter = this

var pageFilter = this
.WhenValueChanged(x => x.CurrentPage)
.Select(PageFilter);
var typeFilter = this
.WhenValueChanged(x => x.FilterIndex)
.Select(TypeFilter);

_photos.Connect()
.Filter(filter)
.Filter(pageFilter)
.Filter(typeFilter)
.ObserveOn(RxApp.MainThreadScheduler)
.Bind(out _photoCollection)
.DisposeMany()
Expand Down Expand Up @@ -139,7 +142,6 @@ private void SetupCommand(IObservable<bool> canExecute, IObservable<bool> canSwi
ShowAllMetadataCommand = ReactiveCommand.Create(ShowAllMetadata, canExecute);
ShowGeoDataCommand = ReactiveCommand.Create(ShowGeoData, canExecute);
AddToFavoritesCommand = ReactiveCommand.Create(AddToFavorites, canExecute);
SwitchBoundBoxesVisibilityCommand = ReactiveCommand.Create(SwitchBoundBoxesVisibility, canSwitchBoundBox);
HelpCommand = ReactiveCommand.Create(Help);
AboutCommand = ReactiveCommand.Create(About);
OpenWizardCommand = ReactiveCommand.Create(OpenWizard);
Expand All @@ -156,21 +158,17 @@ private IObservable<bool> CanSetup()

public ReadOnlyObservableCollection<PhotoViewModel> PhotoCollection => _photoCollection;
[Reactive] public int SelectedIndex { get; set; }
[Reactive] public int FilterIndex { get; set; }
[Reactive] public int CurrentPage { get; set; } = 0;

[Reactive] public int FilterIndex { get; set; } = 0;
[Reactive] public PhotoViewModel PhotoViewModel { get; set; }
[Reactive] public ApplicationStatusViewModel ApplicationStatusViewModel { get; set; }
[Reactive] public int CurrentPage
{
get => _currentPageIndex;
set => _currentPageIndex = value;
}
[Reactive] public int TotalPages
{
get => _totalPages;
set => _totalPages = value;
}
// TODO: update with locales
[Reactive] public string BoundBoxesStateString { get; set; } = "Hide bound boxes";
[Reactive] public string FavoritesStateString { get; set; } = "Add to favorites";
[Reactive] public double CanvasWidth { get; set; } = 500;
[Reactive] public double CanvasHeight { get; set; } = 500;
Expand Down Expand Up @@ -266,8 +264,23 @@ private void ShowLastPage()

private Func<PhotoViewModel, bool> PageFilter(int currentPage)
{
return x =>
x.Id >= itemPerPage * CurrentPage && x.Id < itemPerPage * (CurrentPage + 1);
return x =>
x.Id >= itemPerPage * currentPage && x.Id < itemPerPage * (currentPage + 1);
}
private Func<PhotoViewModel, bool> TypeFilter(int fitlerType)
{
SelectedIndex = 0;
switch (fitlerType)
{
case 0:
return x => true;
case 1:
return x => x.Photo.Attribute == Attribute.WithObject;
case 2:
return x => x.Photo.Attribute == Attribute.Favorite;
default:
return x => true;
}
}

private void CalculateTotalPages()
Expand Down Expand Up @@ -390,23 +403,25 @@ private async void PredictAll()
await model.Init();
var count = 0;
var objectCount = 0;
foreach (var photoViewModel in _photoCollection)
foreach (var photoViewModel in _photos.Items)
{
try
{
photoViewModel.Annotation.Objects = await model.Predict(photoViewModel);
photoViewModel.BoundBoxes = photoViewModel.GetBoundingBoxes();
if (photoViewModel.BoundBoxes.Any())
photoViewModel.Photo.Attribute = Attribute.WithObject;
objectCount += photoViewModel.BoundBoxes.Count();
count++;
Console.WriteLine($"\tProgress: {(double) count / _photoCollection.Count() * 100} %");
Console.WriteLine($"\tProgress: {(double) count / _photos.Items.Count() * 100} %");
}
catch (Exception e)
{
Log.Error(e,$"Unable to process file {photoViewModel.Path}. Slipped.");
}
}
await model.Stop();
Log.Information($"Successfully predict {_photoCollection.Count} photos. Find {objectCount} objects.");
Log.Information($"Successfully predict {_photos.Items.Count()} photos. Find {objectCount} objects.");
}
}
catch (Exception e)
Expand Down Expand Up @@ -484,16 +499,16 @@ private async void SaveAll()
{
try
{
if (!_photoCollection.Any())
if (!_photos.Items.Any())
{
Log.Warning("There are no photos to save.");
return;
}
_applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Working, "");
var writer = new PhotoVMWriter(_window);
await writer.WriteMany(_photoCollection);
await writer.WriteMany(_photos.Items);
_applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready, "Success | saved");
Log.Information($"Saved {_photoCollection.Count} photos.");
Log.Information($"Saved {_photos.Items.Count()} photos.");
}
catch (Exception ex)
{
Expand All @@ -506,16 +521,16 @@ private async void SaveAllImagesWithObjects()
{
try
{
if (!_photoCollection.Any())
if (!_photos.Items.Any())
{
Log.Warning("There are no photos to save.");
return;
}
_applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Working, "");
var writer = new PhotoVMWriter(_window);
await writer.WriteMany(_photoCollection.Where(x => x.Photo.Attribute == Attribute.WithObject));
await writer.WriteMany(_photos.Items.Where(x => x.Photo.Attribute == Attribute.WithObject));
_applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready, "Success | saved");
Log.Information($"Saved {_photoCollection.Count} photos.");
Log.Information($"Saved {_photos.Items.Count()} photos.");
}
catch (Exception ex)
{
Expand All @@ -528,16 +543,16 @@ private async void SaveFavoritesImages()
{
try
{
if (!_photoCollection.Any())
if (!_photos.Items.Any())
{
Log.Warning("There are no photos to save.");
return;
}
_applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Working, "");
var writer = new PhotoVMWriter(_window);
await writer.WriteMany(_photoCollection.Where(x => x.Photo.Attribute == Attribute.Favorite));
await writer.WriteMany(_photos.Items.Where(x => x.Photo.Attribute == Attribute.Favorite));
_applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready, "Success | saved");
Log.Information($"Saved {_photoCollection.Count} photos.");
Log.Information($"Saved {_photos.Items.Count()} photos.");
}
catch (Exception ex)
{
Expand Down Expand Up @@ -661,14 +676,22 @@ public void ShowAllMetadata()
*/
}

public void AddToFavorites()
{

}

public void SwitchBoundBoxesVisibility()
public async void AddToFavorites()
{

if (_photoCollection[SelectedIndex].Photo.Attribute != Attribute.Favorite)
{
_photoCollection[SelectedIndex].Photo.Attribute = Attribute.Favorite;
}
else
{
if(_photoCollection[SelectedIndex].BoundBoxes.Any())
_photoCollection[SelectedIndex].Photo.Attribute = Attribute.WithObject;
else
{
_photoCollection[SelectedIndex].Photo.Attribute = Attribute.Empty;
}
}
await UpdateUi();
}

public async void About()
Expand Down Expand Up @@ -772,6 +795,8 @@ await Dispatcher.UIThread.InvokeAsync(() =>
if (_applicationStatusManager.AppStatusInfo.Status == Enums.Status.Ready)
_applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready,
$"{Enums.Status.Ready.ToString()} | {PhotoViewModel.Path}");

FavoritesStateString = PhotoCollection[SelectedIndex].Photo.Attribute == Attribute.Favorite ? "Remove from favorites" : "Add to favorites";

Log.Debug($"Ui updated to index {SelectedIndex}");
});
Expand Down
2 changes: 1 addition & 1 deletion ViewModels/PhotoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ [Reactive] public Annotation Annotation
}
[Reactive] public string Caption { get; private set; }
[Reactive] public string Path { get; private set; }
[Reactive] public IEnumerable<BoundBox> BoundBoxes { get; set; }
[Reactive] public IEnumerable<BoundBox> BoundBoxes { get; set; } = new List<BoundBox>();
public int Id { get; set; }

private void UpdatePhotoInfo(Annotation annotation)
Expand Down
7 changes: 3 additions & 4 deletions Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
<Separator/>
<Button Name="bImagePrev" Command="{Binding PrevImageCommand}">Prev</Button>
<Button Name="bImageNext" Command="{Binding NextImageCommand}">Next</Button>
<Button Name="bWizard" Margin="1" Command="{Binding OpenWizardCommand}">Wizard</Button>
<Button Name="bWizard" Command="{Binding OpenWizardCommand}">Wizard</Button>
<ToggleButton Padding="8" Margin="1" Name="bShowBorder" IsChecked="True" Content="Border" />
</StackPanel>
</ToolTip>
</StackPanel>
Expand Down Expand Up @@ -156,8 +157,6 @@
Width="{Binding CanvasWidth, Mode=TwoWay}">
<Canvas.ContextMenu>
<ContextMenu>
<MenuItem Header="{Binding BoundBoxesStateString}" Command="{Binding SwitchBoundBoxesVisibilityCommand}"/>
<Separator/>
<MenuItem Header="Show geo position" Command="{Binding ShowGeoDataCommand}"/>
<!-- <MenuItem Header="Find nearest photo"/> -->
<MenuItem Header="Show all metadata" Command="{Binding ShowAllMetadataCommand}"/>
Expand All @@ -173,7 +172,7 @@
<Polygon
Points="{Binding Points}"
Stroke="Red" StrokeThickness="10"
IsVisible="{Binding IsVisible}"/>
IsVisible="{Binding #bShowBorder.IsChecked, Mode=OneWay}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Expand Down

0 comments on commit 381135a

Please sign in to comment.