use of org.jbpm.process.workitem.ibm.watson.result.FaceDetectionResult in project jbpm-work-items by kiegroup.
the class DetectFacesWorkitemHandler method executeWorkItem.
public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
Document detectionImage = (Document) workItem.getParameter("ImageToDetect");
Map<String, Object> widResults = new HashMap<String, Object>();
if (detectionImage != null) {
try {
VisualRecognition service = auth.getService(apiKey);
ByteArrayInputStream imageStream = new ByteArrayInputStream(detectionImage.getContent());
DetectFacesOptions detectFacesOptions = new DetectFacesOptions.Builder().imagesFile(imageStream).build();
DetectedFaces result = service.detectFaces(detectFacesOptions).execute();
if (result == null || result.getImages() == null || result.getImages().size() < 1) {
logger.error("Unable to detect faces on provided image.");
workItemManager.abortWorkItem(workItem.getId());
} else {
List<FaceDetectionResult> resultList = new ArrayList<>();
for (ImageWithFaces imageWithFaces : result.getImages()) {
for (Face face : imageWithFaces.getFaces()) {
resultList.add(new FaceDetectionResult(imageWithFaces, face));
}
}
widResults.put(RESULT_VALUE, resultList);
workItemManager.completeWorkItem(workItem.getId(), widResults);
}
} catch (Exception e) {
handleException(e);
}
} else {
logger.error("Missing image for face detection.");
throw new IllegalArgumentException("Missing image for face detection.");
}
}
use of org.jbpm.process.workitem.ibm.watson.result.FaceDetectionResult in project jbpm-work-items by kiegroup.
the class WatsonWorkitemHandlerTest method testDetectFaces.
@Test
public void testDetectFaces() throws Exception {
when(auth.getService(anyString())).thenReturn(recognitionService);
TestWorkItemManager manager = new TestWorkItemManager();
DocumentImpl imagetoDetect = new DocumentImpl();
imagetoDetect.setName("testImageToDetect.png");
imagetoDetect.setContent(new String("testImageContent").getBytes());
WorkItemImpl workItem = new WorkItemImpl();
workItem.setParameter("ImageToDetect", imagetoDetect);
DetectFacesWorkitemHandler handler = new DetectFacesWorkitemHandler("{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("Detection") instanceof List);
List<FaceDetectionResult> returnValues = (List<FaceDetectionResult>) (manager.getResults().get(workItem.getId())).get("Detection");
assertNotNull(returnValues);
assertEquals(1, returnValues.size());
FaceDetectionResult result = returnValues.get(0);
assertTrue(result.getMinAge() == 20);
assertTrue(result.getMaxAge() == 35);
assertEquals("male", result.getGender());
assertEquals("testPerson", result.getIdentity());
}
Aggregations