Search in sources :

Example 1 with FaceDetectionResult

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.");
    }
}
Also used : FaceDetectionResult(org.jbpm.process.workitem.ibm.watson.result.FaceDetectionResult) HashMap(java.util.HashMap) VisualRecognition(com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition) ArrayList(java.util.ArrayList) DetectedFaces(com.ibm.watson.developer_cloud.visual_recognition.v3.model.DetectedFaces) Document(org.jbpm.document.Document) ImageWithFaces(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ImageWithFaces) DetectFacesOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.DetectFacesOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) Face(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Face)

Example 2 with FaceDetectionResult

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());
}
Also used : FaceDetectionResult(org.jbpm.process.workitem.ibm.watson.result.FaceDetectionResult) TestWorkItemManager(org.jbpm.process.workitem.core.TestWorkItemManager) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) ArrayList(java.util.ArrayList) List(java.util.List) DocumentImpl(org.jbpm.document.service.impl.DocumentImpl) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)2 FaceDetectionResult (org.jbpm.process.workitem.ibm.watson.result.FaceDetectionResult)2 VisualRecognition (com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition)1 DetectFacesOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.DetectFacesOptions)1 DetectedFaces (com.ibm.watson.developer_cloud.visual_recognition.v3.model.DetectedFaces)1 Face (com.ibm.watson.developer_cloud.visual_recognition.v3.model.Face)1 ImageWithFaces (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ImageWithFaces)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 HashMap (java.util.HashMap)1 List (java.util.List)1 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)1 Document (org.jbpm.document.Document)1 DocumentImpl (org.jbpm.document.service.impl.DocumentImpl)1 TestWorkItemManager (org.jbpm.process.workitem.core.TestWorkItemManager)1 Test (org.junit.Test)1