package anotacja; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.File; import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.StringTokenizer; public class WebCBIRwithPATSI extends WebCBIR { private boolean PathImages = true; private BufferedWriter Log = null; void BegginLogging(BufferedWriter newLog) { Log = newLog; } void StopLogging() { Log = null; } void Log(String File, double[] Transfers) throws IOException { if (Log != null) { String ToWrite = File; for (int i = 0; i < Transfers.length; i++) ToWrite += (";" + Transfers[i]); ToWrite += "\n"; Log.write(ToWrite); } } public class OrderedWithIndex extends Ordered { private final int Index; public int getIndex() { return Index; } public OrderedWithIndex(T element, int initIndex, double value) { super(element, value); Index = initIndex; } } public WebCBIRwithPATSI(String basePath, String imagePath, String annotationPath, TransferFunction tf, double t, int k) { super(basePath, imagePath,500); if (imagePath.equals("null")) PathImages = false; annotationFolder = new File(annotationPath); transferFunction = tf; T = t; K = k; } public WebCBIRwithPATSI(String basePath, String imagePath, String annotationPath) { super(basePath, imagePath,500); if (imagePath.equals("null")) PathImages = false; annotationFolder = new File(annotationPath); transferFunction = new RankTransferFunction(); T = 0.73; K = 19; } public File annotationFolder; public TransferFunction transferFunction; public double T; public double[] TArray = null; public double GetT(int Annot) { if (TArray == null) return T; else return TArray[Annot]; } public void LoadTArray(String File, int Annotations) throws Exception { TArray = new double[Annotations]; System.out.println("Reading data..."); BufferedReader Reader = new BufferedReader(new FileReader(new File(File))); for (int i = 0; i < Annotations; i++) { TArray[i] = Double.parseDouble(Reader.readLine()); } Reader.close(); } public int K; public Map getAnnotations() throws Exception { Map map = new HashMap(); BufferedReader readList = new BufferedReader(new FileReader(new File(annotationFolder, "annotation.list"))); String line; while ((line = readList.readLine()) != null) { StringTokenizer tokens = new StringTokenizer(line, ";"); map.put(Integer.parseInt(tokens.nextToken()), tokens.nextToken()); } readList.close(); return map; } public 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; } public List getAnnotationsForFile(File file) throws Exception { return getAnnotationsFromFile(getAnnotationFile(file)); } public File getAnnotationFile(File imageFile) { String temp = imageFile.getName(); int resIndex = temp.lastIndexOf(".png"); temp = temp.substring(0, resIndex + 4); return new File(annotationFolder, temp + ".annot"); } public List> annotateQuery(String query, List> similarity) throws Exception { Map annotations = getAnnotations(); double[] annotationWeights = new double[annotations.keySet().size()]; int currentK = 0; String queryFileName = (new File(query)).getName(); for (Ordered current : similarity) { if (current.get().equalsIgnoreCase(queryFileName)) continue; if (currentK == K) break; currentK++; double transfer = transferFunction.TF(currentK, current.value()); List toTransfer = getAnnotationsForFile(new File(current.get())); for (Integer i : toTransfer) annotationWeights[i] += transfer; } List> result = new LinkedList>(); Log(query, annotationWeights); for (int i = 0; i < annotationWeights.length; i++) if (annotationWeights[i] > GetT(i)) result.add(new OrderedWithIndex(annotations.get(i), i, annotationWeights[i])); Collections.sort(result); return result; } public void printAnnotation(List> result, boolean enclose, boolean skipRuins) { if (enclose) System.out.println("{"); System.out.println(" \"Annotations\" : ["); for (Ordered o: result) { if (!skipRuins || !o.get().equals("ruina")) { System.out.print(" { \"Name\" : \"" + o.get() + "\" , \"TransferStrength\" : " + o.value() + "}"); if (o != result.get(result.size() - 1)) System.out.println(", "); else System.out.println(); } } System.out.println(" ]"); if (enclose) System.out.println("}"); } public String LocationFile = null; public String getLocation(String File) { String Default = "\"Unknown\""; try { int Dot = File.indexOf('.'); File = File.substring(0, Dot); File += ";"; BufferedReader Reader = new BufferedReader(new FileReader(new File(LocationFile))); String Line; while ((Line = Reader.readLine()) != null) if (Line.startsWith(File)) { Reader.close(); String Out = Line.substring(File.length()); Out = "\"" + Out + "\""; return Out; } Reader.close(); return Default; } catch (Exception Ex) { return Default; } } public void printShortSimilarity(List> result, String queryData, boolean enclose) { HashMap Locations = new HashMap(); if (enclose) System.out.println("{"); System.out.println(" \"Similarity\" : ["); int currentK = 0; for (Ordered current : result) { if (current.get().equalsIgnoreCase(queryData)) continue; if (currentK == K) break; currentK++; String out = current.get(); out = out.substring(0, out.lastIndexOf(".png") + 4); String Location = getLocation(current.get()); if (PathImages) System.out.print(" { \"Image\" : \"" + new File(imageFolder, out).getAbsolutePath().replace("/home/www/temp/workspace/visible/", "").replace("\\", "\\\\") + "\" , "); else System.out.print(" { \"Image\" : \"" + out.replace("\\", "\\\\") + "\" , "); System.out.print("\"Location\" : " + Location + " , "); System.out.print("\"Similarity\" : " + current.value() + " }"); if ((currentK < K) && (current != result.get(result.size() - 1))) System.out.println(", "); else System.out.println(); if (Locations.get(Location) == null) { Locations.put(Location, 1.0 / (double)currentK); } else { Locations.put(Location, Locations.get(Location) + (1.0 / (double)currentK)); } } System.out.println(" ]"); System.out.println(" ,"); double Value = Double.NEGATIVE_INFINITY; String Location = "\"Unknown\""; for (String Current : Locations.keySet()) { double CurrentValue = Locations.get(Current); if (CurrentValue > Value) { Value = CurrentValue; Location = Current; } } System.out.println(" \"Location\" : { \"Name\" : " + Location + " , \"TransferStrength\" : " + Value + "}"); if (enclose) System.out.println("}"); } public static void main(String[] args) { if (args.length != 9) { System.out.println("No correct query given:"); for(String s : args) System.out.print(s + " "); return; } String query = args[0]; String clust = args[1]; String exten = args[2]; int clusters = Integer.parseInt(clust); String queryData = query + exten + ".cluster_" + clusters; queryData = queryData.replace("collections/zabytki-anot/demo-queries/", ""); WebCBIRwithPATSI Anot = new WebCBIRwithPATSI(args[3], args[4], args[5], new RankTransferFunction(), Double.parseDouble(args[6]), Integer.parseInt(args[7])); Anot.LocationFile = args[8]; try { List> CBIRresults = Anot.compareAllToQuery(queryData, clusters); List> ANOTresults = Anot.annotateQuery(queryData, CBIRresults); System.out.println("{"); Anot.printAnnotation(ANOTresults, false, false); System.out.println(" ,"); Anot.printShortSimilarity(CBIRresults, queryData, false); System.out.println("}"); } catch (Exception ex) { System.out.println("Something went wrong:"); System.out.println("Query: " + queryData); System.out.println("Could find: " + new File(queryData).exists()); ex.printStackTrace(System.out); } } }