package anotacja; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.StringTokenizer; import anotacja.WebCBIR.Ordered; public class GradientEntropyDataExtractor { private static List GetAnnotationsFromFile(File file) throws Exception { List list = new LinkedList(); BufferedReader readFile = new BufferedReader(new FileReader(file)); StringTokenizer tokens = new StringTokenizer(readFile.readLine()); while (tokens.hasMoreTokens()) list.add(Integer.parseInt(tokens.nextToken())); readFile.close(); return list; } private static List GetAnnotationsForFile(File file) throws Exception { return GetAnnotationsFromFile(GetAnnotationFile(file)); } private static File GetAnnotationFile(File imageFile) { String temp = imageFile.getName(); int resIndex = temp.lastIndexOf(".png"); temp = temp.substring(0, resIndex + 4); return new File(AnnotationPath, temp + ".annot"); } private static String AnnotationPath; private static boolean AnnotationExists(File CheckThis) { return GetAnnotationFile(CheckThis).exists(); } public static void main(String[] args) { try { String CBIRPath = args[0]; AnnotationPath = args[1]; File Queries = new File(args[2]); int Annotations = Integer.parseInt(args[3]); int SimLimit = Integer.parseInt(args[4]); int Clusters = Integer.parseInt(args[5]); String Extension = args[6]; double wT = Double.parseDouble(args[7]); double wF = Double.parseDouble(args[8]); String FullExtension = Extension + ".cluster_" + Clusters; WebCBIR CBIR = new WebCBIR(args[0], "",500); System.out.print("[?] Building data structures... "); List TrainingFiles = new ArrayList(); for (File f : new File(CBIRPath, "files-out").listFiles()) if (f.getName().endsWith(FullExtension) && AnnotationExists(f)) TrainingFiles.add(f); List TestFiles = new ArrayList(); for (File f : Queries.listFiles()) if (f.getName().endsWith(FullExtension) && AnnotationExists(f)) TestFiles.add(f); double[][][] TrainingData = new double[Annotations][TrainingFiles.size()][SimLimit]; boolean[][] TrainingClass = new boolean[Annotations][TrainingFiles.size()]; for (int a = 0; a < Annotations; a++) for (int b = 0; b < TrainingFiles.size(); b++) { TrainingClass[a][b] = false; for (int c = 0; c < SimLimit; c++) TrainingData[a][b][c] = wF; } double[][][] TestData = new double[Annotations][TestFiles.size()][SimLimit]; boolean[][] TestClass = new boolean[Annotations][TestFiles.size()]; for (int a = 0; a < Annotations; a++) for (int b = 0; b < TestFiles.size(); b++) { TestClass[a][b] = false; for (int c = 0; c < SimLimit; c++) TestData[a][b][c] = wF; } System.out.println("[DONE]"); System.out.println("[?] Filling data structures..."); System.out.println("\t[-] Filling training set..."); int N = 0; for (File f : TrainingFiles) { System.out.println("\t[-] Now reading: " + f.getName()); List AnnotationList; List> Similarity = CBIR.compareAllToQuery(f.getAbsolutePath(), Clusters); AnnotationList = GetAnnotationsForFile(f); for (Integer i : AnnotationList) TrainingClass[i][N] = true; int M = 0; for (Ordered o : Similarity) { if (M >= SimLimit) break; if (f.getName().equalsIgnoreCase(o.get()) || !AnnotationExists(new File(o.get()))) continue; AnnotationList = GetAnnotationsForFile(new File(o.get())); for (Integer i : AnnotationList) TrainingData[i][N][M] = wT; M++; } N++; } N = 0; System.out.println("\t[-] Filling test set..."); for (File f : TestFiles) { System.out.println("\t[-] Now reading: " + f.getName()); List AnnotationList; List> Similarity = CBIR.compareAllToQuery(f.getAbsolutePath(), Clusters); AnnotationList = GetAnnotationsForFile(f); for (Integer i : AnnotationList) TestClass[i][N] = true; int M = 0; for (Ordered o : Similarity) { if (M >= SimLimit) break; if (f.getName().equalsIgnoreCase(o.get()) || !AnnotationExists(new File(o.get()))) continue; AnnotationList = GetAnnotationsForFile(new File(o.get())); for (Integer i : AnnotationList) TestData[i][N][M] = wT; M++; } N++; } File SaveFolder = new File(new File(CBIRPath), "sim-extracted"); if (SaveFolder.exists() == false) SaveFolder.mkdir(); for (int i = 0; i < Annotations; i++) { System.out.println("[!] Saving files for annotation: " + i); File USaveFile = new File(SaveFolder, "u.a" + i + ".csv"); File TSaveFile = new File(SaveFolder, "t.a" + i + ".csv"); BufferedWriter UOut = new BufferedWriter(new FileWriter(USaveFile)); for (int j = 0; j < TrainingData[i].length; j++) { for (int k = 0; k < TrainingData[i][j].length; k++) { UOut.write(TrainingData[i][j][k] + ";"); } if (TrainingClass[i][j]) UOut.write("T"); else UOut.write("F"); UOut.newLine(); } UOut.close(); BufferedWriter TOut = new BufferedWriter(new FileWriter(TSaveFile)); for (int j = 0; j < TestData[i].length; j++) { for (int k = 0; k < TestData[i][j].length; k++) { TOut.write(TestData[i][j][k] + ";"); } if (TestClass[i][j]) TOut.write("T"); else TOut.write("F"); TOut.newLine(); } TOut.close(); } System.out.println("[!] Done"); } catch (Exception Ex) { Ex.printStackTrace(); } } }