Exemplo n.º 1
0
  public void testSearch() throws IOException {
    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexPath)));
    int numDocs = reader.numDocs();
    System.out.println("numDocs = " + numDocs);
    ImageSearcher searcher = ImageSearcherFactory.createScalableColorImageSearcher(50);
    FileInputStream imageStream = new FileInputStream(testFilesPath + testFiles[0]);
    BufferedImage bimg = ImageIO.read(imageStream);
    ImageSearchHits hits = null;
    long time = System.currentTimeMillis();
    for (int i = 0; i < numsearches; i++) {
      hits = searcher.search(bimg, reader);
    }
    time = System.currentTimeMillis() - time;
    System.out.println(
        ((float) time / (float) numsearches)
            + " ms per search with image, averaged on "
            + numsearches);
    for (int i = 0; i < 5; i++) {
      System.out.println(
          hits.score(i)
              + ": "
              + hits.doc(i).getField(DocumentBuilder.FIELD_NAME_IDENTIFIER).stringValue());
    }
    Document document = hits.doc(0);
    time = System.currentTimeMillis();
    for (int i = 0; i < numsearches; i++) {
      hits = searcher.search(document, reader);
    }
    time = System.currentTimeMillis() - time;
    System.out.println(
        ((float) time / (float) numsearches)
            + " ms per search with document, averaged on "
            + numsearches);
    for (int i = 0; i < 5; i++) {
      System.out.println(
          hits.score(i)
              + ": "
              + hits.doc(i).getField(DocumentBuilder.FIELD_NAME_IDENTIFIER).stringValue());
    }

    document = getDocumentBuilder().createDocument(bimg, testFilesPath + testFiles[0]);
    time = System.currentTimeMillis();
    for (int i = 0; i < numsearches; i++) {
      hits = searcher.search(document, reader);
    }
    time = System.currentTimeMillis() - time;
    System.out.println(
        ((float) time / (float) numsearches)
            + " ms per search with document, averaged on "
            + numsearches);
    for (int i = 0; i < 5; i++) {
      System.out.println(
          hits.score(i)
              + ": "
              + hits.doc(i).getField(DocumentBuilder.FIELD_NAME_IDENTIFIER).stringValue());
    }
  }
Exemplo n.º 2
0
  /**
   * There was an error that images with the same score but different documents in the index were
   * not included in the result list. Here's the test for that.
   */
  public void testDuplicatesInIndex() throws IOException {
    indexFiles("src\\test\\resources\\images", "index-large-new", 0, true);
    indexFiles("src\\test\\resources\\images", "index-large-new", 0, false);
    indexFiles("src\\test\\resources\\images", "index-large-new", 0, false);

    ImageSearcher s = searchers[0];
    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File("index-large-new")));
    Document query = reader.document(0);
    ImageSearchHits hits = s.search(query, reader);
    FileUtils.saveImageResultsToPng(
        "duplicate_", hits, query.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
  }
  public double singleSearch(int docNum)
      throws IOException, InstantiationException, IllegalAccessException {
    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexPath)));

    // -----------

    String query = reader.document(docNum).getValues("hash")[0];
    CEDD ceddQuery = new CEDD();
    ceddQuery.setByteArrayRepresentation(
        reader.document(docNum).getField(DocumentBuilder.FIELD_NAME_CEDD).binaryValue().bytes,
        reader.document(docNum).getField(DocumentBuilder.FIELD_NAME_CEDD).binaryValue().offset,
        reader.document(docNum).getField(DocumentBuilder.FIELD_NAME_CEDD).binaryValue().length);

    // -----------

    HashSet<String> gold = new HashSet<String>(numImagesEval);
    ImageSearcher cis = ImageSearcherFactory.createCEDDImageSearcher(100);
    ImageSearchHits hits = cis.search(reader.document(docNum), reader);
    for (int i = 0; i < 10; i++) {
      gold.add(hits.doc(i).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
    }

    // ------------

    IndexSearcher searcher = new IndexSearcher(reader);
    searcher.setSimilarity(
        new SimilarityBase() {
          @Override
          protected float score(BasicStats basicStats, float freq, float v2) {
            return 1;
          }

          @Override
          public String toString() {
            return null;
          }
        });
    TopDocs topDocs = searcher.search(createQuery(query), 500);
    topDocs = rerank(topDocs, ceddQuery, reader);
    //        System.out.println("topDocs.scoreDocs.length = " + topDocs.scoreDocs.length);
    double numMatches = 0;
    for (int i = 0; i < topDocs.scoreDocs.length; i++) {
      ScoreDoc scoreDoc = topDocs.scoreDocs[i];
      //            System.out.print(scoreDoc.score + ": ");
      String file =
          reader.document(scoreDoc.doc).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
      //            System.out.println(file.substring(file.lastIndexOf('/') + 1) +
      // (gold.contains(file)?" x":" o"));
      if (gold.contains(file)) numMatches++;
    }
    return numMatches;
  }
