View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 //		keyTyped(e);
118 	}
119 
120 	@Override
121 	public void keyReleased(KeyEvent e) {
122 		// TODO Auto-generated method stub
123 		
124 	}
125 	
126 }