-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWithUI.java
More file actions
176 lines (152 loc) · 6.21 KB
/
MainWithUI.java
File metadata and controls
176 lines (152 loc) · 6.21 KB
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
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class MainWithUI {
private static String selectedImagePath;
private static Sobel sb = new Sobel();
private static DecisionTree dt = new DecisionTree();
private static Map<String, Integer> attributeIndices = loadAttributeIndices();
private static TreeNode load = dt.loadTree("E:\\PROGRAMMING\\SPL-1\\res\\decision_tree.ser");
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGUI();
});
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Digit Eyes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel buttonPanel = new JPanel();
JButton selectImageButton = new JButton("Select Image");
JButton exitButton = new JButton("Exit");
selectImageButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectImage();
}
});
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
buttonPanel.add(selectImageButton);
buttonPanel.add(exitButton);
frame.getContentPane().add(buttonPanel, BorderLayout.NORTH);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void selectImage() {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
selectedImagePath = fileChooser.getSelectedFile().getAbsolutePath();
processSelectedImage();
}
}
private static void processSelectedImage() {
try {
File input = new File(selectedImagePath);
System.out.println("Selected Image Path: " + selectedImagePath);
BufferedImage image = ImageIO.read(input);
BufferedImage resized = sb.resizeImage(image, 28, 28);
BufferedImage grayScale = sb.convertToGrayscale(resized);
BufferedImage procImg = sb.sobelAlgo(grayScale);
int val = 150;
int mat[][] = sb.convertMatrix(procImg, val);
sb.saveToTXT(mat, "E:\\PROGRAMMING\\SPL-1\\res\\mat.txt");
File output = new File("E:\\PROGRAMMING\\SPL-1\\res\\maruf.jpg");
ImageIO.write(procImg, "jpg", output);
makeMatrix();
String[] test = loadTestData();
String predictionResult = dt.predict(test, load, attributeIndices);
// Show the output on a side of the window
JFrame outputFrame = new JFrame("Image Processing Output");
outputFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JTextArea outputTextArea = new JTextArea();
outputTextArea.setEditable(false);
outputTextArea.setText("The digit may be: " + predictionResult);
JScrollPane scrollPane = new JScrollPane(outputTextArea);
outputFrame.getContentPane().add(scrollPane, BorderLayout.CENTER);
outputFrame.setSize(300, 200);
outputFrame.setLocationRelativeTo(null);
outputFrame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Error processing the image.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private static void makeMatrix() {
String input = "E:\\PROGRAMMING\\SPL-1\\res\\mat.txt";
String output = "E:\\PROGRAMMING\\SPL-1\\res\\arr.txt";
try {
BufferedReader reader = new BufferedReader(new FileReader(input));
BufferedWriter writer = new BufferedWriter(new FileWriter(output));
String line;
StringBuilder concat = new StringBuilder();
while ((line = reader.readLine()) != null) {
concat.append(line).append(",");
}
if (concat.length() > 0) {
concat.deleteCharAt(concat.length() - 1);
}
writer.write(concat.toString());
reader.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static String[] loadTestData() {
String[] test = new String[784];
try (BufferedReader reader = new BufferedReader(new FileReader("E:\\PROGRAMMING\\SPL-1\\res\\arr.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
String[] value = line.split(",");
for (int j = 0; j < 784; j++) {
test[j] = value[j];
}
}
} catch (IOException e) {
e.printStackTrace();
}
return test;
}
private static Map<String, Integer> loadAttributeIndices() {
Map<String, Integer> attributeIndices = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader("E:\\PROGRAMMING\\SPL-1\\res\\attributes.txt"))) {
String line;
if ((line = reader.readLine()) != null) {
List<String> attributeList = Arrays.asList(line.split(","));
for (int i = 0; i < attributeList.size(); i++) {
attributeIndices.put(attributeList.get(i), i);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return attributeIndices;
}
}