Skip to content

Commit ca36887

Browse files
author
microos
committed
tiny changes & start working on output
1 parent 066e9d0 commit ca36887

6 files changed

Lines changed: 159 additions & 53 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
### TODO:
44
- [x] basic functions
5-
- [ ] ask for prefix and suffix when load imglist
5+
- [x] ask for prefix and suffix when load imglist
66
- [ ] shift choose on ellipse -> hightlight right table row
77
- [ ] click on right table row(if static) -> enlarge stroke
88
- [ ] click on right table row(if non-static) -> set as activated ellipse
9-
- [ ] load imglist -> disable load img button
10-
- [ ] one click after load -> disable load annotation button
11-
- [ ] load annotation -> disable load annotatino button
9+
- [x] load imglist -> disable load img button
10+
- [x] one click after load -> disable load annotation button
11+
- [x] load annotation -> disable load annotatino button
1212
- [ ] output file function

src/hk/microos/data/Flags.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ public class Flags {
55
public static boolean FUNC_INVOKE_PRINT = true;
66
public static double minStroke = 2;
77
public static double maxStroke = 5;
8+
public static int numNewEllipse = 0;
89
}

src/hk/microos/frames/MainFrame.java

Lines changed: 91 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import javax.swing.table.DefaultTableModel;
1414

1515
import hk.microos.data.Ellipse;
16+
import hk.microos.data.Flags;
1617
import hk.microos.data.MyImage;
1718
import hk.microos.tools.ClickHelper;
1819
import hk.microos.tools.IOTool;
@@ -63,13 +64,16 @@ public class MainFrame extends JFrame {
6364
private String recordedImgPath = "/home/rick/Space/work/FDDB/data/Annotations";
6465

6566
private String recordedAnnotPath = "/home/rick/Space/work/FDDB/data/Annotations";
67+
private String recordedSavePath = "/home/rick/Space/work/FDDB/data/Annotations";
6668
private JButton btnReadImageList;
6769

6870
private int leftTableSelectedRow = -1;
6971
private HashMap<String, MyImage> pathImgPair = null;
7072
private HashMap<String, ArrayList<Ellipse>> pathElpsesPair = null;
7173
private JButton btnReadAnnotations;
72-
74+
private String annotContentPrefix = null;
75+
private String annotContentSuffix = null;
76+
private JButton btnOutputAnnotation;
7377
/**
7478
* Launch the application.
7579
*/
@@ -81,13 +85,13 @@ public void run() {
8185
MainFrame frame = new MainFrame();
8286
// frame.recordedOpenPath =
8387
// System.getProperty("user.home")+"/Desktop";
84-
// frame.recordedImgPath =
85-
// "/Users/microos/Downloads/originalPics/imgPath.txt";
88+
frame.recordedImgPath =
89+
"/Users/microos/Downloads/originalPics/imgPath.txt";
8690

8791
// frame.recordedAnnotPath = System.getProperty("user.home")
8892
// + "/Desktop";
89-
// frame.recordedAnnotPath =
90-
// "/Users/microos/Downloads/FDDB-folds/FDDB-fold-05-ellipseList.txt";
93+
frame.recordedAnnotPath =
94+
"/Users/microos/Downloads/FDDB-folds/FDDB-fold-05-ellipseList.txt";
9195
frame.setVisible(true);
9296
} catch (Exception e) {
9397
e.printStackTrace();
@@ -167,6 +171,14 @@ public void actionPerformed(ActionEvent e) {
167171
}
168172
});
169173
toolPanel.add(btnReadAnnotations);
174+
175+
btnOutputAnnotation = new JButton("Output Annotation");
176+
btnOutputAnnotation.addActionListener(new ActionListener() {
177+
public void actionPerformed(ActionEvent e) {
178+
outputAnnotations();
179+
}
180+
});
181+
toolPanel.add(btnOutputAnnotation);
170182