Exemplo n.º 4
0
 public void testFindDuplicates() throws Exception {
   IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexPath)));
   int numDocs = reader.numDocs();
   System.out.println("numDocs = " + numDocs);
   ImageSearcher searcher = ImageSearcherFactory.createCEDDImageSearcher(50);
   ImageDuplicates imageDuplicates = searcher.findDuplicates(reader);
   if (imageDuplicates == null) {
     System.out.println("No duplicates found");
     return;
   }
   for (int i = 0; i < imageDuplicates.length(); i++) {
     System.out.println(imageDuplicates.getDuplicate(i).toString());
   }
 }
Exemplo n.º 5
0
 public void computeErrorRate(ImageSearcher searcher, String prefix)
     throws IOException, InstantiationException, IllegalAccessException {
   //        int maxHits = 10;
   IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(testIndex)));
   for (Iterator<String> testIterator = testcases.keySet().iterator(); testIterator.hasNext(); ) {
     queryImage = testIterator.next();
     Document query;
     if (cutImages) {
       BufferedImage bimg =
           ImageUtils.cropImage(ImageIO.read(new FileInputStream(queryImage)), 0, 0, 200, 69);
       query = builder.createDocument(new FileInputStream(queryImage), queryImage);
     } else query = builder.createDocument(new FileInputStream(queryImage), queryImage);
     ImageSearchHits hits = searcher.search(query, reader);
     // hits = rerank(hits, query, ColorLayout.class, DocumentBuilder.FIELD_NAME_COLORLAYOUT);
     for (int i = 0; i < hits.length(); i++) {
       if (hits.doc(i)
           .get("descriptorImageIdentifier")
           .toLowerCase()
           .endsWith(testcases.get(queryImage))) {
         System.out.println(
             queryImage.substring(queryImage.lastIndexOf('\\') + 1)
                 + "-"
                 + prefix
                 + " -> Found at rank "
                 + i
                 + " ("
                 + hits.length()
                 + ")");
       }
     }
     // saveToHtml(queryImage.substring(queryImage.lastIndexOf('\\') + 1) + "-" + prefix, hits,
     // queryImage);
   }
 }
Exemplo n.º 6
0
  public void tttestMAPLocalFeatureHistogram() throws IOException {
    int maxSearches = 200;
    int maxHits = 100;
    IndexReader reader = IndexReader.open(FSDirectory.open(new File(indexPath)));
    IndexSearcher is = new IndexSearcher(reader);
    ImageSearcher searcher;
    //        searcher = new SiftLocalFeatureHistogramImageSearcher(maxHits);
    searcher = ImageSearcherFactory.createColorHistogramImageSearcher(maxHits);
    //        searcher = ImageSearcherFactory.createCEDDImageSearcher(maxHits);
    //        searcher = ImageSearcherFactory.createFCTHImageSearcher(maxHits);
    Pattern p = Pattern.compile("\\\\\\d+\\.jpg");
    double map = 0;
    for (int i = 0; i < sampleQueries.length; i++) {
      int id = sampleQueries[i];
      System.out.print("id = " + id + ": ");
      String file = testExtensive + "/" + id + ".jpg";

      ImageSearchHits hits = searcher.search(findDoc(reader, id + ".jpg"), reader);
      int goodOnes = 0;
      double avgPrecision = 0;
      for (int j = 0; j < hits.length(); j++) {
        Document d = hits.doc(j);
        String hitsId = d.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
        Matcher matcher = p.matcher(hitsId);
        if (matcher.find()) hitsId = hitsId.substring(matcher.start() + 1, hitsId.lastIndexOf("."));
        else fail("Did not get the number ...");
        int testID = Integer.parseInt(hitsId);
        //                System.out.print(". " + hitsId + "/"  +
        // d.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]+ " ");
        if ((int) Math.floor(id / 100) == (int) Math.floor(testID / 100)) {
          goodOnes++;
          System.out.print("x");
        } else {
          System.out.print("o");
        }
        //                System.out.print(" (" + testID + ") ");
        avgPrecision += (double) goodOnes / (double) (j + 1);
      }
      avgPrecision = avgPrecision / hits.length();
      map += avgPrecision;
      System.out.println(" " + avgPrecision + " (" + map / (i + 1) + ")");
    }
    map = map / sampleQueries.length;
    System.out.println("map = " + map);
  }
Exemplo n.º 7
0
  public void testLsaFilter() throws IOException {
    // index images
    //        indexFiles();
    // search
    System.out.println("---< searching >-------------------------");
    IndexReader reader = IndexReader.open(FSDirectory.open(new File(indexPath)));
    Document document = reader.document(0);
    ImageSearcher searcher = ImageSearcherFactory.createCEDDImageSearcher(100);
    ImageSearchHits hits = searcher.search(document, reader);
    // rerank
    System.out.println("---< filtering >-------------------------");
    LsaFilter filter = new LsaFilter(CEDD.class, DocumentBuilder.FIELD_NAME_CEDD);
    hits = filter.filter(hits, document);

    // output
    FileUtils.saveImageResultsToHtml(
        "filtertest", hits, document.getField(DocumentBuilder.FIELD_NAME_IDENTIFIER).stringValue());
  }
