-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScoreMaker.java
529 lines (437 loc) · 14.9 KB
/
ScoreMaker.java
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
/**
* @author Derek Babb <[email protected]>
* @version 1.0
* @since 7-17-2013
*
* ScoreMaker is an application to test an Optical Character Recognition algorithm
* specifically for an OCR reading a scoreboard. OCRs should implement the DigitReader interface.
*
* ScoreMaker has two modes:
* 1) Lights Mode: Allows the user to individually turn off and on "Lights" in an image.
* This mode will allow a programmer to test ideal cases of each digit, also has a feature
* to modify one or two lights to see what cases begin to fail.
*
* 2) Image Selector Mode: Allows the user to open an existing scoreboard image and select
* an area in which a digit is contained. That sub-image is fed to the OCR and through this you can
* program a real-world tester based on either actual images of your scoreboard or other images from
* the internet and other sources.
*
* To load your OCRs to test, simply define them in the construcor
*
*/
import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.*;
import java.net.*;
import java.util.*;
public class ScoreMaker extends JPanel implements ActionListener, MouseListener, MouseMotionListener
{
//Adjust this number depending on how many OCRs you want to load.
/**********************************************************************/
DigitReader[] reader = new DigitReader[2];
/**********************************************************************/
private JButton[] lights = new JButton[54];
private static final int NUM_ROWS = 9;
private static final int NUM_COLS = 6;
private JFrame jf;
private Point[] point = new Point[2];
Color lightColor = Color.RED;
Color backColor = Color.BLACK;
PictureEdit pic, actualImage;
int selectedOCR = 0;
JMenu selectOCR;
ButtonGroup group;
public static void main(String[] args)
{
ScoreMaker sm = new ScoreMaker();
sm.setupLightEditor();
}
public ScoreMaker()
{
//setup the OCRs you want to be able to test
/**********************************************************************/
reader[0] = new ExampleOCR();
reader[1] = new TerribleOCR();
/**********************************************************************/
for(int i = 0; i < lights.length; i++)
{
lights[i] = new JButton();
lights[i].setBackground(Color.BLACK);
}
point[0] = new Point(0,0);
point[1] = new Point(1,1);
selectOCR= new JMenu("Set OCR");
JRadioButtonMenuItem[] ocrs = new JRadioButtonMenuItem[reader.length];
group = new ButtonGroup();
for(int i = 0; i < reader.length; i++)
{
ocrs[i] = new JRadioButtonMenuItem(reader[i].getName());
group.add(ocrs[i]);
selectOCR.add(ocrs[i]);
ocrs[i].addActionListener(this);
}
ocrs[selectedOCR].setSelected(true);
}
public int getSelectedOCR()
{
int i = 0;
for (Enumeration<AbstractButton> buttons = group.getElements(); buttons.hasMoreElements();i++) {
AbstractButton button = buttons.nextElement();
if (button.isSelected()) {
return i;
}
}
return 0;
}
public void setupImageSelector()
{
jf = new JFrame("Image Selector");
Container cp = jf.getContentPane();
actualImage = new PictureEdit();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(actualImage.getWidth() + 50 ,actualImage.getHeight() + 100);
jf.setLocation(100,100);
jf.setLayout(new GridLayout(1,1));
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem saveImage = new JMenuItem("Save Area");
JMenuItem switchTo = new JMenuItem("Switch to Light Editor");
JMenuItem exit = new JMenuItem("Exit");
JMenu imageMenu = new JMenu("Image");
JMenuItem testImage = new JMenuItem("Test Area");
imageMenu.add(testImage);
testImage.addActionListener(this);
fileMenu.add(saveImage);
fileMenu.add(selectOCR);
fileMenu.addSeparator();
fileMenu.add(switchTo);
fileMenu.addSeparator();
fileMenu.add(exit);
saveImage.addActionListener(this);
selectOCR.addActionListener(this);
switchTo.addActionListener(this);
exit.addActionListener(this);
menuBar.add(fileMenu);
menuBar.add(imageMenu);
jf.setJMenuBar(menuBar);
cp.add(this);
cp.addMouseListener(this);
cp.addMouseMotionListener(this);
jf.setVisible(true);
}
public void setupLightEditor()
{
jf = new JFrame("Lights Editor");
Container cp = jf.getContentPane();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(300,400);
jf.setLocation(100,100);
jf.setLayout(new GridLayout(NUM_ROWS, NUM_COLS));
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem saveImage = new JMenuItem("Save Image");
JMenuItem switchTo = new JMenuItem("Switch to Image Select");
JMenuItem exit = new JMenuItem("Exit");
JMenu imageMenu = new JMenu("Image");
JMenuItem clearImage = new JMenuItem("Clear Image");
JMenuItem testImage = new JMenuItem("Test Image");
imageMenu.add(clearImage);
imageMenu.add(testImage);
clearImage.addActionListener(this);
testImage.addActionListener(this);
fileMenu.add(saveImage);
fileMenu.add(selectOCR);
fileMenu.addSeparator();
fileMenu.add(switchTo);
fileMenu.addSeparator();
fileMenu.add(exit);
selectOCR.addActionListener(this);
switchTo.addActionListener(this);
saveImage.addActionListener(this);
exit.addActionListener(this);
menuBar.add(fileMenu);
menuBar.add(imageMenu);
JMenu testMenu = new JMenu("Test");
JMenuItem oneWrong = new JMenuItem("One Wrong");
JMenuItem twoWrong = new JMenuItem("Two Wrong");
testMenu.add(oneWrong);
testMenu.add(twoWrong);
menuBar.add(testMenu);
oneWrong.addActionListener(this);
twoWrong.addActionListener(this);
jf.setJMenuBar(menuBar);
for(int i = 0; i < lights.length; i++)
{
lights[i] = new JButton();
lights[i].setBackground(Color.BLACK);
cp.add(lights[i]);
lights[i].addActionListener(this);
}
jf.setVisible(true);
}
public void setOCR()
{
JFileChooser chooser = new JFileChooser(".");
FileNameExtensionFilter filter = new FileNameExtensionFilter("CLASS files", "class");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
File classFile = null;
if(returnVal == JFileChooser.APPROVE_OPTION) {
try{
classFile = chooser.getSelectedFile();
String className = classFile.getName().substring(0,classFile.getName().indexOf("."));
String fileURL = classFile.toURI().toURL().toString();
fileURL = fileURL.substring(0, fileURL.lastIndexOf("/")+1);
URL testURL = new URL(fileURL);
URLClassLoader classLoader = new URLClassLoader(new URL[]{testURL});
System.out.println();
Class<?> clazz = classLoader.loadClass(className);
Class<? extends DigitReader> sub = clazz.asSubclass(DigitReader.class);
Object instance = clazz.newInstance();
System.out.println(instance);
//DigitReader instance = (DigitReader)clazz.newInstance();
//System.out.println(instance.getDigit());
// reader = (DigitReader)instance;
// Method method = clazz.getDeclaredMethod(methodName, String.class);
//method.setAccessible(true);
//method.invoke(instance, toSet);
}
catch(Exception e){e.printStackTrace(); }
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(actualImage.getImage(), 0, 0, actualImage.getWidth(), actualImage.getHeight(), this);
g.setColor(new Color(0, 0, 255, 100));
int x1 = (int)point[0].getX();
int y1 = (int)point[0].getY();
int w = (int)point[1].getX() - x1;
int h = (int)point[1].getY() - y1;
g.fillRect(x1, y1, w, h);
}
public void createImage()
{
pic = new PictureEdit("images/blank.png");
for(int y = 0; y < pic.getHeight(); y++)
for(int x = 0; x < pic.getWidth(); x++)
pic.setColor(x,y, backColor);
//lights will be square for this test.
//gap between the lights will be 1/4 the size of the lights.
//first, compute the size of the bulb given the size of the canvas.
//don't want to overflow in either direction so take the min of the two options
int bulbWidth = pic.getWidth() / (NUM_COLS);
int bulbHeight = pic.getHeight() / (NUM_ROWS);
int bulbSize = Math.min(bulbWidth, bulbHeight);
int gap = bulbSize / 4;
int bulbCount = 0;
for(int y = 0; y < NUM_ROWS; y++)
{
for(int x = 0; x < NUM_COLS; x++)
{
if(lights[bulbCount].getBackground().equals(lightColor))
fillBulb(pic, x * (bulbSize + gap) ,y* (bulbSize + gap),bulbSize);
bulbCount++;
}
}
//pic.displayImage();
}
public void fillBulb(PictureEdit pic, int xStart, int yStart, int bulbSize)
{
for(int y = 0; y < bulbSize; y++)
{
for(int x = 0; x < bulbSize; x++)
{
pic.setColor(xStart + x, yStart + y,lightColor);
}
}
}
public int[] switchOne()
{
int expected = Integer.parseInt(JOptionPane.showInputDialog("Enter the expected number: "));
int wrongCount = 0;
int totalCount = 0;
for(int i = 0; i < lights.length; i++)
{
//Switch one light
if(lights[i].getBackground().equals(Color.BLACK))
lights[i].setBackground(Color.RED);
else
lights[i].setBackground(Color.BLACK);
createImage();
//reader = new DigitRead();
reader[selectedOCR].setImage(pic);
int numRead = reader[selectedOCR].getDigit();
if(numRead != expected)
{
wrongCount++;
pic.saveAs("wrongImages\\"+expected +"wrong" + wrongCount +"_" + numRead +".png");
}
totalCount++;
//reader.displayScore();
//Switch the light back to the way it was.
if(lights[i].getBackground().equals(Color.BLACK))
lights[i].setBackground(Color.RED);
else
lights[i].setBackground(Color.BLACK);
}
int[] result = {totalCount, wrongCount};
return result;
}
public int[] switchTwo()
{
int expected = Integer.parseInt(JOptionPane.showInputDialog("Enter the expected number: "));
int wrongCount = 0;
int totalCount = 0;
for(int i = 0; i < lights.length; i++)
{
//Switch one light
if(lights[i].getBackground().equals(Color.BLACK))
lights[i].setBackground(Color.RED);
else
lights[i].setBackground(Color.BLACK);
for(int j = i+1; j < lights.length; j++)
{
//Switch one light
if(lights[j].getBackground().equals(Color.BLACK))
lights[j].setBackground(Color.RED);
else
lights[j].setBackground(Color.BLACK);
createImage();
//DigitRead reader = new DigitRead();
reader[selectedOCR].setImage(pic);
int numRead = reader[selectedOCR].getDigit();
if(numRead != expected)
{
wrongCount++;
pic.saveAs("wrongImages\\"+expected +"wrong" + wrongCount +"_" + numRead +".png");
}
totalCount++;
//Switch the light back to the way it was.
if(lights[j].getBackground().equals(Color.BLACK))
lights[j].setBackground(Color.RED);
else
lights[j].setBackground(Color.BLACK);
}
//Switch the light back to the way it was.
if(lights[i].getBackground().equals(Color.BLACK))
lights[i].setBackground(Color.RED);
else
lights[i].setBackground(Color.BLACK);
}
int[] result = {totalCount, wrongCount};
return result;
}
public void createAreaImage()
{
int x1 = (int)point[0].getX();
int y1 = (int)point[0].getY();
int w = (int)point[1].getX() - x1;
int h = (int)point[1].getY() - y1;
pic = new PictureEdit(w,h);
for(int x = 0; x < w; x++)
{
for(int y = 0; y < h; y++)
{
pic.setColor(x,y,actualImage.getColor(x1+x, y1+y));
}
}
pic.displayImage();
}
public void actionPerformed(ActionEvent e)
{
for(int i = 0; i < lights.length; i++)
{
if(e.getSource() == lights[i])
{
if(lights[i].getBackground().equals(Color.BLACK))
lights[i].setBackground(Color.RED);
else
lights[i].setBackground(Color.BLACK);
}
}
if(e.getActionCommand().equals("Exit"))
System.exit(0);
if(e.getActionCommand().equals("Save Image"))
{
createImage();
pic.saveAs();
}
if(e.getActionCommand().equals("Clear Image"))
{
for(int i = 0; i < lights.length; i++)
lights[i].setBackground(Color.BLACK);
}
if(e.getActionCommand().equals("Test Image"))
{
createImage();
//DigitRead reader = new DigitRead();
reader[selectedOCR].setImage(pic);
int numRead = reader[selectedOCR].getDigit();
JOptionPane.showMessageDialog(null, numRead+"", "Result", JOptionPane.INFORMATION_MESSAGE);
}
if(e.getActionCommand().equals("Test Area"))
{
createAreaImage();
//DigitRead reader = new DigitRead();
reader[selectedOCR].setImage(pic);
int numRead = reader[selectedOCR].getDigit();
JOptionPane.showMessageDialog(null, numRead+"", "Result", JOptionPane.INFORMATION_MESSAGE);
}
if(e.getActionCommand().equals("Save Area"))
{
createAreaImage();
pic.saveAs();
}
if(e.getActionCommand().equals("One Wrong"))
{
int[] result = switchOne();
JOptionPane.showMessageDialog(null, "Correct: " + (result[0] - result[1]) + "\n" + "Wrong: " + result[1], "Results", JOptionPane.INFORMATION_MESSAGE);
}
if(e.getActionCommand().equals("Two Wrong"))
{
int[] result = switchTwo();
JOptionPane.showMessageDialog(null, "Correct: " + (result[0] - result[1]) + "\n" + "Wrong: " + result[1], "Results", JOptionPane.INFORMATION_MESSAGE);
}
if(e.getActionCommand().equals("Switch to Image Select"))
{
jf.dispose();
setupImageSelector();
}
if(e.getActionCommand().equals("Switch to Light Editor"))
{
jf.dispose();
setupLightEditor();
}
selectedOCR = getSelectedOCR();
}
public void mousePressed(MouseEvent e)
{
point[0] = e.getPoint();
repaint();
}
public void mouseReleased(MouseEvent e)
{
point[1] = e.getPoint();
repaint();
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseMoved(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
point[1] = e.getPoint();
repaint();
}
}