-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReader.java
executable file
·225 lines (184 loc) · 3.92 KB
/
Reader.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
/**
* Sachin Shah
* version 1
*/
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FilenameFilter;
import java.io.FileReader;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.ArrayList;
public class Reader
{
public File dir;
// array of supported extensions
public String[] EXTENSIONS = new String[]{
"gif", "png", "bmp", "jpeg", "jpg"
};
public FilenameFilter IMAGE_FILTER = new FilenameFilter() {
@Override
public boolean accept(File dir, String name)
{
for (String ext : EXTENSIONS)
{
if (name.endsWith("." + ext))
{
return true;
}
}
return false;
}
};
// Create reader with default folder
public Reader()
{
this.dir = new File("dataDense");
}
public Reader(String folder)
{
this.dir = new File(folder);
}
public void changeFolder(String name)
{
File temp = new File(name);
if (temp.isDirectory())
{
dir = temp;
}
else
{
System.err.println(name + " is not a directory.");
}
}
// Get all image files in directory
public ArrayList<String> fileNames()
{
ArrayList<String> list = new ArrayList<String>();
if (dir.isDirectory())
{
for (File f : dir.listFiles(IMAGE_FILTER))
{
list.add(f.getName());
}
}
return list;
}
// Load image files as BufferedImages
public ArrayList<BufferedImage> loadBufferedImages()
{
System.out.println("Loading Data...");
ArrayList<BufferedImage> list = new ArrayList<BufferedImage>();
if (dir.isDirectory())
{
for (File f : dir.listFiles(IMAGE_FILTER))
{
try
{
BufferedImage img = ImageIO.read(f);
list.add(img);
System.out.println("Loaded File: " + f.getName());
System.out.println(" width : " + img.getWidth());
System.out.println(" height: " + img.getHeight());
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return list;
}
// Convert an image into a 2D array of RGB
public int[][] imgToArray(BufferedImage img)
{
int c, r, g, b;
int place = 0;
int[][] output = new int[3][img.getHeight() * img.getWidth()];
for (int y = 0; y < img.getHeight(); y++)
{
for (int x = 0; x < img.getWidth(); x++)
{
c = img.getRGB(x, y);
r = getRed(c);
g = getGreen(c);
b = getBlue(c);
output[0][place] = r;
output[1][place] = g;
output[2][place] = b;
place++;
}
}
return output;
}
private TrainingPoint[] labelData(int one, int two, int neither)
{
System.out.printf("Labeling data with: <%d, %d, %d>%n", one, two, neither);
ArrayList<BufferedImage> imgs = loadBufferedImages();
ArrayList<String> names = fileNames();
TrainingPoint[] output = new TrainingPoint[imgsLength(imgs)];
int counter = 0;
int place = 0;
int l;
for (BufferedImage s : imgs)
{
int[][] rgb = imgToArray(s);
if (names.get(counter).toLowerCase().contains(Constants.label1))
{
l = one;
}
else if (names.get(counter).toLowerCase().contains(Constants.label2))
{
l = two;
}
else
{
l = neither;
}
for (int i = 0; i < rgb[0].length; i++)
{
Vector v = new Vector(rgb[0][i], rgb[1][i], rgb[2][i]);
TrainingPoint pt = new TrainingPoint(l, v);
output[place] = pt;
place++;
}
counter++;
}
return output;
}
public TrainingPoint[] labeledNeither()
{
return labelData(1, 1, -1);
}
public TrainingPoint[] labeledOne()
{
return labelData(1, -1, -1);
}
public TrainingPoint[] labeledTwo()
{
return labelData(-1, 1, -1);
}
public int imgsLength(ArrayList<BufferedImage> imgs)
{
int length = 0;
for (BufferedImage img : imgs)
{
int size = img.getHeight() * img.getWidth();
length += size;
}
return length;
}
public int getRed(int colorInt)
{
return (colorInt >> 16) & 0xff;
}
public int getGreen(int colorInt)
{
return (colorInt >> 8) & 0xff;
}
public int getBlue(int colorInt)
{
return (colorInt ) & 0xff;
}
}