-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainViewModel.cs
114 lines (101 loc) · 3.99 KB
/
MainViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using CsvHelper;
using Microsoft.Win32;
using Reactive.Bindings;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace LetterClassifier
{
class MainViewModel
{
private string _folderPath = @"\\Mac\Home\Documents\stat-602-2019-spring\plots";
private List<ManualClassification> manualClassifications = new List<ManualClassification>();
private List<int> xValues = new List<int>();
public MainViewModel()
{
SaveClassificationsCommand.Subscribe(_ => SaveClassifications());
LoadClassificationsCommand.Subscribe(_ => LoadClassifications());
LabelImageCommand = new ReactiveCommand(Letter.Select(x => !string.IsNullOrEmpty(x)));
LabelImageCommand.Subscribe(_ => LabelImage());
LoadFolder();
}
private void LoadFolder() {
xValues.Clear();
var imageFiles = Directory.EnumerateFiles(_folderPath, "*.png");
foreach(var imageFile in imageFiles)
{
var match = _imageFileNameRegex.Match(Path.GetFileName(imageFile));
if (!match.Success)
{
continue;
}
xValues.Add(int.Parse(match.Groups[1].Value));
}
PrepareToClassifyNextImage();
}
private void PrepareToClassifyNextImage()
{
foreach(var x in xValues)
{
if (!manualClassifications.Any(c => c.X == x))
{
ImagePath.Value = _folderPath + "\\image" + x + ".png";
Letter.Value = "";
break;
}
}
}
private void LoadClassifications()
{
var fileOpenDlg = new OpenFileDialog();
fileOpenDlg.DefaultExt = "*.csv";
if (fileOpenDlg.ShowDialog() == true)
{
using (var streamReader = new StreamReader(fileOpenDlg.FileName))
using (var reader = new CsvReader(streamReader))
{
manualClassifications = reader.GetRecords<ManualClassification>().ToList();
}
}
PrepareToClassifyNextImage();
}
private void SaveClassifications()
{
var fileSaveDlg = new SaveFileDialog();
fileSaveDlg.DefaultExt = "*.csv";
if (fileSaveDlg.ShowDialog() == true)
{
using (var streamWriter = new StreamWriter(fileSaveDlg.FileName))
using (var writer = new CsvWriter(streamWriter))
{
writer.WriteHeader<ManualClassification>();
writer.NextRecord();
writer.WriteRecords(manualClassifications);
}
}
}
private static Regex _imageFileNameRegex = new Regex(@"image(\d+)\.png");
private void LabelImage()
{
var match = _imageFileNameRegex.Match(Path.GetFileName(ImagePath.Value));
var capture = int.Parse(match.Groups[1].Value);
manualClassifications.Add(new ManualClassification
{
Letter = Letter.Value,
X = capture
});
PrepareToClassifyNextImage();
}
public ReactiveCommand LabelImageCommand { get; }
public ReactiveCommand SaveClassificationsCommand { get; } = new ReactiveCommand();
public ReactiveCommand LoadClassificationsCommand { get; } = new ReactiveCommand();
public ReactiveProperty<string> ImagePath { get; } = new ReactiveProperty<string>();
public ReactiveProperty<string> Letter { get; } = new ReactiveProperty<string>();
}
}