-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
679 lines (587 loc) · 29.4 KB
/
Main.java
File metadata and controls
679 lines (587 loc) · 29.4 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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
package DigitRecogniser;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.*;
/**
* A program to predict the value of drawn and uploaded images
* @author codingmroberts
*/
public class Main extends javax.swing.JFrame {
final DrawArea paint = new DrawArea();
String TEST_LABEL_FILE = "C:/Users/User/Documents/MNIST/t10k-labels.idx1-ubyte";
String TEST_IMAGE_FILE = "C:/Users/User/Documents/MNIST/t10k-images.idx3-ubyte";
String TRAIN_LABEL_FILE = "C:/Users/User/Documents/MNIST/train-labels.idx1-ubyte";
String TRAIN_IMAGE_FILE = "C:/Users/User/Documents/MNIST/train-images.idx3-ubyte";
/**
* Initialise a dataset object
*/
public Dataset primary;
/**
* Initialise designer components
* Initialise components
*/
public Main() {
initComponents();
myInit();
}
private void myInit(){
paintHolder.setLayout(new FlowLayout(FlowLayout.CENTER));
paintHolder.add(paint);
}
static int mostFrequent(int arr[], int n)
{
// Sort the array
Arrays.sort(arr);
// find the max frequency using linear
// traversal
int max_count = 1, res = arr[0];
int curr_count = 1;
for (int i = 1; i < n; i++)
{
if (arr[i] == arr[i - 1])
curr_count++;
else
{
if (curr_count > max_count)
{
max_count = curr_count;
res = arr[i - 1];
}
curr_count = 1;
}
}
// If last element is most frequent
if (curr_count > max_count)
{
max_count = curr_count;
res = arr[n - 1];
}
return res;
}
// An optimized version of Bubble Sort
static void bubbleSort(double arr[], int idx[], int n)
{
int i, j, tempB;
double temp;
boolean swapped;
for (i = 0; i < n - 1; i++)
{
swapped = false;
for (j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
tempB = idx[j];
idx[j] = idx[j + 1];
idx[j + 1] = tempB;
swapped = true;
}
}
// IF no two elements were
// swapped by inner loop, then break
if (swapped == false)
break;
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
tabMenu = new javax.swing.JTabbedPane();
modelPanel = new javax.swing.JPanel();
trainModel = new javax.swing.JButton();
trainLoadingBar = new javax.swing.JProgressBar();
statusLabel = new javax.swing.JLabel();
accuracyLabel = new javax.swing.JLabel();
accuracyValueModel = new javax.swing.JLabel();
testModel = new javax.swing.JButton();
statusLabel1 = new javax.swing.JLabel();
trainLoadingBar1 = new javax.swing.JProgressBar();
drawDigitPanel = new javax.swing.JPanel();
jPanel1 = new javax.swing.JPanel();
previewPanel = new javax.swing.JPanel();
predictionValueDraw = new javax.swing.JLabel();
predictPaint = new javax.swing.JButton();
clearPaint = new javax.swing.JButton();
paintHolder = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
accuracyValueDraw = new javax.swing.JLabel();
uploadDigitPanel = new javax.swing.JPanel();
predictionArea = new javax.swing.JPanel();
previewPanel1 = new javax.swing.JPanel();
predictionValueUpload = new javax.swing.JLabel();
predictUpload = new javax.swing.JButton();
chooseFile = new javax.swing.JButton();
fileLocationTextbox = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
accuracyValueUpload = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tabMenu.setName("tabMenu"); // NOI18N
trainModel.setText("Train Model");
trainModel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
trainModelActionPerformed(evt);
}
});
statusLabel.setText("Ready");
accuracyLabel.setText("Accuracy: ");
accuracyValueModel.setText("0");
testModel.setText("Test Model");
testModel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
testModelActionPerformed(evt);
}
});
statusLabel1.setText("Ready");
javax.swing.GroupLayout modelPanelLayout = new javax.swing.GroupLayout(modelPanel);
modelPanel.setLayout(modelPanelLayout);
modelPanelLayout.setHorizontalGroup(
modelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(modelPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(modelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(trainModel)
.addGroup(modelPanelLayout.createSequentialGroup()
.addComponent(accuracyLabel)
.addGap(18, 18, 18)
.addComponent(accuracyValueModel))
.addComponent(testModel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 86, Short.MAX_VALUE)
.addGroup(modelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(modelPanelLayout.createSequentialGroup()
.addComponent(statusLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(trainLoadingBar, javax.swing.GroupLayout.PREFERRED_SIZE, 383, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(modelPanelLayout.createSequentialGroup()
.addComponent(statusLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(trainLoadingBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 383, javax.swing.GroupLayout.PREFERRED_SIZE))))
);
modelPanelLayout.setVerticalGroup(
modelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, modelPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(modelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(accuracyLabel)
.addComponent(accuracyValueModel))
.addGap(236, 236, 236)
.addGroup(modelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(testModel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(statusLabel1)
.addComponent(trainLoadingBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(modelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(modelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(trainModel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(statusLabel))
.addComponent(trainLoadingBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
trainModel.getAccessibleContext().setAccessibleName("");
tabMenu.addTab("Model", modelPanel);
jPanel1.setBackground(new java.awt.Color(204, 204, 204));
previewPanel.setBackground(new java.awt.Color(204, 204, 204));
predictionValueDraw.setFont(new java.awt.Font("Tahoma", 0, 72)); // NOI18N
predictionValueDraw.setText("0");
javax.swing.GroupLayout previewPanelLayout = new javax.swing.GroupLayout(previewPanel);
previewPanel.setLayout(previewPanelLayout);
previewPanelLayout.setHorizontalGroup(
previewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(previewPanelLayout.createSequentialGroup()
.addGap(118, 118, 118)
.addComponent(predictionValueDraw)
.addContainerGap(123, Short.MAX_VALUE))
);
previewPanelLayout.setVerticalGroup(
previewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(previewPanelLayout.createSequentialGroup()
.addGap(87, 87, 87)
.addComponent(predictionValueDraw)
.addContainerGap(106, Short.MAX_VALUE))
);
jPanel1.add(previewPanel);
predictPaint.setText("Predict");
predictPaint.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
predictPaint.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
predictPaintActionPerformed(evt);
}
});
jPanel1.add(predictPaint);
clearPaint.setText("Clear");
clearPaint.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
clearPaint.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
clearPaint.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearPaintActionPerformed(evt);
}
});
jPanel1.add(clearPaint);
javax.swing.GroupLayout paintHolderLayout = new javax.swing.GroupLayout(paintHolder);
paintHolder.setLayout(paintHolderLayout);
paintHolderLayout.setHorizontalGroup(
paintHolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 306, Short.MAX_VALUE)
);
paintHolderLayout.setVerticalGroup(
paintHolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 285, Short.MAX_VALUE)
);
jLabel1.setText("Accuracy: ");
accuracyValueDraw.setText("0");
javax.swing.GroupLayout drawDigitPanelLayout = new javax.swing.GroupLayout(drawDigitPanel);
drawDigitPanel.setLayout(drawDigitPanelLayout);
drawDigitPanelLayout.setHorizontalGroup(
drawDigitPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(drawDigitPanelLayout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(drawDigitPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(paintHolder, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(drawDigitPanelLayout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(accuracyValueDraw)
.addGap(0, 0, Short.MAX_VALUE))))
);
drawDigitPanelLayout.setVerticalGroup(
drawDigitPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
.addGroup(drawDigitPanelLayout.createSequentialGroup()
.addComponent(paintHolder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(drawDigitPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(accuracyValueDraw))
.addGap(0, 0, Short.MAX_VALUE))
);
tabMenu.addTab("Draw Digit", drawDigitPanel);
predictionArea.setBackground(new java.awt.Color(204, 204, 204));
previewPanel1.setBackground(new java.awt.Color(204, 204, 204));
predictionValueUpload.setFont(new java.awt.Font("Tahoma", 0, 72)); // NOI18N
predictionValueUpload.setText("0");
javax.swing.GroupLayout previewPanel1Layout = new javax.swing.GroupLayout(previewPanel1);
previewPanel1.setLayout(previewPanel1Layout);
previewPanel1Layout.setHorizontalGroup(
previewPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(previewPanel1Layout.createSequentialGroup()
.addGap(118, 118, 118)
.addComponent(predictionValueUpload)
.addContainerGap(123, Short.MAX_VALUE))
);
previewPanel1Layout.setVerticalGroup(
previewPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(previewPanel1Layout.createSequentialGroup()
.addGap(87, 87, 87)
.addComponent(predictionValueUpload)
.addContainerGap(106, Short.MAX_VALUE))
);
predictUpload.setText("Predict");
predictUpload.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
predictUpload.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
predictUploadActionPerformed(evt);
}
});
javax.swing.GroupLayout predictionAreaLayout = new javax.swing.GroupLayout(predictionArea);
predictionArea.setLayout(predictionAreaLayout);
predictionAreaLayout.setHorizontalGroup(
predictionAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(predictionAreaLayout.createSequentialGroup()
.addGap(7, 7, 7)
.addComponent(previewPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(predictionAreaLayout.createSequentialGroup()
.addGap(84, 84, 84)
.addComponent(predictUpload))
);
predictionAreaLayout.setVerticalGroup(
predictionAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(predictionAreaLayout.createSequentialGroup()
.addGap(5, 5, 5)
.addComponent(previewPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(5, 5, 5)
.addComponent(predictUpload))
);
chooseFile.setText("Choose File");
chooseFile.setActionCommand("chooseFile");
chooseFile.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
chooseFileActionPerformed(evt);
}
});
fileLocationTextbox.setText("C:\\");
jLabel2.setText("Accuracy: ");
accuracyValueUpload.setText("0");
javax.swing.GroupLayout uploadDigitPanelLayout = new javax.swing.GroupLayout(uploadDigitPanel);
uploadDigitPanel.setLayout(uploadDigitPanelLayout);
uploadDigitPanelLayout.setHorizontalGroup(
uploadDigitPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(uploadDigitPanelLayout.createSequentialGroup()
.addComponent(predictionArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(uploadDigitPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(uploadDigitPanelLayout.createSequentialGroup()
.addComponent(fileLocationTextbox)
.addContainerGap())
.addGroup(uploadDigitPanelLayout.createSequentialGroup()
.addGroup(uploadDigitPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(chooseFile)
.addGroup(uploadDigitPanelLayout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(accuracyValueUpload)))
.addGap(0, 215, Short.MAX_VALUE))))
);
uploadDigitPanelLayout.setVerticalGroup(
uploadDigitPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(predictionArea, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(uploadDigitPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(fileLocationTextbox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(chooseFile)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(uploadDigitPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(accuracyValueUpload))
.addContainerGap())
);
tabMenu.addTab("Upload Digit", uploadDigitPanel);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tabMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 612, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tabMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* Train model button
* initialises the training dataset and performs training to create classification
* Sets the accuracy label in GUI
*/
private void trainModelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_trainModelActionPerformed
// initialize dataset
int[] labels = readerMNIST.getLabels(TRAIN_LABEL_FILE);
List<int[]> images2 = readerMNIST.getImages2(TRAIN_IMAGE_FILE);
primary = new Dataset(images2, labels);
primary.trainModel();
trainLoadingBar.setValue(100);
accuracyValueModel.setText(Double.toString(primary.getAccuracy()));
}//GEN-LAST:event_trainModelActionPerformed
private void clearPaintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearPaintActionPerformed
paint.clear();
}//GEN-LAST:event_clearPaintActionPerformed
/**
* Predict paint button
* using the training classification it attempts to predict the drawn digit
* using the image from DrawArea
*/
private void predictPaintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_predictPaintActionPerformed
saveImg();
primary.predict(convertImgToArray(saveImg()));
predictionValueDraw.setText(Integer.toString(primary.getPredictionValue()));
accuracyValueDraw.setText(Double.toString(primary.getAccuracy()));
}//GEN-LAST:event_predictPaintActionPerformed
/**
* Predict upload button
* using the training classification it attempts to predict the drawn digit
* using the uploaded image
*/
private void predictUploadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_predictUploadActionPerformed
try
{
BufferedImage image = ImageIO.read(new File(fileLocationTextbox.getText()));
// grayscale image and resize to 28x28
image = grayscaleImg(resizeImg(image));
// predict image digit
primary.predict(convertImgToArray(image));
predictionValueUpload.setText(Integer.toString(primary.getPredictionValue()));
accuracyValueUpload.setText(Double.toString(primary.getAccuracy()));
}
catch(IOException e)
{
System.out.println("Error: "+e);
}
}//GEN-LAST:event_predictUploadActionPerformed
/**
* Creates a new file chooser for the user to find and upload an image
* saves the file location to a textbox
*/
private void chooseFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseFileActionPerformed
JFileChooser fileChooser = new JFileChooser();
// Set current directory
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
// Show up dialog
int result = fileChooser.showOpenDialog(fileChooser);
if (result == JFileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
fileLocationTextbox.setText(file.toString());
}
}//GEN-LAST:event_chooseFileActionPerformed
/**
* Test training model button
* This button performs a test on a dataset of 10000 images
* Using the classification from the training model
*/
private void testModelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testModelActionPerformed
primary.addImagesListTest(readerMNIST.getImages2(TEST_IMAGE_FILE));
primary.addLabelsListTest(readerMNIST.getLabels(TEST_LABEL_FILE));
primary.testModel();
}//GEN-LAST:event_testModelActionPerformed
/**
* Resizes a given image to a size of 28x28
* @param image
* @return outputImage
*/
public BufferedImage resizeImg(BufferedImage image){
// creates output image
BufferedImage outputImage = new BufferedImage(28,
28, image.TYPE_INT_ARGB);
// scales the input image to the output image
Graphics2D g2d = outputImage.createGraphics();
g2d.drawImage(image, 0, 0, 28, 28, null);
g2d.dispose();
return outputImage;
}
/**
* Turns a given image into a grayscale version of itself
* @param image
* @return outputImage
*/
public BufferedImage grayscaleImg(BufferedImage image){
BufferedImage outputImage = image;
// convert image to grayscale
for (int x = 0; x < outputImage.getWidth(); ++x){
for (int y = 0; y < outputImage.getHeight(); ++y)
{
int rgb = outputImage.getRGB(x, y);
int r = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int b = (rgb & 0xFF);
int grayscale = (int) ((0.3*r) + (0.59*green) + (0.11 * b));
int newPixel = 0xFF000000 | (grayscale << 16 ) |
(grayscale << 8 ) |
(grayscale);
outputImage.setRGB(x, y, newPixel);
}
}
return outputImage;
}
/**
* Converts a given image to a 2 dimensional array of integers
* @param image
* @return imgArr
*/
public int[][] convertImgToArray(BufferedImage image){
BufferedImage img = image;
int width = img.getWidth();
int height = img.getHeight();
int[][] imgArr = new int[width][height];
Raster raster = img.getData();
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
imgArr[i][j] = raster.getSample(i, j, 0);
}
}
return imgArr;
}
/**
* Grabs the image from the DrawArea and performs processing on the image
* converts image to grayscale and resizes to 28x28
* @return outputImage
*/
public BufferedImage saveImg(){
// grab image from DrawArea
BufferedImage image = new BufferedImage(paint.getWidth(), paint.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
paint.paint(g);
g.dispose();
// resize and grayscale the image
BufferedImage outputImage = (grayscaleImg(resizeImg(image)));
return outputImage;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel accuracyLabel;
private javax.swing.JLabel accuracyValueDraw;
private javax.swing.JLabel accuracyValueModel;
private javax.swing.JLabel accuracyValueUpload;
private javax.swing.JButton chooseFile;
private javax.swing.JButton clearPaint;
private javax.swing.JPanel drawDigitPanel;
private javax.swing.JTextField fileLocationTextbox;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel modelPanel;
private javax.swing.JPanel paintHolder;
private javax.swing.JButton predictPaint;
private javax.swing.JButton predictUpload;
private javax.swing.JPanel predictionArea;
private javax.swing.JLabel predictionValueDraw;
private javax.swing.JLabel predictionValueUpload;
private javax.swing.JPanel previewPanel;
private javax.swing.JPanel previewPanel1;
private javax.swing.JLabel statusLabel;
private javax.swing.JLabel statusLabel1;
private javax.swing.JTabbedPane tabMenu;
private javax.swing.JButton testModel;
private javax.swing.JProgressBar trainLoadingBar;
private javax.swing.JProgressBar trainLoadingBar1;
private javax.swing.JButton trainModel;
private javax.swing.JPanel uploadDigitPanel;
// End of variables declaration//GEN-END:variables
}