-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Augugrumi/dev
Version 2.0.0 ready
- Loading branch information
Showing
25 changed files
with
915 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/it/polpetta/libris/image/ibm/IBMAbstractFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package it.polpetta.libris.image.ibm; | ||
|
||
import it.polpetta.libris.contract.IQueryBuilder; | ||
import it.polpetta.libris.image.ibm.contract.IAbstractIBMImageFactoryReverseSearchProvider; | ||
import it.polpetta.libris.image.ibm.visualRecognition.URLIBMImageSearcher; | ||
|
||
/** | ||
* Created by dpolonio on 17/05/17. | ||
*/ | ||
public class IBMAbstractFactory implements IAbstractIBMImageFactoryReverseSearchProvider { | ||
|
||
private String subscriptionKey = null; | ||
|
||
public IBMAbstractFactory(String subscriptionKey) { | ||
|
||
this.subscriptionKey = subscriptionKey; | ||
} | ||
|
||
@Override | ||
public URLIBMImageSearcher.Builder imageSearchBuildQuery() { | ||
URLIBMImageSearcher.setSubscriptionKey(subscriptionKey); | ||
return new URLIBMImageSearcher.Builder(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
.../it/polpetta/libris/image/ibm/contract/IAbstractIBMImageFactoryReverseSearchProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package it.polpetta.libris.image.ibm.contract; | ||
|
||
import it.polpetta.libris.image.contract.IAbstractImageFactoryReverseSearchProvider; | ||
import it.polpetta.libris.image.ibm.visualRecognition.URLIBMImageSearcher; | ||
|
||
/** | ||
* Created by dpolonio on 17/05/17. | ||
*/ | ||
public interface IAbstractIBMImageFactoryReverseSearchProvider extends IAbstractImageFactoryReverseSearchProvider { | ||
|
||
URLIBMImageSearcher.Builder imageSearchBuildQuery(); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/it/polpetta/libris/image/ibm/contract/IIBMImageSearchResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package it.polpetta.libris.image.ibm.contract; | ||
|
||
|
||
import it.polpetta.libris.image.contract.IImageSearchResult; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* Created by dpolonio on 17/05/17. | ||
*/ | ||
public interface IIBMImageSearchResult extends IImageSearchResult { | ||
ArrayList<String> getTags(); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/it/polpetta/libris/image/ibm/contract/IIBMImageSearcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package it.polpetta.libris.image.ibm.contract; | ||
|
||
import it.polpetta.libris.contract.ISearcher; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Created by dpolonio on 17/05/17. | ||
*/ | ||
public interface IIBMImageSearcher extends ISearcher { | ||
|
||
IIBMImageSearchResult search() throws IOException; | ||
} |
89 changes: 89 additions & 0 deletions
89
src/main/java/it/polpetta/libris/image/ibm/visualRecognition/IBMImageSearchResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package it.polpetta.libris.image.ibm.visualRecognition; | ||
|
||
import it.polpetta.libris.image.azure.contract.IAzureImageSearchResult; | ||
import it.polpetta.libris.image.ibm.contract.IIBMImageSearchResult; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* Created by dpolonio on 10/05/17. | ||
*/ | ||
public class IBMImageSearchResult implements IIBMImageSearchResult { | ||
|
||
private String bestGuess; | ||
private ArrayList<String> tags; | ||
|
||
private IBMImageSearchResult( | ||
String bestGuess, | ||
ArrayList<String> tags | ||
) { | ||
this.bestGuess = bestGuess; | ||
this.tags = tags; | ||
} | ||
|
||
// TODO evaluate the possibility to introduce constants instead of using explicitly strings | ||
@Override | ||
public String toJSONString() { | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append("{") | ||
.append("\"best_guess\":\"") | ||
.append(bestGuess) | ||
.append("\",") | ||
.append("\"tags\":[") | ||
.append(arrayListToString(tags)) | ||
.append("]}"); | ||
return builder.toString(); | ||
} | ||
|
||
private String arrayListToString(ArrayList<String> arrayList) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < arrayList.size(); i++) { | ||
sb.append("\""); | ||
sb.append(arrayList.get(i)); | ||
sb.append("\""); | ||
if (i != arrayList.size() - 1) | ||
sb.append(","); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
@Override | ||
public String getBestGuess() { | ||
return bestGuess; | ||
} | ||
|
||
@Override | ||
public ArrayList<String> getTags() { | ||
return tags; | ||
} | ||
|
||
|
||
public static class Builder { | ||
|
||
private String bestGuess; | ||
private ArrayList<String> tags; | ||
|
||
public Builder () { | ||
this.bestGuess = ""; | ||
this.tags = new ArrayList<>(); | ||
} | ||
|
||
public IBMImageSearchResult.Builder addBestGuess(String bestGuess) { | ||
this.bestGuess = bestGuess; | ||
return this; | ||
} | ||
|
||
public IBMImageSearchResult.Builder addTags(ArrayList<String> tags) { | ||
this.tags = tags; | ||
return this; | ||
} | ||
|
||
public IBMImageSearchResult build() { | ||
return new IBMImageSearchResult( | ||
bestGuess, | ||
tags | ||
); | ||
} | ||
} | ||
|
||
} |
112 changes: 112 additions & 0 deletions
112
src/main/java/it/polpetta/libris/image/ibm/visualRecognition/URLIBMImageSearcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package it.polpetta.libris.image.ibm.visualRecognition; | ||
|
||
import com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition; | ||
import com.ibm.watson.developer_cloud.visual_recognition.v3.model.*; | ||
import it.polpetta.libris.contract.AbstractURLSearcher; | ||
import it.polpetta.libris.contract.IQueryBuilder; | ||
import it.polpetta.libris.image.azure.contract.IAzureImageSearcher; | ||
import it.polpetta.libris.image.azure.imageRecognition.URLAzureImageSearcher; | ||
import it.polpetta.libris.image.ibm.contract.IIBMImageSearcher; | ||
import it.polpetta.libris.image.ibm.contract.IIBMImageSearchResult; | ||
import it.polpetta.libris.util.Coordinates; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.util.ArrayList; | ||
|
||
/** | ||
* Created by dpolonio on 17/05/17. | ||
*/ | ||
public class URLIBMImageSearcher extends AbstractURLSearcher implements IIBMImageSearcher { | ||
|
||
private static String subscriptionKey = null; | ||
|
||
private VisualClassification response; | ||
|
||
public URLIBMImageSearcher (URL link) { | ||
super(link); | ||
|
||
} | ||
|
||
public static void setSubscriptionKey(String key) { | ||
subscriptionKey = key; | ||
} | ||
|
||
@Override | ||
protected URLConnection setConnectionParameters() { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected IIBMImageSearchResult parseResult(String nullString) { | ||
String bestGuess = null; | ||
ArrayList<String> tags = new ArrayList<>(); | ||
|
||
double score; | ||
double bestScore = 0; | ||
for (VisualClassifier classifier : | ||
this.response.getImages().get(0).getClassifiers()) { | ||
for (VisualClassifier.VisualClass visualClass : classifier.getClasses()) { | ||
score = visualClass.getScore(); | ||
if (score > bestScore) { | ||
if (bestGuess != null) | ||
tags.add(bestGuess); | ||
bestGuess = visualClass.getName(); | ||
bestScore = score; | ||
} else | ||
tags.add(visualClass.getName()); | ||
} | ||
} | ||
|
||
return new IBMImageSearchResult.Builder() | ||
.addBestGuess(bestGuess) | ||
.addTags(tags) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public IIBMImageSearchResult search() throws IOException { | ||
VisualRecognition service = | ||
new VisualRecognition(VisualRecognition.VERSION_DATE_2016_05_20, subscriptionKey); | ||
|
||
ClassifyImagesOptions options1 = new ClassifyImagesOptions.Builder() | ||
.url(link.toString()) | ||
.build(); | ||
response = service.classify(options1).execute(); | ||
|
||
return parseResult(""); | ||
} | ||
|
||
public static class Builder implements IQueryBuilder { | ||
|
||
private File photo = null; | ||
private URL link = null; | ||
private Coordinates location = null; | ||
|
||
public Builder(){} | ||
|
||
public URLIBMImageSearcher.Builder setLocation(float x, float y) { | ||
location = new Coordinates(x, y); | ||
return this; | ||
} | ||
|
||
@Override | ||
public URLIBMImageSearcher.Builder setImage(File file) { | ||
photo = file; | ||
return this; | ||
} | ||
|
||
@Override | ||
public URLIBMImageSearcher.Builder setImage(URL linkToImage) { | ||
link = linkToImage; | ||
return this; | ||
} | ||
|
||
@Override | ||
public URLIBMImageSearcher build() { | ||
return new URLIBMImageSearcher(link); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/it/polpetta/libris/image/imagga/ImaggaAbstractFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package it.polpetta.libris.image.imagga; | ||
|
||
import it.polpetta.libris.image.imagga.contract.IAbstractImaggaImageFactoryReverseSearchProvider; | ||
import it.polpetta.libris.image.imagga.imageRecognition.URLImaggaImageSearcher; | ||
|
||
/** | ||
* Created by federico on 17/05/17. | ||
*/ | ||
public class ImaggaAbstractFactory implements IAbstractImaggaImageFactoryReverseSearchProvider { | ||
private String subscriptionKey = null; | ||
|
||
public ImaggaAbstractFactory(String subscriptionKey) { | ||
|
||
this.subscriptionKey = subscriptionKey; | ||
} | ||
|
||
public URLImaggaImageSearcher.Builder imageSearchBuildQuery() { | ||
URLImaggaImageSearcher.setSubscriptionKey(subscriptionKey); | ||
return new URLImaggaImageSearcher.Builder(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...lpetta/libris/image/imagga/contract/IAbstractImaggaImageFactoryReverseSearchProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package it.polpetta.libris.image.imagga.contract; | ||
|
||
import it.polpetta.libris.image.contract.IAbstractImageFactoryReverseSearchProvider; | ||
import it.polpetta.libris.image.imagga.imageRecognition.URLImaggaImageSearcher; | ||
|
||
/** | ||
* Created by federico on 17/05/17. | ||
*/ | ||
public interface IAbstractImaggaImageFactoryReverseSearchProvider extends IAbstractImageFactoryReverseSearchProvider { | ||
URLImaggaImageSearcher.Builder imageSearchBuildQuery(); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/it/polpetta/libris/image/imagga/contract/IImaggaImageSearchResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package it.polpetta.libris.image.imagga.contract; | ||
|
||
import it.polpetta.libris.image.contract.IImageSearchResult; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* Created by federico on 17/05/17. | ||
*/ | ||
public interface IImaggaImageSearchResult extends IImageSearchResult { | ||
ArrayList<String> getOtherTags(); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/it/polpetta/libris/image/imagga/contract/IImaggaImageSearcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package it.polpetta.libris.image.imagga.contract; | ||
|
||
import it.polpetta.libris.contract.ISearcher; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Created by federico on 17/05/17. | ||
*/ | ||
public interface IImaggaImageSearcher extends ISearcher { | ||
IImaggaImageSearchResult search() throws IOException; | ||
} |
Oops, something went wrong.