Search in sources :

Example 1 with ImageClassificationResult

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.");
    }
}
Also used : HashMap(java.util.HashMap) VisualRecognition(com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition) ClassifyOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions) ErrorInfo(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ErrorInfo) ArrayList(java.util.ArrayList) Document(org.jbpm.document.Document) ImageClassificationResult(org.jbpm.process.workitem.ibm.watson.result.ImageClassificationResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ClassifiedImage(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImage) ClassifierResult(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifierResult) ClassResult(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassResult)

Example 2 with ImageClassificationResult

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());
}
Also used : ImageClassificationResult(org.jbpm.process.workitem.ibm.watson.result.ImageClassificationResult) 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 ImageClassificationResult (org.jbpm.process.workitem.ibm.watson.result.ImageClassificationResult)2 VisualRecognition (com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition)1 ClassResult (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassResult)1 ClassifiedImage (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImage)1 ClassifierResult (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifierResult)1 ClassifyOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions)1 ErrorInfo (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ErrorInfo)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