Skip to content

Commit

Permalink
update to Lire-1.0b2
Browse files Browse the repository at this point in the history
  • Loading branch information
zengde committed Mar 8, 2016
1 parent b0fc1a5 commit 3ef7dd0
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 38 deletions.
Binary file added libs/lire.jar
Binary file not shown.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@
</exclusions>
</dependency>

<dependency>
<groupId>com.github.kzwang</groupId>
<artifactId>lire</artifactId>
<version>0.9.4-kzwang-beta1</version>
</dependency>

<dependency>
<groupId>com.twelvemonkeys.imageio</groupId> <!-- Some jpg images failed to parse by standard Java ImageIO, this plugin will fix that -->
<artifactId>imageio-jpeg</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.elasticsearch.index.mapper.image;


import net.semanticmetadata.lire.imageanalysis.*;
import net.semanticmetadata.lire.imageanalysis.joint.JointHistogram;
import net.semanticmetadata.lire.imageanalysis.features.LireFeature;
import net.semanticmetadata.lire.imageanalysis.features.global.*;
import net.semanticmetadata.lire.imageanalysis.features.global.joint.*;
import net.semanticmetadata.lire.imageanalysis.features.global.centrist.*;
import net.semanticmetadata.lire.imageanalysis.features.global.spatialpyramid.*;