171183
imagePanel = new MyImagePanel(this, scrollPanel);
172184
imagePanel.addMouseListener(new MouseAdapter() {
@@ -242,7 +254,8 @@ void leftTableOnClick(ListSelectionEvent e) {
242254
}
243255

244256
void setRightPanelCoords(MyImage mim) {
245-
257+
//call this will update right panel with image's staticEllipse/Ellipse
258+
//use null as the arg will use a selectedRow as target image
246259
if (mim == null) {
247260
System.out.println("setRightPanelCoords with null");
248261
if (leftTableSelectedRow == -1) {
@@ -307,14 +320,15 @@ void loadImageList() {
307320
// read successes
308321
// load Images to table
309322
// reset
310-
pathImgPair = null;
311-
leftTableSelectedRow = -1;
312-
imagePanel.reset();
323+
// pathImgPair = null;
324+
// leftTableSelectedRow = -1;
325+
// imagePanel.reset();
313326

314327
pathImgPair = IOTool.filterImageList(imgList, this);
315328
fillImageNameTable();
316329
leftTableSelectedRow = 0;
317330
imgListTH.setSelectedRow(leftTableSelectedRow);
331+
freezeReadImageListBtn();
318332

319333
}
320334
}
@@ -334,6 +348,16 @@ void loadAnnotList() {
334348
if (pathImgPair == null) {
335349
// no images loaded, abort
336350
return;
351+
}
352+
if(annotContentPrefix == null){
353+
String s = JOptionPane.showInputDialog(this, "Please set a prefix string for your annotation image path.\n(leave empty or cancel if not necessary)", "Prefix", JOptionPane.QUESTION_MESSAGE);
354+
annotContentPrefix = s==null? "" : s;
355+
356+
s = JOptionPane.showInputDialog(this, "Please set a suffix(usually is image format extention) for your annotation image path.\n(leave empty or cancel if not necessary)", "Suffix", JOptionPane.QUESTION_MESSAGE);
357+
annotContentSuffix = s==null? "" : s;
358+
359+
360+
337361
}
338362
JFileChooser fc = new JFileChooser(recordedAnnotPath);
339363
// fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
@@ -351,7 +375,7 @@ void loadAnnotList() {
351375
boolean noError = true;
352376
String errMessage = "";
353377
try {
354-
pathElpsesPair = IOTool.readAnnotationFile(f);
378+
pathElpsesPair = IOTool.readAnnotationFile(f, annotContentPrefix, annotContentSuffix);
355379
} catch (Exception e) {
356380
pathElpsesPair = null;
357381
noError = false;
@@ -382,6 +406,8 @@ void loadAnnotList() {
382406
}
383407
}
384408
marksLoadAnnotationUpdate();
409+
imagePanel.repaint();
410+
freezeReadAnnotationBtn();
385411
// // reset
386412
// pathImgPair = null;
387413
// leftTableSelectedRow = -1;
@@ -431,7 +457,7 @@ public void marksLoadAnnotationUpdate() {
431457
}
432458

433459
public void marksUpdatedAtSelected(MyImage mim) {
434-
460+
435461
if (leftTableSelectedRow == -1) {
436462
System.err.println("marking on non-loaded image?");
437463
}
@@ -444,7 +470,12 @@ public void marksUpdatedAtSelected(MyImage mim) {
444470
imgListTH.setValueAt(leftTableSelectedRow, 2, mim.getMarkNumString());
445471
setRightPanelCoords(mim);
446472
}
447-
473+
public void freezeReadImageListBtn(){
474+
this.btnReadImageList.setEnabled(false);
475+
}
476+
public void freezeReadAnnotationBtn(){
477+
this.btnReadAnnotations.setEnabled(false);
478+
}
448479
MyImage getMyImageFromPathImgPair(String path) {
449480
MyImage img = pathImgPair.get(path);
450481
if (img == null) {
@@ -455,6 +486,54 @@ MyImage getMyImageFromPathImgPair(String path) {
455486
} else {
456487
return img;
457488
}
489+
}
490+
void outputAnnotations(){
491+
if(pathImgPair == null){
492+
return; //even no image loaded? abort
493+
}
494+
if(Flags.numNewEllipse <= 0){
495+
JOptionPane.showMessageDialog(this, "You did not mark any annotations, nothing to output.","Nothing to output",JOptionPane.WARNING_MESSAGE);
496+
return;
497+
}
498+
//open a chooser
499+
//make it a .txt file
500+
//output only the new Ellipse
501+
502+
JFileChooser fc = new JFileChooser(recordedSavePath);
503+
// fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
504+
int res = fc.showOpenDialog(this);
505+
if (res == JFileChooser.APPROVE_OPTION) {
506+
File f = fc.getSelectedFile();
507+
recordedImgPath = f.getParent();
508+
if (!IOTool.isTextFile(f)) {
509+
// not a readable text file
510+
JOptionPane.showMessageDialog(this,
511+
String.format("\"%s\" \nis not a txt file.", f.getAbsolutePath()).toString(), "Not a txt file",
512+
JOptionPane.WARNING_MESSAGE);
513+
} else {
514+
// read
515+
ArrayList<String> imgList = IOTool.readText(f);
516+
if (imgList == null) {
517+
// read failed
518+
JOptionPane.showMessageDialog(this,
519+
String.format("\"%s\" \ncannot be properly read.", f.getAbsolutePath()).toString(),
520+
"Reading failed", JOptionPane.WARNING_MESSAGE);
521+
} else {
522+
// read successes
523+
// load Images to table
524+
// reset
525+
// pathImgPair = null;
526+
// leftTableSelectedRow = -1;
527+
// imagePanel.reset();
528+
529+
pathImgPair = IOTool.filterImageList(imgList, this);
530+
fillImageNameTable();
531+
leftTableSelectedRow = 0;
532+
imgListTH.setSelectedRow(leftTableSelectedRow);
533+
freezeReadImageListBtn();
458534

535+
}
536+
}
537+
}
459538
}
460539
}

src/hk/microos/frames/MyImagePanel.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import javax.swing.plaf.synth.SynthSpinnerUI;
1515

1616
import hk.microos.data.Ellipse;
17+
import hk.microos.data.Flags;
1718
import hk.microos.data.LinearLine;
1819
import hk.microos.data.MyImage;
1920
import hk.microos.data.Point_;
@@ -249,6 +250,7 @@ public void updateStatus() {
249250
}
250251
if (unfinished.size() == 3) {
251252
mImg.addElps(new Ellipse(unfinished.get(0), unfinished.get(1), this.projPoint));
253+
Flags.numNewEllipse ++;
252254
unfinished = new ArrayList<>();
253255
this.activedEllipseIdx = mImg.getElpses().size() - 1;
254256
}
@@ -257,28 +259,28 @@ public void updateStatus() {
257259
}
258260

259261
public void addUnfinishedPoint(Point_ p) {
260-
262+
mainFrame.freezeReadAnnotationBtn();
261263
unfinished.add(p);
262264
updateStatus();
263265
}
264266

265267
public void activateClosest(int x, int y) {
266-
// try to remove the closest one, if the unfinished is clear
268+
//if found that we still have jobs to be done OR no ellipse can be deleted --> abort
267269
if (unfinished.size() != 0 || mImg.getElpses().size() == 0) {
268270
return;
269271
}
270-
// when it indeed is clear
271272
Point_ p = new Point_(x, y);
272273
this.activedEllipseIdx = findTheClosestEllipse(p);
273274
this.repaint();
274275
}
275276

276277
public void removeActived() {
277278
if (unfinished.size() != 0) {
278-
// canont remove any ellipse, just remove unfinished
279+
// cannot remove any ellipse, just remove unfinished point
279280
unfinished.remove(unfinished.size() - 1);
280281

281282
} else {
283+
Flags.numNewEllipse --;
282284
ArrayList<Ellipse> elpses = mImg.getElpses();
283285
if (elpses.size() != 0 && this.activedEllipseIdx != -1) {
284286
Ellipse e = elpses.remove(this.activedEllipseIdx);

src/hk/microos/tools/IOTool.java

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package hk.microos.tools;
22

33
import java.io.File;
4+
import java.io.FileWriter;
45
import java.io.IOException;
56
import java.nio.charset.Charset;
67
import java.nio.file.Files;
@@ -12,6 +13,7 @@
1213
import javax.swing.JOptionPane;
1314

1415
import hk.microos.data.Ellipse;
16+
import hk.microos.data.Flags;
1517
import hk.microos.data.MyImage;
1618

1719
public class IOTool {
@@ -64,50 +66,68 @@ public static HashMap filterImageList(ArrayList<String> list, JFrame dialogFathe
6466
}
6567
return map;
6668
}
69+
private static void writeFile(String txtPath, String content) {
70+
File f = new File(txtPath);
71+
FileWriter fw;
72+
try {
73+
if (!f.exists())
74+
f.createNewFile();
6775

68-
public static HashMap<String, ArrayList<Ellipse>> readAnnotationFile(File f) throws Exception {
76+
fw = new FileWriter(f);
77+
fw.write(content);
78+
fw.flush();
79+
fw.close();
80+
} catch (IOException e) {
81+
e.printStackTrace();
82+
}
83+
}
84+
public static HashMap<String, ArrayList<Ellipse>> readAnnotationFile(File f, String prefix, String suffix) throws Exception {
6985
HashMap<String, ArrayList<Ellipse>> map = new HashMap<>();
7086
ArrayList<String> lines = readText(f);
71-
String prefix = "/Users/microos/Downloads/originalPics/";
72-
prefix = "/home/rick/Space/work/FDDB/data/images/";
73-
prefix = "";
74-
String suffix = ".jpg";
7587

88+
if(Flags.GLOABAL_DEBUG){
89+
prefix = "/Users/microos/Downloads/originalPics/";
90+
// prefix = "/home/rick/Space/work/FDDB/data/images/";
91+
// prefix = "";
92+
93+
suffix = ".jpg";
94+
}
95+
96+
7697
// parse annotation files
7798
int at = 0;
7899
while (at < lines.size()) {
79-
String path = prefix+lines.get(at)+suffix;
100+
String path = prefix + lines.get(at) + suffix;
80101
at++;
81-
102+
82103
int detNum = Integer.parseInt(lines.get(at));
83104
at++;
84-
105+
85106
ArrayList<Ellipse> elpses = readNEllipse(lines, at, detNum);
86107
at += detNum;
87-
88-
if(new File(path).exists()){
108+
109+
if (new File(path).exists()) {
89110
map.put(path, elpses);
90-
}else{
91-
System.err.println(path+" listed in annotation files not found!");
111+
} else {
112+
System.err.println(path + " listed in annotation files not found!");
92113
}
93-
94114

95115
}
96116

97117
return map;
98118
}
99119

100-
private static ArrayList<Ellipse> readNEllipse(ArrayList<String> lines, int start, int num) throws Exception{
120+
private static ArrayList<Ellipse> readNEllipse(ArrayList<String> lines, int start, int num) throws Exception {
101121
ArrayList<Ellipse> elps = new ArrayList<>();
102122
for (int i = 0; i < num; i++) {
103123
int at = start + i;
104124
String line = lines.get(at);
105125
String[] splitStr = line.split(" +");
106-
if(splitStr.length < 5){
126+
if (splitStr.length < 5) {
107127
throw new Exception(String.format("Line %d: contains less than 5 float values.", at));
108128
}
109129
ArrayList<Double> splitFlt = new ArrayList<>();
110-
for(String s:splitStr){
130+
for (String s : splitStr) {
111131
splitFlt.add(Double.parseDouble(s));
112132
}
113133
elps.add(new Ellipse(splitFlt));

0 commit comments

Comments
 (0)