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 package org.openimaj.ml.neuralnet;
31
32 import java.awt.event.KeyEvent;
33 import java.awt.event.KeyListener;
34 import java.util.Random;
35
36 import javax.swing.JFrame;
37
38 import org.encog.ml.data.MLDataPair;
39 import org.encog.ml.data.MLDataSet;
40 import org.openimaj.image.DisplayUtilities;
41 import org.openimaj.image.FImage;
42 import org.openimaj.image.MBFImage;
43 import org.openimaj.image.colour.RGBColour;
44 import org.openimaj.image.processing.resize.ResizeProcessor;
45 import org.openimaj.image.typography.hershey.HersheyFont;
46
47 class HandWritingInputDisplay implements KeyListener{
48
49 private double[][] imageValues;
50 private int currentImageIndex;
51 private FImage currentImage;
52 private ResizeProcessor rp;
53 private int[] numberValues;
54
55 public HandWritingInputDisplay(double[][] xVals, int[] yVals) {
56 this.imageValues = xVals;
57 this.numberValues = yVals;
58 this.currentImageIndex = 0;
59
60 rp = new ResizeProcessor(200, 200);
61 JFrame frame = DisplayUtilities.displayName(this.getCurrentImage(), "numbers");
62 frame.addKeyListener(this);
63 }
64
65 public HandWritingInputDisplay(MLDataSet training) {
66 this.imageValues = new double[(int) training.getRecordCount()][];
67 this.numberValues = new int[(int) training.getRecordCount()];
68
69 int index = 0;
70 for (MLDataPair mlDataPair : training) {
71 this.imageValues[index] = mlDataPair.getInputArray();
72 int yIndex = 0;
73 while(mlDataPair.getIdealArray()[yIndex]!=1)yIndex++;
74 this.numberValues[index] = (yIndex + 1) % 10;
75 index++;
76 }
77
78 this.currentImageIndex = 0;
79 rp = new ResizeProcessor(200, 200);
80 JFrame frame = DisplayUtilities.displayName(this.getCurrentImage(), "numbers");
81 frame.addKeyListener(this);
82 }
83
84 private MBFImage getCurrentImage() {
85 if(imageValues.length<1)return null;
86 double[] imageDoubles = imageValues[this.currentImageIndex];
87 int wh = (int) Math.sqrt(imageDoubles.length);
88 int i = 0;
89 if(this.currentImage == null)
90 this.currentImage = new FImage(wh,wh);
91 for (int x = 0; x < wh; x++) {
92 for (int y = 0; y < wh; y++) {
93 this.currentImage.pixels[y][x] = (float) imageDoubles[i++];
94 }
95 }
96 MBFImage toDraw = this.currentImage.normalise().process(rp).toRGB();
97 toDraw.drawText("Guess: " + this.numberValues[this.currentImageIndex],10, 30, HersheyFont.ASTROLOGY , 20, RGBColour.RED);
98 return toDraw;
99 }
100
101 @Override
102 public void keyTyped(KeyEvent e) {
103 if(e.getKeyChar() == 'e'){
104 this.currentImageIndex = new Random().nextInt(this.imageValues.length);
105 }
106 else if(e.getKeyChar() == 'q'){
107 this.currentImageIndex = this.currentImageIndex > 0 ? this.currentImageIndex - 1: 0;
108 }
109 else if(e.getKeyChar() == 'w'){
110 this.currentImageIndex = this.currentImageIndex < this.imageValues.length - 1 ? this.currentImageIndex + 1 : this.imageValues.length - 1;
111 }
112 DisplayUtilities.displayName(this.getCurrentImage(), "numbers");
113 }
114
115 @Override
116 public void keyPressed(KeyEvent e) {
117
118 }
119
120 @Override
121 public void keyReleased(KeyEvent e) {
122
123
124 }
125
126 }