Search in sources :

Example 1 with VisualRecognition

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

use of com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition 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 3 with VisualRecognition

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

the class WatsonAuth method getService.

public VisualRecognition getService(String apiKey) {
    VisualRecognition service = new VisualRecognition(VisualRecognition.VERSION_DATE_2016_05_20);
    service.setApiKey(apiKey);
    return service;
}
Also used : VisualRecognition(com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition)

Example 4 with VisualRecognition

use of com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition in project java-sdk by watson-developer-cloud.

the class VisualRecognitionExample method main.

public static void main(String[] args) {
    VisualRecognition service = new VisualRecognition("2016-05-20");
    service.setApiKey("<api-key>");
    System.out.println("Classify an image");
    ClassifyOptions options = new ClassifyOptions.Builder().imagesFile(new File("src/test/resources/visual_recognition/car.png")).imagesFilename("car.png").build();
    ClassifiedImages result = service.classify(options).execute();
    System.out.println(result);
    System.out.println("Create a classifier with positive and negative images");
    CreateClassifierOptions createOptions = new CreateClassifierOptions.Builder().name("foo").addClass("car", new File("src/test/resources/visual_recognition/car_positive.zip")).addClass("baseball", new File("src/test/resources/visual_recognition/baseball_positive.zip")).negativeExamples(new File("src/test/resources/visual_recognition/negative.zip")).build();
    Classifier foo = service.createClassifier(createOptions).execute();
    System.out.println(foo);
    System.out.println("Classify using the 'Car' classifier");
    options = new ClassifyOptions.Builder().imagesFile(new File("src/test/resources/visual_recognition/car.png")).imagesFilename("car.png").addClassifierId(foo.getClassifierId()).build();
    result = service.classify(options).execute();
    System.out.println(result);
    System.out.println("Update a classifier with more positive images");
    UpdateClassifierOptions updateOptions = new UpdateClassifierOptions.Builder().classifierId(foo.getClassifierId()).addClass("car", new File("src/test/resources/visual_recognition/car_positive.zip")).build();
    Classifier updatedFoo = service.updateClassifier(updateOptions).execute();
    System.out.println(updatedFoo);
}
Also used : CreateClassifierOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.CreateClassifierOptions) ClassifyOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions) UpdateClassifierOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.UpdateClassifierOptions) Classifier(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier) ClassifiedImages(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImages) File(java.io.File)

Aggregations

VisualRecognition (com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition)3 ClassifyOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Document (org.jbpm.document.Document)2 ClassResult (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassResult)1 ClassifiedImage (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImage)1 ClassifiedImages (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImages)1 Classifier (com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier)1 ClassifierResult (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifierResult)1 CreateClassifierOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.CreateClassifierOptions)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 ErrorInfo (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ErrorInfo)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 UpdateClassifierOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.UpdateClassifierOptions)1 File (java.io.File)1 FaceDetectionResult (org.jbpm.process.workitem.ibm.watson.result.FaceDetectionResult)1