Search in sources :

Example 1 with ClassResult

use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassResult 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 ClassResult

use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassResult in project jbpm-work-items by kiegroup.

the class WatsonWorkitemHandlerTest method setUp.

// @Mock
// Face recognitionFace;
@Before
public void setUp() {
    // image classification
    ClassResult classResultObj = new ClassResult();
    classResultObj.setClassName("testClassName");
    classResultObj.setScore(new Float(1));
    classResultObj.setTypeHierarchy("testTypeHierarchy");
    List<ClassifiedImage> classifiedImageList = new ArrayList<>();
    classifiedImageList.add(associationClassifiedImage);
    List<ClassifierResult> classifierResults = new ArrayList<>();
    classifierResults.add(associationClassifierResult);
    List<ClassResult> classResults = new ArrayList<>();
    classResults.add(classResultObj);
    when(associationService.classify(any(ClassifyOptions.class))).thenReturn(associationServiceCall);
    when(associationServiceCall.execute()).thenReturn(assoiationClassifiedImages);
    when(assoiationClassifiedImages.getImages()).thenReturn(classifiedImageList);
    when(associationClassifiedImage.getClassifiers()).thenReturn(classifierResults);
    when(associationClassifierResult.getClasses()).thenReturn(classResults);
    when(associationClassifierResult.getClassifierId()).thenReturn("testClassifierId");
    // face detection
    List<ImageWithFaces> recognitionImageList = new ArrayList<>();
    recognitionImageList.add(recognitionImageWithFaces);
    FaceAge recognitionFaceAge = new FaceAge();
    recognitionFaceAge.setMin(20);
    recognitionFaceAge.setMax(35);
    FaceGender recognitionFaceGender = new FaceGender();
    recognitionFaceGender.setGender("male");
    FaceIdentity recognitionFaceIdentity = new FaceIdentity();
    recognitionFaceIdentity.setName("testPerson");
    Face recognitionFace = new Face();
    recognitionFace.setAge(recognitionFaceAge);
    recognitionFace.setGender(recognitionFaceGender);
    recognitionFace.setIdentity(recognitionFaceIdentity);
    List<Face> recognitionFaceList = new ArrayList<>();
    recognitionFaceList.add(recognitionFace);
    when(recognitionService.detectFaces(any(DetectFacesOptions.class))).thenReturn(recognitionServiceCall);
    when(recognitionServiceCall.execute()).thenReturn(recognitionDetectFaces);
    when(recognitionDetectFaces.getImages()).thenReturn(recognitionImageList);
    when(recognitionImageWithFaces.getFaces()).thenReturn(recognitionFaceList);
    when(recognitionImageWithFaces.getImage()).thenReturn("testImage");
}
Also used : ClassifyOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions) ArrayList(java.util.ArrayList) FaceGender(com.ibm.watson.developer_cloud.visual_recognition.v3.model.FaceGender) ImageWithFaces(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ImageWithFaces) DetectFacesOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.DetectFacesOptions) ClassifiedImage(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImage) ClassifierResult(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifierResult) Face(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Face) ClassResult(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassResult) FaceAge(com.ibm.watson.developer_cloud.visual_recognition.v3.model.FaceAge) FaceIdentity(com.ibm.watson.developer_cloud.visual_recognition.v3.model.FaceIdentity) Before(org.junit.Before)

Aggregations

ClassResult (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassResult)2 ClassifiedImage (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImage)2 ClassifierResult (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifierResult)2 ClassifyOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions)2 ArrayList (java.util.ArrayList)2 VisualRecognition (com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition)1 DetectFacesOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.DetectFacesOptions)1 ErrorInfo (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ErrorInfo)1 Face (com.ibm.watson.developer_cloud.visual_recognition.v3.model.Face)1 FaceAge (com.ibm.watson.developer_cloud.visual_recognition.v3.model.FaceAge)1 FaceGender (com.ibm.watson.developer_cloud.visual_recognition.v3.model.FaceGender)1 FaceIdentity (com.ibm.watson.developer_cloud.visual_recognition.v3.model.FaceIdentity)1 ImageWithFaces (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ImageWithFaces)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 HashMap (java.util.HashMap)1 Document (org.jbpm.document.Document)1 ImageClassificationResult (org.jbpm.process.workitem.ibm.watson.result.ImageClassificationResult)1 Before (org.junit.Before)1