Exemplo n.º 8
0
  public void testCreateAndSearchSmallIndex() throws IOException {
    for (int i = 0, buildersLength = builders.length; i < buildersLength; i++) {
      DocumentBuilder b = builders[i];
      // create an index with a specific builder:
      IndexWriter iw = LuceneUtils.createIndexWriter(indexPath + "-small", true);
      for (String identifier : testFiles) {
        Document doc =
            b.createDocument(new FileInputStream(testFilesPath + identifier), identifier);
        doc.add(new StoredField("video_file", "surgery1.mp4"));
        doc.add(new StoredField("timestamp", "25"));
        iw.addDocument(doc);
      }
      iw.close();

      ImageSearcher s = searchers[i];
      IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexPath + "-small")));
      for (int k = 0; k < reader.maxDoc(); k++) {
        Document query = reader.document(k);
        ImageSearchHits hits = s.search(query, reader);
        for (int y = 0; y < hits.length(); y++) {
          Document result = hits.doc(y);
          if (y == 0) {
            // check if the first result is the query:
            assertEquals(
                result.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0].equals(
                    query.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]),
                true);
            System.out.println(result.getValues("video_file")[0]);
          } else {
            // check if they are ordered by distance:
            assertEquals(hits.score(y) < hits.score(y - 1), true);
          }
        }
      }
    }
  }
Exemplo n.º 9
0
 public void computeMAP(ImageSearcher searcher, String prefix, IndexReader reader)
     throws IOException {
   Pattern p = Pattern.compile("([0-9]+).jpg");
   double map = 0;
   double errorRate = 0d;
   double precision10 = 0d;
   double[] pr10cat = new double[10];
   double[] pr10cnt = new double[10];
   for (int i = 0; i < pr10cat.length; i++) {
     pr10cat[i] = 0d;
     pr10cnt[i] = 0d;
   }
   long sum = 0, ms = 0;
   for (int i = 0; i < sampleQueries.length; i++) {
     int id = sampleQueries[i];
     String file = testExtensive + "/" + id + ".jpg";
     ms = System.currentTimeMillis();
     ImageSearchHits hits = searcher.search(findDoc(reader, id + ".jpg"), reader);
     sum += (System.currentTimeMillis() - ms);
     int goodOnes = 0;
     double avgPrecision = 0d;
     double precision10temp = 0d;
     int countResults = 0;
     for (int j = 0; j < hits.length(); j++) {
       Document d = hits.doc(j);
       String hitsId = d.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
       Matcher matcher = p.matcher(hitsId);
       if (matcher.find()) hitsId = matcher.group(1);
       else fail("Did not get the number ...");
       int testID = Integer.parseInt(hitsId);
       if (testID != id) countResults++;
       if ((testID != id) && ((int) Math.floor(id / 100) == (int) Math.floor(testID / 100))) {
         goodOnes++;
         // Only if there is a change in recall
         avgPrecision += (double) goodOnes / (double) countResults;
         //                    System.out.print("x");
         if (j <= 10) {
           precision10temp += 1d;
         }
       } else {
         if (j == 1) { // error rate
           errorRate++;
         }
       }
     } // end for loop iterating results.
     //            if (avgPrecision<=0) {
     //                System.out.println("avgPrecision = " + avgPrecision);
     //                System.out.println("goodOnes = " + goodOnes);
     //            }
     assertTrue("Check if average precision is > 0", avgPrecision > 0);
     assertTrue("Check if goodOnes is > 0", goodOnes > 0);
     avgPrecision = avgPrecision / goodOnes;
     precision10 += precision10temp / 10d;
     // precision @ 10 for each category ...
     pr10cat[(int) Math.floor(id / 100)] += precision10temp / 10d;
     pr10cnt[(int) Math.floor(id / 100)] += 1d;
     map += avgPrecision;
   }
   map = map / sampleQueries.length;
   errorRate = errorRate / sampleQueries.length;
   precision10 = precision10 / sampleQueries.length;
   System.out.print(prefix + "\t");
   System.out.print(String.format("%.5f", map) + '\t');
   System.out.print(String.format("%.5f", precision10) + '\t');
   System.out.print(String.format("%.5f", errorRate) + '\t');
   // [email protected] per category
   for (int i = 0; i < pr10cat.length; i++) {
     double v = 0;
     if (pr10cnt[i] > 0) v = pr10cat[i] / pr10cnt[i];
     //            System.out.print(i + ": ");
     System.out.printf("%.5f\t", v);
   }
   System.out.printf("%2.3f\t", (double) sum / (double) sampleQueries.length);
   System.out.println();
 }