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.");
}
}
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.");
}
}
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;
}
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);
}
Aggregations