-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMainWindowViewModel.cs
304 lines (268 loc) · 9.27 KB
/
MainWindowViewModel.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace YoloMarkNet
{
public class Image : Notifier
{
private bool _isSelected;
public string Path { get; set; }
public ImageSource Thumb { get; set; }
public bool IsSelected
{
get { return _isSelected; }
set { _isSelected = value; NotifyPropertyChanged(); }
}
}
public class BoundingBox : Notifier
{
public BoundingBox(Rect rect, Class c) { this.rect = rect; Class = c; }
private Rect rect;
public Class Class { get; }
public double X
{
get { return rect.X; }
set
{
rect.X = value;
NotifyPropertyChanged();
}
}
public double Y
{
get { return rect.Y; }
set
{
rect.Y = value;
NotifyPropertyChanged();
}
}
public double Width
{
get { return rect.Width; }
set
{
rect.Width = value;
NotifyPropertyChanged();
}
}
public double Height
{
get { return rect.Height; }
set
{
rect.Height = value;
NotifyPropertyChanged();
}
}
}
public class Class
{
public Class(string desc, Brush color)
{
ClassDescriptor = desc;
Color = color;
}
public string ClassDescriptor { get; }
public Brush Color { get; }
}
public class MainWindowViewModel : Notifier
{
private ImageSource _selectedImageSource;
private Class _selectedClass;
private bool _isMouseDown;
private Rect _ghost;
public Rect Ghost
{
get { return _ghost; }
set { _ghost = value; NotifyPropertyChanged(); }
}
public bool IsMouseDown
{
get { return _isMouseDown; }
set { _isMouseDown = value; NotifyPropertyChanged(); }
}
public IList<Class> Classes { get; }
public IList<Image> Images { get; }
public IList<Brush> Colors { get; }
public ObservableCollection<BoundingBox> BoundingBoxes { get; } = new ObservableCollection<BoundingBox>();
public bool IsLastImage => SelectedImage == Images.Last();
public Image SelectedImage
{
get { return Images.FirstOrDefault(f => f.IsSelected); }
set
{
SelectedImageSource = null;
foreach (var image in Images)
{
if (image == value)
{
image.IsSelected = true;
SelectedImageSource = LoadImage(image.Path);
}
else
{
image.IsSelected = false;
}
}
BoundingBoxes.Clear();
var path = SelectedImage.Path.Substring(0, SelectedImage.Path.Length - 4) + ".txt";
if (File.Exists(path))
{
var boundingBoxes = File.ReadAllLines(path);
foreach (var bb in boundingBoxes)
{
var split = bb.Split(' ');
var x = double.Parse(split[1]) * SelectedImageSource.Width;
var y = double.Parse(split[2]) * SelectedImageSource.Height;
var w = double.Parse(split[3]) * SelectedImageSource.Width;
var h = double.Parse(split[4]) * SelectedImageSource.Height;
var classIdx = int.Parse(split[0]);
var rect = new Rect(x, y, w, h);
BoundingBoxes.Add(new BoundingBox(rect, Classes[classIdx]));
}
}
NotifyPropertyChanged();
NotifyPropertyChanged(nameof(IsLastImage));
}
}
public ImageSource SelectedImageSource
{
get { return _selectedImageSource; }
set { _selectedImageSource = value; NotifyPropertyChanged(); }
}
public Class SelectedClass
{
get { return _selectedClass; }
set { _selectedClass = value; NotifyPropertyChanged(); }
}
public MainWindowViewModel()
{
System.IO.Directory.CreateDirectory("data\\img");
//Fix design time errors
if (DesignerProperties.GetIsInDesignMode(new System.Windows.DependencyObject())) return;
Classes = System.IO.File.ReadAllText("data\\obj.names")
.Split('\n')
.Select(f => new Class(f.Trim(), GetPseudorandomBrush()))
.ToList();
Images = System.IO.Directory.GetFiles("data\\img", "*.jpg").Select(f => new Image()
{
Path = f,
Thumb = LoadImage(f, true)
}).ToList();
SelectedImage = Images.FirstOrDefault();
if(SelectedImage == null)
{
MessageBox.Show("No images found, please place images into data/img/");
Application.Current.MainWindow.Close();
return;
}
var data = "";
foreach (var img in Images) data += $"{img.Path}{Environment.NewLine}";
File.WriteAllText("data\\train.txt", data);
SelectedClass = Classes.FirstOrDefault();
if(SelectedClass == null)
{
MessageBox.Show("No classes found, please enter classes into data/obj.names");
Application.Current.MainWindow.Close();
return;
}
}
static Random seededRandom = new Random(694201337);
private Brush GetPseudorandomBrush()
{
var brushes = typeof(Brushes).GetProperties();
int random = seededRandom.Next(brushes.Length);
return (Brush)brushes[random].GetValue(null, null);
}
private ImageSource LoadImage(string path, bool thumb = false)
{
var src = new BitmapImage();
using (FileStream stream = File.OpenRead(path))
{
src.BeginInit();
src.StreamSource = stream;
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
}
if (thumb)
{
var scaleX = 128f / src.PixelWidth;
var scaleY = 128f / src.PixelHeight;
return new TransformedBitmap(src, new ScaleTransform(scaleX, scaleY));
}
return src;
}
public void MouseMove(Point point)
{
Ghost = new Rect(mouseDownLocation, point);
}
private Point mouseDownLocation;
public void MouseDown(Point point)
{
IsMouseDown = true;
mouseDownLocation = point;
}
public void MouseUp(Point point)
{
if(IsMouseDown)
{
var rect = new Rect(mouseDownLocation, point);
BoundingBoxes.Add(new BoundingBox(rect, SelectedClass));
}
IsMouseDown = false;
}
public void MouseLeave()
{
IsMouseDown = false;
}
public ICommand SelectImageCommand => new Command(p =>
{
SaveCurrentImage();
SelectedImage = (Image)p;
});
public ICommand DeleteBoundingBoxCommand => new Command(p =>
{
BoundingBoxes.Remove((BoundingBox)p);
});
public ICommand SaveCommand => new Command(_ =>
{
SaveCurrentImage();
var currentImgIdx = Images.IndexOf(SelectedImage);
if (currentImgIdx < Images.Count - 1)
{
SelectedImage = Images[currentImgIdx + 1];
}
else
{
Application.Current.MainWindow.Close();
}
});
private void SaveCurrentImage()
{
var path = SelectedImage.Path.Substring(0, SelectedImage.Path.Length - 4) + ".txt";
var data = "";
foreach (var bb in BoundingBoxes)
{
var classIndex = Classes.IndexOf(bb.Class);
var scaledX = bb.X / SelectedImageSource.Width;
var scaledY = bb.Y / SelectedImageSource.Height;
var scaledWidth = bb.Width / SelectedImageSource.Width;
var scaledHeight = bb.Height / SelectedImageSource.Height;
data += $"{classIndex} {scaledX} {scaledY} {scaledWidth} {scaledHeight}{Environment.NewLine}";
}
File.WriteAllText(path, data);
}
}
}