forked from microsoft/CameraTraps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm.cs
More file actions
502 lines (441 loc) · 19.2 KB
/
Form.cs
File metadata and controls
502 lines (441 loc) · 19.2 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CameraTrapJsonManagerApp
{
#region ProgressBar delegate declarations
delegate void SetProgressBarStyleCallback(ProgressBarStyle style);
delegate void SetProgressBarCallback(long value, long maximumvalue,
ProgressBarStyle barStyle, bool showProgressBar);
delegate void SetStatusTextboxMsgCallback(string text);
delegate void SetLabelProgressMsgOnlyCallback(Label lbl, string text);
delegate void SetLabelProgressMsgandTextColorCallback(Label lbl, string text, Color textColor);
delegate void ShowHideProgressCallback(bool show);
#endregion
public partial class Form : System.Windows.Forms.Form
{
SubsetJsonDetectorOutputOptions options = new SubsetJsonDetectorOutputOptions();
public Form()
{
InitializeComponent();
//center the form in the middle of the computer screen
this.StartPosition = FormStartPosition.Manual;
this.Left = (Screen.PrimaryScreen.Bounds.Width - this.Width) / 2;
}
#region Form and control events
//private void Form_Paint(object sender, PaintEventArgs e)
//{
// System.Drawing.Graphics graphics = e.Graphics;
// System.Drawing.Rectangle gradient_rectangle = new System.Drawing.Rectangle(0, 0, this.Width, this.Height);
// System.Drawing.Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(gradient_rectangle, Color.White, Color.CornflowerBlue, 65f);
// graphics.FillRectangle(b, gradient_rectangle);
//}
private void panelSplitFolderMode_Paint(object sender, PaintEventArgs e)
{
panelSplitFolderMode.CreateGraphics().DrawRectangle(Pens.DimGray,
comboBoxSplitFolderMode.Left - 1,
comboBoxSplitFolderMode.Top - 1,
comboBoxSplitFolderMode.Width + 1,
comboBoxSplitFolderMode.Height + 1
);
}
private void form_Load(object sender, EventArgs e)
{
comboBoxSplitFolderMode.SelectedIndex = 1;
labelProgressMsg.Text = string.Empty;
labelProgressPercentage.Text = string.Empty;
textboxInputFile.Text = string.Empty;
textboxOutputFolderFile.Text = string.Empty;
this.Text += " (version " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() + ")";
#if DEBUG
// textboxInputFile.Text = @"C:\temp\980_detections_uw_gardner_mar2020_20200325233026.json";
textboxInputFile.Text = @"C:\temp\json_test.json";
textboxOutputFolderFile.Text = @"c:\temp\out";
textboxConfidenceThreshold.Text = "0.5";
textBoxSplitParameter.Text = "1";
comboBoxSplitFolderMode.SelectedItem = "NFromTop";
checkBoxMakeFolderRelative.Checked = true;
checkBoxOverwriteJsonFiles.Checked = true;
checkBoxSplitFolders.Checked = true;
#endif
}
private void buttonBrowse_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textboxInputFile.Text = openFileDialog.FileName;
}
}
private void buttonSubmit_Click(object sender, EventArgs e)
{
if (this.GetOptions())
{
labelProgressMsg.ForeColor = Color.MediumBlue;
panelMain.Enabled = false;
richTextboxStatus.Text = string.Empty;
panelMain.Enabled = false;
backgroundWorkerMain.RunWorkerAsync();
labelProgressMsg.Focus();
progressBar1.Visible = false;
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
backgroundWorkerProgressReporter.ReportProgress(0, new ProgressDetails { RemoveProgressInfo = true });
SubsetJsonDetectorOutput sub = new SubsetJsonDetectorOutput(backgroundWorkerProgressReporter, options);
var data = sub.SubsetJsonDetectorOutputMain(textboxInputFile.Text.Trim(), textboxOutputFolderFile.Text.Trim(), options, null);
if (data != null)
{
backgroundWorkerProgressReporter.ReportProgress(0, new ProgressDetails
{
ShowProgressBar = false,
SetlabelProgressMessage = true,
Message = "Program executed successfully"
});
}
else
{
backgroundWorkerProgressReporter.ReportProgress(0, new ProgressDetails
{
ShowProgressBar = false,
SetlabelProgressMessage = true,
Message = "Program executed with error",
IsError = true
});
}
ShowHideProgressBar(false);
}
catch (Exception ex)
{
backgroundWorkerProgressReporter.ReportProgress(0, new ProgressDetails
{
ShowProgressBar = false,
SetStatusTextBoxMessage = true,
Message = ex.ToString()
});
}
}
private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ProgressDetails p = (ProgressDetails)e.UserState;
if (p.RemoveProgressInfo)
{
SetLabelProgressMsg(labelProgressMsg, "");
SetLabelProgressMsg(labelProgressPercentage, "");
SetStatusTextboxMsg("");
SetProgressBar(0);
}
if (p.ShowProgressBar)
{
if (p.style == ProgressBarStyle.Marquee)
SetProgressBar(10, 100, ProgressBarStyle.Marquee);
else
SetProgressBar(p.CurrentCount, p.Maximum, ProgressBarStyle.Continuous);
}
if (p.SetlabelProgressMessage)
{
if (p.IsError)
SetLabelProgressMsgandTextColor(labelProgressMsg, p.Message, Color.Red);
else
SetLabelProgressMsgandTextColor(labelProgressMsg, p.Message, Color.MediumBlue);
}
if(p.SetlabelProgressPercentageMessage)
{
if (p.IsError)
SetLabelProgressMsgandTextColor(labelProgressPercentage, p.Message, Color.Red);
else
SetLabelProgressMsgandTextColor(labelProgressPercentage, p.Message, Color.Black);
}
if (p.SetStatusTextBoxMessage)
SetStatusTextboxMsg(p.Message +"\n");
}
private void backgroundWorkerMain_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
panelMain.Enabled = true;
richTextboxStatus.SelectionStart = richTextboxStatus.Text.Length;
richTextboxStatus.ScrollToCaret();
richTextboxStatus.Refresh();
}
private void textboxConfidenceThreshold_TextChanged(object sender, EventArgs e)
{
TextBoxChangeControlBorderColor(textboxConfidenceThreshold, Pens.Black);
}
private void checkBoxCopyJsonstoFolders_CheckedChanged(object sender, EventArgs e)
{
checkBoxCopyJsonstoFolders.ForeColor = Color.Black;
}
private void textboxInputFile_TextChanged(object sender, EventArgs e)
{
TextBoxChangeControlBorderColor(textboxInputFile, Pens.DimGray);
}
private void textboxOutputFolderFile_TextChanged(object sender, EventArgs e)
{
TextBoxChangeControlBorderColor(textboxOutputFolderFile, Pens.DimGray);
}
#endregion
private bool GetOptions()
{
options.Query = textboxQuery.Text.Trim();
options.Replacement = textboxReplacement.Text.Trim();
if (string.IsNullOrEmpty(textboxInputFile.Text.Trim()))
{
TextBoxChangeControlBorderColor(textboxInputFile, Pens.Red);
backgroundWorkerProgressReporter.ReportProgress(0, new ProgressDetails
{
ShowProgressBar = false,
SetlabelProgressMessage = true,
Message = "Please select an input file"
});
return false;
}
if (string.IsNullOrEmpty(textboxOutputFolderFile.Text.Trim()))
{
TextBoxChangeControlBorderColor(textboxOutputFolderFile, Pens.Red);
backgroundWorkerProgressReporter.ReportProgress(0, new ProgressDetails
{
ShowProgressBar = false,
SetlabelProgressMessage = true,
Message = "Please enter value for output file or folder name"
});
return false;
}
if (checkBoxCopyJsonstoFolders.Checked)
{
if (!(checkBoxSplitFolders.Checked && checkBoxMakeFolderRelative.Checked))
{
checkBoxCopyJsonstoFolders.ForeColor = Color.Red;
backgroundWorkerProgressReporter.ReportProgress(0, new ProgressDetails
{
ShowProgressBar = false,
SetlabelProgressMessage = true,
Message = "'Copy jsons to folders' set without 'make folder-relative' and 'split folders'"
});
return false;
}
}
if (!string.IsNullOrEmpty(textboxConfidenceThreshold.Text.Trim()))
{
double confidenceThreshold;
if (double.TryParse(textboxConfidenceThreshold.Text.Trim(), out confidenceThreshold))
options.ConfidenceThreshold = confidenceThreshold;
else
{
panelMain.CreateGraphics().DrawRectangle(Pens.Red,
textboxConfidenceThreshold.Left - 1,
textboxConfidenceThreshold.Top - 1,
textboxConfidenceThreshold.Width + 1,
textboxConfidenceThreshold.Height + 1
);
textboxConfidenceThreshold.Refresh();
backgroundWorkerProgressReporter.ReportProgress(0, new ProgressDetails
{
ShowProgressBar = false,
SetlabelProgressMessage = true,
Message = "Confidence threshold should be numeric"
});
return false;
}
if(confidenceThreshold < 0 | confidenceThreshold > 1)
{
panelMain.CreateGraphics().DrawRectangle(Pens.Red,
textboxConfidenceThreshold.Left - 1,
textboxConfidenceThreshold.Top - 1,
textboxConfidenceThreshold.Width + 1,
textboxConfidenceThreshold.Height + 1
);
textboxConfidenceThreshold.Refresh();
backgroundWorkerProgressReporter.ReportProgress(0, new ProgressDetails
{
ShowProgressBar = false,
SetlabelProgressMessage = true,
Message = "Confidence threshold should be between 0 and 1"
});
return false;
}
}
else
{
options.ConfidenceThreshold = -1;
}
options.SplitFolders = checkBoxSplitFolders.Checked;
options.SplitFolderMode = comboBoxSplitFolderMode.SelectedItem.ToString();
options.MakeFolderRelative = checkBoxMakeFolderRelative.Checked;
options.OverwriteJsonFiles = checkBoxOverwriteJsonFiles.Checked;
options.CopyJsonstoFolders = checkBoxCopyJsonstoFolders.Checked;
options.CopyJsonstoFoldersDirectoriesMustExist = !(checkBoxCreateFolders.Checked);
String splitParameterString = textBoxSplitParameter.Text.ToString().Trim();
if (splitParameterString.Length > 0)
{
int splitParam;
if (int.TryParse(splitParameterString, out splitParam))
options.nDirectoryParam = splitParam;
else
{
backgroundWorkerProgressReporter.ReportProgress(0, new ProgressDetails
{
ShowProgressBar = false,
SetlabelProgressMessage = true,
Message = "Split parameter should be an integer"
});
return false;
}
}
return true;
} // private bool GetOptions()
private void TextBoxChangeControlBorderColor(TextBox txtbox, Pen penColor)
{
panelMain.CreateGraphics().DrawRectangle(penColor,
txtbox.Left - 1,
txtbox.Top - 1,
txtbox.Width + 1,
txtbox.Height + 1
);
txtbox.Refresh();
}
#region Progressbar delegates
private void SetProgressBar(long value, long maximumvalue = 100,
ProgressBarStyle barStyle = ProgressBarStyle.Blocks, bool showProgressBar = true)
{
if (this.progressBar1.InvokeRequired)
{
SetProgressBarCallback d = new SetProgressBarCallback(SetProgressBar);
this.Invoke(d, new object[] { value, maximumvalue, barStyle, showProgressBar });
}
else
{
progressBar1.Visible = showProgressBar;
if (showProgressBar)
{
progressBar1.Style = barStyle;
if (barStyle == ProgressBarStyle.Marquee)
{
progressBar1.MarqueeAnimationSpeed = 30;
progressBar1.Value = 10;
}
else
{
progressBar1.Style = barStyle;
int percentage = SharedFunctions.GetProgressPercentage(value, maximumvalue);
//if (percentage < 1)
//{
// percentage = 1;
//}
progressBar1.Maximum = 100;
//progressBar1.SetProgressNoAnimation(percentage);
progressBar1.Value = percentage;
}
}
}
}
private void SetProgressBarStyle(ProgressBarStyle style)
{
if (this.progressBar1.InvokeRequired)
{
SetProgressBarStyleCallback d = new SetProgressBarStyleCallback(SetProgressBarStyle);
this.Invoke(d, new object[] { style });
}
else
{
if (style == ProgressBarStyle.Marquee)
progressBar1.Style = ProgressBarStyle.Marquee;
else
progressBar1.Style = ProgressBarStyle.Blocks;
}
}
private void ShowHideProgressBar(bool show)
{
if (this.progressBar1.InvokeRequired)
{
ShowHideProgressCallback d = new ShowHideProgressCallback(ShowHideProgressBar);
this.Invoke(d, new object[] { show });
}
else
{
progressBar1.Visible = show;
}
if (this.labelProgressPercentage.InvokeRequired)
{
ShowHideProgressCallback d = new ShowHideProgressCallback(ShowHideProgressBar);
this.Invoke(d, new object[] { show });
}
else
{
labelProgressPercentage.Visible = false;
}
}
private void SetStatusTextboxMsg(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.richTextboxStatus.InvokeRequired)
{
SetStatusTextboxMsgCallback d = new SetStatusTextboxMsgCallback(SetStatusTextboxMsg);
this.Invoke(d, new object[] { text });
}
else
{
this.richTextboxStatus.AppendText(text);
}
}
private void SetLabelProgressMsgandTextColor(Label lbl, string text, Color color)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (lbl.InvokeRequired)
{
SetLabelProgressMsgandTextColorCallback d =
new SetLabelProgressMsgandTextColorCallback(SetLabelProgressMsgandTextColor);
lbl.Invoke(d, new object[] { lbl, text, color });
}
else
{
lbl.Text = text;
lbl.ForeColor = color;
}
}
private void SetLabelProgressMsg(Label lbl, string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (lbl.InvokeRequired)
{
SetLabelProgressMsgOnlyCallback d =
new SetLabelProgressMsgOnlyCallback(SetLabelProgressMsg);
lbl.Invoke(d, new object[] { lbl, text });
}
else
{
lbl.Text = text;
}
}
#endregion
private void ButtonHelp_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://github.com/microsoft/CameraTraps/blob/master/api/batch_processing/postprocessing/CameraTrapJsonManagerApp.md");
}
private void buttonBrowseFolder_Click(object sender, EventArgs e)
{
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
textboxOutputFolderFile.Text = folderBrowserDialog.SelectedPath;
}
}
}