Skip to content

Commit bfba5dc

Browse files
Merge pull request #56 from watson-developer-cloud/dev
Added support for Visual Insights
2 parents 6f6644c + 6ccdb15 commit bfba5dc

File tree

8 files changed

+387
-4
lines changed

8 files changed

+387
-4
lines changed

README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ Java code wrappers to quickly get started with the various [Watson Developer Clo
3434
* [Text to Speech](#text-to-speech)
3535
* [Tone Analyzer](#tone-analyzer)
3636
* [Tradeoff Analytics](#tradeoff-analytics)
37+
* [Visual Insights](#visual-insights)
3738
* [Visual Recognition](#visual-recognition)
38-
* [Concept Insights](#concept-insights)
3939
* [Android](#android)
4040
* [Build + Test](#build--test)
4141
* [Eclipse and Intellij](#working-with-eclipse-and-intellij-idea)
@@ -105,7 +105,7 @@ System.out.println(sentiment);
105105
```
106106

107107
### Alchemy Vision
108-
[Alchemy Vision][alchemy_vision] uses deep learning innovations to understand a picture's content and context. It sees complex visual scenes in their entirety —without needing any textual clues— leveraging a holistic approach to understanding the multiple objects and surroundings.
108+
[Alchemy Vision][alchemy_vision] uses deep learning innovations to understand a picture's content and context. It sees complex visual scenes in their entirety —without needing any textual clues— leveraging a holistic approach to understand the objects, faces, and words in an image.
109109

110110
Example: Extract keywords from an image.
111111

@@ -489,6 +489,24 @@ Dilemma dilemma = service.dilemmas(problem);
489489
System.out.println(dilemma);
490490
```
491491

492+
### Visual Insights
493+
Use the [Visual Insights][visual_insights] to get insight into the themes present in a collection of images based on their visual appearance/content.
494+
495+
496+
```java
497+
import com.ibm.watson.developer_cloud.visual_insights.v1.VisualInsights;
498+
import com.ibm.watson.developer_cloud.visual_insights.v1.model.Classifiers;
499+
import java.io.File;
500+
501+
VisualInsights service = new VisualInsights();
502+
service.setUsernameAndPassword("<username>", "<password>");
503+
504+
File images = new File("src/test/resources/images.zip");
505+
Summary summary = service.getSummary(images);
506+
507+
System.out.println(summary);
508+
```
509+
492510
### Visual Recognition
493511
Use the [Visual Recognition][visual_recognition] service to recognize the
494512
following picture.
@@ -586,6 +604,7 @@ See [CONTRIBUTING.md](CONTRIBUTING.md).
586604
[tone-analyzer]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/tone-analyzer/
587605
[dialog]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/dialog/
588606
[concept-insights]: https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/concept-insights/
607+
[visual_insights]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/visual-insights/
589608

590609
[alchemy_language]: http://www.alchemyapi.com/products/alchemylanguage
591610
[sentiment_analysis]: http://www.alchemyapi.com/products/alchemylanguage/sentiment-analysis
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.ibm.watson.developer_cloud.visual_insights.v1;
2+
3+
4+
import java.io.FileNotFoundException;
5+
import java.net.URISyntaxException;
6+
7+
import com.ibm.watson.developer_cloud.visual_insights.v1.model.Classifiers;
8+
9+
10+
public class VisualInsightsExample {
11+
12+
public static void main(String[] args) throws URISyntaxException, FileNotFoundException {
13+
VisualInsights service = new VisualInsights();
14+
service.setUsernameAndPassword("<username>", "<password>");
15+
16+
Classifiers classifiers = service.getClassifiers();
17+
18+
System.out.println(classifiers);
19+
}
20+
21+
}

src/main/java/com/ibm/watson/developer_cloud/service/Request.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.ArrayList;
2222
import java.util.List;
2323
import java.util.Locale;
24+
import java.util.Map;
2425

2526
import org.apache.http.HttpEntity;
2627
import org.apache.http.NameValuePair;
@@ -425,4 +426,35 @@ public Request withHeader(Object... args) {
425426
public Request withQuery(Object... args) {
426427
return with(queryParams, args);
427428
}
429+
430+
/**
431+
* Adds query parameters.
432+
*
433+
* @param parameters
434+
* a list of name-value query parameters
435+
*
436+
* @return this
437+
*/
438+
public Request withQuery(Map<String,Object> parameters) {
439+
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
440+
withQuery(entry.getKey(),entry.getValue());
441+
}
442+
return this;
443+
}
444+
445+
/**
446+
* Adds form parameters.
447+
*
448+
* @param parameters
449+
* a list of name-value form parameters
450+
*
451+
* @return this
452+
*/
453+
public Request withForm(Map<String,Object> parameters) {
454+
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
455+
withForm(entry.getKey(),entry.getValue());
456+
}
457+
return this;
458+
}
459+
428460
}

src/main/java/com/ibm/watson/developer_cloud/service/WatsonService.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ private URI buildRequestURI(HttpRequestBase request) {
122122
requestURL = getEndPoint() + request.getURI();
123123
return new URI(requestURL);
124124
} catch (URISyntaxException e) {
125-
log.log(Level.SEVERE, requestURL
126-
+ " could not be parsed as a URI reference");
125+
log.log(Level.SEVERE, requestURL + " could not be parsed as a URI reference");
127126
throw new RuntimeException(e);
128127
}
129128
}
@@ -396,6 +395,23 @@ protected void setAuthentication(HttpRequestBase request){
396395

397396
}
398397

398+
/**
399+
* Execute the request and return the POJO that represent the response.
400+
*
401+
* @param <T> The POJO that represents the response object
402+
* @param request the request
403+
* @param returnType the POJO class to be parsed from the response
404+
* @return the POJO object that represent the response
405+
*/
406+
protected <T> T executeRequest(Request request, Class<T> returnType) {
407+
HttpRequestBase requestBase = request.build();
408+
try {
409+
HttpResponse response = execute(requestBase);
410+
return ResponseUtil.getObject(response, returnType);
411+
} catch (IOException e) {
412+
throw new RuntimeException(e);
413+
}
414+
}
399415
/*
400416
* (non-Javadoc)
401417
*
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ibm.watson.developer_cloud.visual_insights.v1;
17+
18+
import java.io.File;
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
import org.apache.http.entity.mime.MultipartEntity;
23+
import org.apache.http.entity.mime.content.FileBody;
24+
25+
import com.ibm.watson.developer_cloud.service.Request;
26+
import com.ibm.watson.developer_cloud.service.WatsonService;
27+
import com.ibm.watson.developer_cloud.visual_insights.v1.model.Classifiers;
28+
import com.ibm.watson.developer_cloud.visual_insights.v1.model.Summary;
29+
30+
31+
/**
32+
* The IBM Watson Visual Insights gives insight into the themes present in a collection of images based on their visual appearance / content.
33+
*
34+
* @author Nizar Alseddeg (nmalsedd@us.ibm.com)
35+
* @version v1
36+
* @see <a
37+
* href="http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/visual-insights.html">
38+
* Visual Insights</a>
39+
*/
40+
public class VisualInsights extends WatsonService {
41+
42+
private static final String FILE = "file";
43+
44+
/**
45+
* The CLASSIFIERS_PATH. (value is "/classifiers")
46+
*/
47+
private static final String CLASSIFIERS_PATH = "/v1/classifiers";
48+
49+
/**
50+
* The SUMMARY_PATH. (value is "/summary")
51+
*/
52+
private static final String SUMMARY_PATH = "/v1/summary";
53+
54+
55+
/**
56+
* The Constant FILTER_NAME. (value is "filter_name")
57+
*/
58+
public static final String FILTER_NAME = "filter_name";
59+
60+
/**
61+
* The service url.
62+
* (value is "https://gateway.watsonplatform.net/visual-insights/api")
63+
*/
64+
private static final String URL = "https://gateway.watsonplatform.net/visual-insights-experimental/api";
65+
66+
/**
67+
* Instantiates a new visual insights service.
68+
*/
69+
public VisualInsights() {
70+
setEndPoint(URL);
71+
}
72+
73+
/**
74+
* Returns a summary of the collection's visual classifiers
75+
*
76+
* @return the Summary
77+
*/
78+
public Classifiers getClassifiers() {
79+
Request request = Request.Get(CLASSIFIERS_PATH);
80+
return executeRequest(request, Classifiers.class);
81+
}
82+
83+
/**
84+
* Returns a summary of the collection's visual classifiers, filtered by name
85+
*
86+
* @param filterName the images File
87+
* @return the Summary
88+
*/
89+
public Classifiers getClassifiers(final String filterName) {
90+
91+
Request request = Request.Get(CLASSIFIERS_PATH);
92+
93+
if(filterName!=null && !filterName.isEmpty()) {
94+
Map<String, Object> queryParameters = new HashMap<String, Object>();
95+
queryParameters.put(FILTER_NAME,filterName);
96+
request.withQuery(queryParameters);
97+
}
98+
99+
return executeRequest(request, Classifiers.class);
100+
}
101+
102+
/**
103+
* Upload a set of images as a zip file for visual insight extraction.
104+
*
105+
* @param imagesFile the images File
106+
* @return the Summary of the collection's visual attributes
107+
*/
108+
public Summary getSummary(final File imagesFile) {
109+
if (imagesFile == null || !imagesFile.exists())
110+
throw new IllegalArgumentException(
111+
"imagesFile can not be null or empty");
112+
113+
MultipartEntity reqEntity = new MultipartEntity();
114+
reqEntity.addPart(FILE, new FileBody(imagesFile));
115+
Request request = Request.Post(SUMMARY_PATH)
116+
.withEntity(reqEntity);
117+
118+
return executeRequest(request, Summary.class);
119+
120+
}
121+
122+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ibm.watson.developer_cloud.visual_insights.v1.model;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
import com.ibm.watson.developer_cloud.service.model.GenericModel;
22+
23+
/**
24+
* The Class Classifiers.
25+
*/
26+
public class Classifiers extends GenericModel {
27+
28+
/** The classifiers. */
29+
private List<Classifier> classifiers = new ArrayList<Classifier>();
30+
31+
/**
32+
* Gets the classifiers.
33+
*
34+
* @return the classifiers
35+
*/
36+
public List<Classifier> getClassifiers() {
37+
return classifiers;
38+
}
39+
40+
/**
41+
* Sets the classifiers.
42+
*
43+
* @param classifiers
44+
* the new classifiers
45+
*/
46+
public void setClassifiers(List<Classifier> classifiers) {
47+
this.classifiers = classifiers;
48+
}
49+
50+
public static class Classifier extends GenericModel {
51+
52+
private String name;
53+
54+
/**
55+
* Gets the name.
56+
*
57+
* @return the name
58+
*/
59+
public String getName() {
60+
return name;
61+
}
62+
63+
/**
64+
* Sets the name.
65+
*
66+
* @param name
67+
* the new name
68+
*/
69+
public void setName(String name) {
70+
this.name = name;
71+
}
72+
}
73+
74+
}

0 commit comments

Comments
 (0)