use of org.jbpm.process.workitem.ibm.watson.result.ImageClassificationResult in project jbpm-work-items by kiegroup.
the class ClassifyImageWorkitemHandler method executeWorkItem.
public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
Document classificationImage = (Document) workItem.getParameter("ImageToClassify");
Map<String, Object> widResults = new HashMap<String, Object>();
if (classificationImage != null) {
try {
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);
}
} else {
logger.error("Missing image for classification.");
throw new IllegalArgumentException("Missing image for classification.");
}
}
use of org.jbpm.process.workitem.ibm.watson.result.ImageClassificationResult in project jbpm-work-items by kiegroup.
the class WatsonWorkitemHandlerTest method testClassifyImage.
@Test
public void testClassifyImage() throws Exception {
when(auth.getService(anyString())).thenReturn(associationService);
TestWorkItemManager manager = new TestWorkItemManager();
DocumentImpl imageToClassify = new DocumentImpl();
imageToClassify.setName("testImageToClassify.png");
imageToClassify.setContent(new String("testImageContent").getBytes());
WorkItemImpl workItem = new WorkItemImpl();
workItem.setParameter("ImageToClassify", imageToClassify);
ClassifyImageWorkitemHandler handler = new ClassifyImageWorkitemHandler("{testApiKey}");
handler.setAuth(auth);
handler.executeWorkItem(workItem, manager);
assertNotNull(manager.getResults());
assertEquals(1, manager.getResults().size());
assertTrue(manager.getResults().containsKey(workItem.getId()));
assertTrue((manager.getResults().get(workItem.getId())).get("Classification") instanceof List);
List<ImageClassificationResult> returnValues = (List<ImageClassificationResult>) (manager.getResults().get(workItem.getId())).get("Classification");
assertNotNull(returnValues);
assertEquals(1, returnValues.size());
ImageClassificationResult result = returnValues.get(0);
assertTrue(result.getClassScore() == 1);
assertEquals("testClassName", result.getClassName());
assertEquals("testTypeHierarchy", result.getClassTypeHierarchy());
}
Aggregations