/**
* Features supported by LIRE
Expand All @@ -22,12 +25,23 @@ public enum FeatureEnum {
JOINT_HISTOGRAM(JointHistogram.class),
JPEG_COEFFICIENT_HISTOGRAM(JpegCoefficientHistogram.class),
LOCAL_BINARY_PATTERNS(LocalBinaryPatterns.class),
LOCAL_BINARY_PATTERNS_AND_OPPONENT(LocalBinaryPatternsAndOpponent.class),
LUMINANCE_LAYOUT(LuminanceLayout.class),
OPPONENT_HISTOGRAM(OpponentHistogram.class),
PHOG(PHOG.class),
ROTATION_INVARIANT_LOCAL_BINARY_PATTERNS(RotationInvariantLocalBinaryPatterns.class),
SCALABLE_COLOR(ScalableColor.class),
TAMURA(Tamura.class),
FUZZY_COLOR_HISTOGRAM(FuzzyColorHistogram.class),
FUZZY_OPPONENT_HISTOGRAM(FuzzyOpponentHistogram.class),
RANK_AND_OPPONENT(RankAndOpponent.class),
SIMPLE_CENTRIST(SimpleCentrist.class),
SPACC(SPACC.class),
SPATIAL_PYRAMID_CENTRIST(SpatialPyramidCentrist.class),
SPCEDD(SPCEDD.class),
SPFCTH(SPFCTH.class),
SPJCD(SPJCD.class),
SPLBP(SPLBP.class)
;

private Class<? extends LireFeature> featureClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import net.semanticmetadata.lire.imageanalysis.LireFeature;
import net.semanticmetadata.lire.indexing.hashing.BitSampling;
import net.semanticmetadata.lire.indexing.hashing.LocalitySensitiveHashing;
import net.semanticmetadata.lire.imageanalysis.features.Extractor;
import net.semanticmetadata.lire.imageanalysis.features.LireFeature;
import net.semanticmetadata.lire.indexers.hashing.BitSampling;
import net.semanticmetadata.lire.indexers.hashing.LocalitySensitiveHashing;
import net.semanticmetadata.lire.utils.ImageUtils;
import net.semanticmetadata.lire.utils.SerializationUtils;
import org.apache.lucene.document.BinaryDocValuesField;
Expand Down Expand Up @@ -302,7 +303,7 @@ public Mapper parse(ParseContext context) throws IOException {
lireFeature = featureExtractMap.get(featureEnum);
} else {
lireFeature = featureEnum.getFeatureClass().newInstance();
lireFeature.extract(img);
((Extractor)lireFeature).extract(img);
}
byte[] parsedContent = lireFeature.getByteArrayRepresentation();

Expand All @@ -317,9 +318,9 @@ public Mapper parse(ParseContext context) throws IOException {
HashEnum hashEnum = HashEnum.valueOf(h);
int[] hashVals = null;
if (hashEnum.equals(HashEnum.BIT_SAMPLING)) {
hashVals = BitSampling.generateHashes(lireFeature.getDoubleHistogram());
hashVals = BitSampling.generateHashes(lireFeature.getFeatureVector());
} else if (hashEnum.equals(HashEnum.LSH)) {
hashVals = LocalitySensitiveHashing.generateHashes(lireFeature.getDoubleHistogram());
hashVals = LocalitySensitiveHashing.generateHashes(lireFeature.getFeatureVector());
}
String mapperName = featureEnum.name() + "." + HASH + "." + h;
FieldMapper hashMapper = hashMappers.get(mapperName) ;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.elasticsearch.index.query.image;

import net.semanticmetadata.lire.imageanalysis.LireFeature;
import net.semanticmetadata.lire.imageanalysis.features.LireFeature;
import org.apache.lucene.index.BinaryDocValues;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReader;
Expand Down Expand Up @@ -45,7 +45,7 @@ public float score() throws IOException {
LireFeature docFeature = lireFeature.getClass().newInstance();
docFeature.setByteArrayRepresentation(bytesRef.bytes);

float distance = lireFeature.getDistance(docFeature);
float distance = (float) lireFeature.getDistance(docFeature);
float score;
if (Float.compare(distance, 1.0f) <= 0) { // distance less than 1, consider as same image
score = 2f - distance;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.elasticsearch.index.query.image;

import net.semanticmetadata.lire.imageanalysis.LireFeature;
import net.semanticmetadata.lire.imageanalysis.features.LireFeature;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.search.similarities.DefaultSimilarity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.List;
import java.util.Set;

import net.semanticmetadata.lire.imageanalysis.LireFeature;
import net.semanticmetadata.lire.imageanalysis.features.LireFeature;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.util.Bits;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package org.elasticsearch.index.query.image;

import net.semanticmetadata.lire.AbstractImageSearcher;
import net.semanticmetadata.lire.imageanalysis.LireFeature;
import net.semanticmetadata.lire.imageanalysis.features.LireFeature;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.*;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.ToStringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


/**
* Copied from {@link MatchAllDocsQuery}, calculate score for all docs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.elasticsearch.index.query.image;


import net.semanticmetadata.lire.imageanalysis.LireFeature;
import net.semanticmetadata.lire.indexing.hashing.BitSampling;
import net.semanticmetadata.lire.indexing.hashing.LocalitySensitiveHashing;
import net.semanticmetadata.lire.imageanalysis.features.Extractor;
import net.semanticmetadata.lire.imageanalysis.features.LireFeature;
import net.semanticmetadata.lire.indexers.hashing.BitSampling;
import net.semanticmetadata.lire.indexers.hashing.LocalitySensitiveHashing;
import net.semanticmetadata.lire.utils.ImageUtils;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
Expand Down Expand Up @@ -119,7 +120,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
if (Math.max(img.getHeight(), img.getWidth()) > ImageMapper.MAX_IMAGE_DIMENSION) {
img = ImageUtils.scaleImage(img, ImageMapper.MAX_IMAGE_DIMENSION);
}
feature.extract(img);
((Extractor)feature).extract(img);
} catch (Exception e) {
throw new ElasticsearchImageProcessException("Failed to parse image", e);
}
Expand Down Expand Up @@ -149,9 +150,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
} else { // query by hash first
int[] hash = null;
if (hashEnum.equals(HashEnum.BIT_SAMPLING)) {
hash = BitSampling.generateHashes(feature.getDoubleHistogram());
hash = BitSampling.generateHashes(feature.getFeatureVector());
} else if (hashEnum.equals(HashEnum.LSH)) {
hash = LocalitySensitiveHashing.generateHashes(feature.getDoubleHistogram());
hash = LocalitySensitiveHashing.generateHashes(feature.getFeatureVector());
}
String hashFieldName = luceneFieldName + "." + ImageMapper.HASH + "." + hashEnum.name();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void test_index_search_image() throws Exception {
.get();
assertNoFailures(searchResponse);
SearchHits hits = searchResponse.getHits();
assertThat("Should match at least one image", hits.getTotalHits(), greaterThanOrEqualTo(1l)); // if using hash, total result maybe different than number of images
assertThat("Should match at least one image", hits.getTotalHits(), greaterThanOrEqualTo(1L)); // if using hash, total result maybe different than number of images
SearchHit hit = hits.getHits()[0];
assertThat("First should be exact match and has score 1", hit.getScore(), equalTo(2.0f));
assertImageScore(hits, nameToSearch, 2.0f);
Expand All @@ -130,7 +130,7 @@ public void test_index_search_image() throws Exception {
SearchResponse searchResponse3 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE_NAME).setQuery(boolQueryBuilder).setSize(totalImages).get();
assertNoFailures(searchResponse3);
SearchHits hits3 = searchResponse3.getHits();
assertThat("Should match one document only", hits3.getTotalHits(), equalTo(1l)); // added filename to query, should have only one result
assertThat("Should match one document only", hits3.getTotalHits(), equalTo(1L)); // added filename to query, should have only one result
SearchHit hit3 = hits3.getHits()[0];
assertThat((String)hit3.getSource().get("name"), equalTo(nameToSearch));

Expand All @@ -139,7 +139,7 @@ public void test_index_search_image() throws Exception {
SearchResponse searchResponse4 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE_NAME).setQuery(imageQueryBuilder4).setSize(totalImages).get();
assertNoFailures(searchResponse4);
SearchHits hits4 = searchResponse4.getHits();
assertThat("Should match at least one image", hits4.getTotalHits(), greaterThanOrEqualTo(1l)); // if using hash, total result maybe different than number of images
assertThat("Should match at least one image", hits4.getTotalHits(), greaterThanOrEqualTo(1L)); // if using hash, total result maybe different than number of images
SearchHit hit4 = hits4.getHits()[0];
assertThat("First should be exact match and has score 1", hit4.getScore(), equalTo(2.0f));
assertImageScore(hits4, nameToSearch, 2.0f);
Expand All @@ -149,7 +149,7 @@ public void test_index_search_image() throws Exception {
SearchResponse searchResponse5 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE_NAME).setQuery(termQueryBuilder).setSize(totalImages).get();
assertNoFailures(searchResponse5);
SearchHits hits5 = searchResponse5.getHits();
assertThat("Should match at least one record", hits5.getTotalHits(), greaterThanOrEqualTo(1l)); // if using hash, total result maybe different than number of images
assertThat("Should match at least one record", hits5.getTotalHits(), greaterThanOrEqualTo(1L)); // if using hash, total result maybe different than number of images

// test search with exist image
ImageQueryBuilder imageQueryBuilder6 = new ImageQueryBuilder("img").feature(FeatureEnum.CEDD.name()).lookupIndex(INDEX_NAME).lookupType(DOC_TYPE_NAME).lookupId(idToSearch).lookupPath("img");
Expand Down Expand Up @@ -193,8 +193,7 @@ private byte[] getRandomImage() throws IOException, ImageWriteException {
}
}
ImageFormat format = ImageFormat.IMAGE_FORMAT_TIFF;
byte[] bytes = Sanselan.writeImageToBytes(image, format, Maps.newHashMap());
return bytes;
return Sanselan.writeImageToBytes(image, format, Maps.newHashMap());
}

public String copyToStringFromClasspath(String path) throws IOException {
Expand Down

0 comments on commit 3ef7dd0

Please sign in to comment.