use of com.ibm.watson.visual_recognition.v3.model.ClassifyOptions in project java-sdk by watson-developer-cloud.
the class VisualRecognitionTest method testClassifyWOptions.
@Test
public void testClassifyWOptions() throws Throwable {
// Schedule some responses.
String mockResponseBody = "{\"custom_classes\": 13, \"images_processed\": 15, \"images\": [{\"source_url\": \"sourceUrl\", \"resolved_url\": \"resolvedUrl\", \"image\": \"image\", \"error\": {\"code\": 4, \"description\": \"description\", \"error_id\": \"errorId\"}, \"classifiers\": [{\"name\": \"name\", \"classifier_id\": \"classifierId\", \"classes\": [{\"class\": \"xClass\", \"score\": 0, \"type_hierarchy\": \"typeHierarchy\"}]}]}], \"warnings\": [{\"warning_id\": \"warningId\", \"description\": \"description\"}]}";
String classifyPath = "/v3/classify";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
constructClientService();
// Construct an instance of the ClassifyOptions model
ClassifyOptions classifyOptionsModel = new ClassifyOptions.Builder().imagesFile(TestUtilities.createMockStream("This is a mock file.")).imagesFilename("testString").imagesFileContentType("testString").url("testString").threshold(Float.valueOf("36.0")).owners(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).classifierIds(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).acceptLanguage("en").build();
// Invoke operation with valid options model (positive test)
Response<ClassifiedImages> response = visualRecognitionService.classify(classifyOptionsModel).execute();
assertNotNull(response);
ClassifiedImages responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "POST");
// Check query
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
// Get query params
assertEquals(query.get("version"), "testString");
// Check request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, classifyPath);
}
use of com.ibm.watson.visual_recognition.v3.model.ClassifyOptions in project java-sdk by watson-developer-cloud.
the class VisualRecognitionIT method testClassifyImagesFromBytes.
/**
* Test classify a single jpg image.
*
* @throws IOException Signals that an I/O exception has occurred.
*/
@Test
public void testClassifyImagesFromBytes() throws IOException {
InputStream imagesStream = new FileInputStream(SINGLE_IMAGE_FILE);
ClassifyOptions options = new ClassifyOptions.Builder().imagesFile(imagesStream).imagesFilename("car.png").build();
ClassifiedImages result = service.classify(options).execute().getResult();
assertClassifyImage(result, options);
}
use of com.ibm.watson.visual_recognition.v3.model.ClassifyOptions in project java-sdk by watson-developer-cloud.
the class VisualRecognitionIT method testClassifyImagesFromUrl.
/**
* Test classify images from url.
*/
@Test
public void testClassifyImagesFromUrl() {
ClassifyOptions options = new ClassifyOptions.Builder().url(IMAGE_URL).build();
ClassifiedImages result = service.classify(options).execute().getResult();
assertClassifyImage(result, options);
}
use of com.ibm.watson.visual_recognition.v3.model.ClassifyOptions in project jbpm-work-items by kiegroup.
the class ClassifyImageWorkitemHandler method executeWorkItem.
public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
try {
RequiredParameterValidator.validate(this.getClass(), workItem);
Document classificationImage = (Document) workItem.getParameter("ImageToClassify");
Map<String, Object> widResults = new HashMap<String, Object>();
VisualRecognition service = auth.getService(apiKey);
ByteArrayInputStream imageStream = new ByteArrayInputStream(classificationImage.getContent());
ClassifyOptions classifyOptions = new ClassifyOptions.Builder().imagesFile(imageStream).imagesFilename(classificationImage.getName()).parameters("{\"owners\": [\"me\"]}").build();
ClassifiedImage result = service.classify(classifyOptions).execute().getImages().get(0);
if (result.getError() != null) {
ErrorInfo errorInfo = result.getError();
logger.error("Error classifying image: " + errorInfo.getDescription());
workItemManager.abortWorkItem(workItem.getId());
} else {
List<ImageClassificationResult> resultList = new ArrayList<>();
for (ClassifierResult classification : result.getClassifiers()) {
for (ClassResult classResult : classification.getClasses()) {
resultList.add(new ImageClassificationResult(classification, classResult));
}
widResults.put(RESULT_VALUE, resultList);
}
workItemManager.completeWorkItem(workItem.getId(), widResults);
}
} catch (Exception e) {
handleException(e);
}
}
use of com.ibm.watson.visual_recognition.v3.model.ClassifyOptions in project java-sdk by watson-developer-cloud.
the class NaturalLanguageClassifierExample method main.
public static void main(String[] args) {
NaturalLanguageClassifier service = new NaturalLanguageClassifier();
service.setUsernameAndPassword("<username>", "<password>");
ClassifyOptions classifyOptions = new ClassifyOptions.Builder().classifierId("<classifierId>").text("Is it sunny?").build();
Classification classification = service.classify(classifyOptions).execute();
System.out.println(classification);
}
Aggregations