This repository has been archived by the owner on Mar 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataHandler.java
159 lines (138 loc) · 3.54 KB
/
DataHandler.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
package com;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.io.File;
import java.util.Collections;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.BlockingQueue;
import java.util.function.Consumer;
public class DataHandler{
public static double[][] X;
public static double[][] y;
public static int max;
DataHandler(int max){
this.max = max;
}
DataHandler(){
this.max = -1;
}
/**
* Reads an image.
*
* @param file The file
*
* @return single row with 49 bits representing the image
*/
public static double[] loadImage(File file) {
try {
BufferedImage image = ImageIO.read(file);
if (image.getWidth() == 7 && image.getHeight() == 7) {
double[] flattenPixels = new double[image.getWidth() * image.getHeight()];
int index = 0;
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int colour = image.getRGB(x, y);
// r,g,b will share the same value because of our color chosie.
int red = (colour >> 16) & 0xff;
if(125 < red){
flattenPixels[index++] = 1;
}else{
flattenPixels[index++] = 0;
}
}
}
return flattenPixels;
}else{
throw new IllegalArgumentException("image has to be 7x7");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Search for a file pattern in a folder
*
* @param pattern The pattern
* @param folder The folder
* @param result The result
*/
public static void searchFiles(final String pattern, final File folder, List<String> result) {
int count = 0;
for (final File file : folder.listFiles()) {
if (file.isFile()) {
if (file.getName().matches(pattern)) {
result.add(file.getAbsolutePath());
}
}
if(count == max){
break;
}
count++;
}
}
/**
* Parse the dataset
*
* @param folderPath The folder path with the data
*/
public static void makeDataSet(String folderPath){
final File folder = new File(folderPath);
if(folder.exists()) {
List<String> result = new ArrayList<>();
searchFiles(".*\\.jpg", folder, result);
X = new double[result.size()][7];
y = new double[result.size()][10];
int index = 0;
for (String s : result) {
int expectedNumber = s.replace(folderPath, "").charAt(0) - '0';
X[index] = loadImage(new File(s));
y[index][expectedNumber] = 1;
index++;
}
}else{
System.out.println("folder does not exsist!!!");
}
}
/**
* Return the feature for the dataset
*/
public static double[][] getX(){
return X;
}
/**
* Return the label for the dataset
*/
public static double[][] getY(){
return y;
}
}