-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialog1.java
476 lines (420 loc) · 20.8 KB
/
dialog1.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import ij.IJ;
import static ij.IJ.URL;
import static ij.IJ.selectWindow;
import ij.ImagePlus;
import ij.Prefs;
import ij.WindowManager;
import ij.plugin.frame.RoiManager;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import weka.classifiers.Classifier;
import weka.core.Instances;
import weka.core.Utils;
import weka.core.converters.ArffSaver;
import weka.core.converters.CSVLoader;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Add;
/**
*
* @author usuario
*/
public class dialog1 extends javax.swing.JFrame {
private String name3;
private Object txtarea;
private String image;
private ImagePlus imp;
void v(){
//MachinLearningInterface.liststring.getClass();
}
/**
* Creates new form NewJFrame1
*/
public dialog1() {
initComponents();
txtfilename.setVisible(false);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
txtfilename = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu2 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenu9 = new javax.swing.JMenu();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
jMenuItem5 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Would you like to classify your results with");
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/correct.png"))); // NOI18N
jButton1.setText("OK");
jButton1.setToolTipText("");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Untitled-4.png"))); // NOI18N
jButton2.setText("Cancel");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
txtfilename.setEditable(false);
txtfilename.setForeground(new java.awt.Color(232, 232, 232));
txtfilename.setText("jTextField1");
txtfilename.setBorder(null);
txtfilename.setDisabledTextColor(new java.awt.Color(232, 232, 232));
txtfilename.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtfilenameActionPerformed(evt);
}
});
jLabel2.setText("our machin learning model of classification?");
jMenu2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/options.png"))); // NOI18N
jMenu2.setText("Selection Tool");
jMenu2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jMenu2MouseClicked(evt);
}
});
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/options.png"))); // NOI18N
jMenuItem1.setText("Free Selection ");
jMenuItem1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jMenuItem1MouseClicked(evt);
}
});
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem1);
jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3, 0));
jMenuItem2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/options.png"))); // NOI18N
jMenuItem2.setText("Rectangular Selection ");
jMenuItem2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jMenuItem2MouseClicked(evt);
}
});
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem2);
jMenuBar1.add(jMenu2);
jMenu9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tools.png"))); // NOI18N
jMenu9.setText("Options");
jMenu9.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jMenu9MouseClicked(evt);
}
});
jMenu9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenu9ActionPerformed(evt);
}
});
jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, 0));
jMenuItem3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/trash.png"))); // NOI18N
jMenuItem3.setText("Clear Selection");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
jMenu9.add(jMenuItem3);
jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, 0));
jMenuItem4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/back.png"))); // NOI18N
jMenuItem4.setText("jMenuItem1");
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem4ActionPerformed(evt);
}
});
jMenu9.add(jMenuItem4);
jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F6, 0));
jMenuItem5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/all.png"))); // NOI18N
jMenuItem5.setText("Select All");
jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem5ActionPerformed(evt);
}
});
jMenu9.add(jMenuItem5);
jMenuBar1.add(jMenu9);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jButton2))
.addGroup(layout.createSequentialGroup()
.addGap(227, 227, 227)
.addComponent(txtfilename, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2)
.addComponent(jLabel1))
.addGap(20, 20, 20))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(txtfilename, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(2, 2, 2)
.addComponent(jLabel1)
.addGap(5, 5, 5)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2)))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
try {
CSVLoader loader = new CSVLoader();
loader.setSource(new File(txtfilename.getText()+"_complete.csv"));
Instances data = loader.getDataSet();
System.out.println(data);
// save ARFF
String arffile = this.name3 + ".arff";
System.out.println(arffile);
ArffSaver saver = new ArffSaver();
saver.setInstances(data);
saver.setFile(new File(arffile));
saver.writeBatch();
} catch (IOException ex) {
Logger.getLogger(MachinLearningInterface.class.getName()).log(Level.SEVERE, null, ex);
}
Instances data;
try {
data = new Instances(new BufferedReader(new FileReader(this.name3 + ".arff")));
Instances newData = null;
Add filter;
newData = new Instances(data);
filter = new Add();
filter.setAttributeIndex("last");
filter.setNominalLabels("rods,punctua,networks");
filter.setAttributeName("target");
filter.setInputFormat(newData);
newData = Filter.useFilter(newData, filter);
System.out.print(newData);
Vector vec = new Vector();
newData.setClassIndex(newData.numAttributes() - 1);
if (!newData.equalHeaders(newData)) {
throw new IllegalArgumentException("Train and test are not compatible!");
}
/*URL urlToModel = this.getClass().getResource("/" + "Final.model");
InputStream stream = urlToModel.openStream();*/
InputStream stream= this.getClass().getResourceAsStream("/" + "Final.model");
Classifier cls = (Classifier) weka.core.SerializationHelper.read(stream);
System.out.println("PROVANT MODEL.classifyInstance");
for (int i = 0; i < newData.numInstances(); i++) {
double pred = cls.classifyInstance(newData.instance(i));
double[] dist = cls.distributionForInstance(newData.instance(i));
System.out.print((i + 1) + " - ");
System.out.print(newData.classAttribute().value((int) pred) + " - ");
//txtarea2.setText(Utils.arrayToString(dist));
System.out.println(Utils.arrayToString(dist));
vec.add(newData.classAttribute().value((int) pred));
//txtarea2.append(Utils.arrayToString(newData.classAttribute().value((int) pred)));
//this.target2.add((i + 1) + " -);
//this.target.add(newData.classAttribute().value((int) pred));
//for (String s : this.list) {
//this.target2 += s + ",";
}
int p = 0, n = 0, r = 0;
//txtarea2.append(Utils.arrayToString(this.target));
for (Object vec1 : vec) {
if ("rods".equals(vec1.toString())) {
r = r + 1;
}
if ("punctua".equals(vec1.toString())) {
p = p + 1;
}
if ("networks".equals(vec1.toString())) {
n = n + 1;
}
PrintWriter out = null;
try {
out = new PrintWriter(this.name3 + "_morphology.txt");
out.println(vec);
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
//System.out.println(vec.get(i));
}
System.out.println("VECTOR-> punctua: " + p + ", rods: " + r + ", networks: " + n);
IJ.showMessage("Your file:" + this.name3 + "arff"
+ "\nhas been analysed, and it is composed by-> punctua: " + p + ", rods: " + r + ", networks: " + n);
//txtarea2.setText("Your file:" + this.name3 + ".arff"
//+ "\nhas been analysed, and it is composed by-> punctua: " + p + ", rods: " + r + ", networks: " + n
//+ "\n"
//+ "\nAnalyse complete");
//txtarea.setText("Analyse complete");
} catch (IOException ex) {
Logger.getLogger(MachinLearningInterface.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(MachinLearningInterface.class.getName()).log(Level.SEVERE, null, ex);
}
IJ.run("Clear Results");
IJ.run("Clear Results");
IJ.run("Close All", "");
if (WindowManager.getFrame("Results") != null) {
IJ.selectWindow("Results");
IJ.run("Close");
}
if (WindowManager.getFrame("Summary") != null) {
IJ.selectWindow("Summary");
IJ.run("Close");
}
if (WindowManager.getFrame("Results") != null) {
IJ.selectWindow("Results");
IJ.run("Close");
}
if (WindowManager.getFrame("ROI Manager") != null) {
IJ.selectWindow("ROI Manager");
IJ.run("Close");
}
IJ.run("Close All", "roiManager");
IJ.run("Close All", "");
setVisible(false);
dispose();// TODO add your handling code here:
setVisible(false);
dispose();// TODO add your handling code here:
// TODO add your handling code here:
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
setVisible(false);
dispose();// TODO add your handling code here: // TODO add your handling code here:
}//GEN-LAST:event_jButton2ActionPerformed
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
IJ.run(this.imp, "Select None", ""); // TODO add your handling code here:
}//GEN-LAST:event_jMenuItem3ActionPerformed
private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
IJ.run("Restore Selection", ""); // TODO add your handling code here:
}//GEN-LAST:event_jMenuItem4ActionPerformed
private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
IJ.run(imp, "Select All", ""); // TODO add your handling code here:
}//GEN-LAST:event_jMenuItem5ActionPerformed
private void jMenu9MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu9MouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_jMenu9MouseClicked
private void jMenu9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu9ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jMenu9ActionPerformed
private void jMenuItem1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem1MouseClicked
IJ.setTool("freehand"); // TODO add your handling code here: // TODO add your handling code here:
}//GEN-LAST:event_jMenuItem1MouseClicked
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
IJ.setTool("freehand"); // TODO add your handling code here: // TODO add your handling code here:
}//GEN-LAST:event_jMenuItem1ActionPerformed
private void jMenuItem2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem2MouseClicked
IJ.setTool("rectangle");// TODO add your handling code here: // TODO add your handling code here:
}//GEN-LAST:event_jMenuItem2MouseClicked
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
IJ.setTool("rectangle"); // TODO add your handling code here:
}//GEN-LAST:event_jMenuItem2ActionPerformed
private void jMenu2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu2MouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_jMenu2MouseClicked
private void txtfilenameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtfilenameActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtfilenameActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(classifyfromimage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(classifyfromimage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(classifyfromimage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(classifyfromimage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
/*java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new classifyfromimage().setVisible(true);
}
});*/
new classifyfromimage().setVisible(true);
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu9;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
public static javax.swing.JTextField txtfilename;
// End of variables declaration//GEN-END:variables
}