use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions 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.model.ClassifyOptions in project java-sdk by watson-developer-cloud.
the class NaturalLanguageClassifierIT method dClassify.
/**
* Test classify. Use the pre created classifier to avoid waiting for availability
*/
@Test
public void dClassify() {
Classification classification = null;
try {
ClassifyOptions classifyOptions = new ClassifyOptions.Builder().classifierId(preCreatedClassifierId).text("is it hot outside?").build();
classification = service.classify(classifyOptions).execute();
} catch (NotFoundException e) {
// The build should not fail here, because this is out of our control.
throw new AssumptionViolatedException(e.getMessage(), e);
}
assertNotNull(classification);
assertEquals("temperature", classification.getTopClass());
}
use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions in project java-sdk by watson-developer-cloud.
the class NaturalLanguageClassifierTest method testNullText.
/**
* Test null text.
*/
@Test(expected = IllegalArgumentException.class)
public void testNullText() {
ClassifyOptions classifyOptions = new ClassifyOptions.Builder().classifierId(classifierId).build();
service.classify(classifyOptions);
}
use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions in project java-sdk by watson-developer-cloud.
the class NaturalLanguageClassifierTest method testNullClassifier.
// START NEGATIVE TESTS
/**
* Test null classifier.
*/
@Test(expected = IllegalArgumentException.class)
public void testNullClassifier() {
ClassifyOptions classifyOptions = new ClassifyOptions.Builder().text("test").build();
service.classify(classifyOptions);
}
use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions in project java-sdk by watson-developer-cloud.
the class VisualRecognitionIT method testCreateClassifierAndClassifyImage.
/**
* Test create a classifier.
*
* @throws FileNotFoundException the file not found exception
* @throws InterruptedException the interrupted exception
*/
@Ignore
@Test
public void testCreateClassifierAndClassifyImage() throws FileNotFoundException, InterruptedException {
String classifierName = "integration-test-java-sdk";
String carClassifier = "car";
String baseballClassifier = "baseball";
File carImages = new File("src/test/resources/visual_recognition/car_positive.zip");
File baseballImages = new File("src/test/resources/visual_recognition/baseball_positive.zip");
File negativeImages = new File("src/test/resources/visual_recognition/negative.zip");
File imageToClassify = new File("src/test/resources/visual_recognition/car.png");
CreateClassifierOptions.Builder builder = new CreateClassifierOptions.Builder().name(classifierName);
builder.addClass(carClassifier, carImages);
builder.addClass(baseballClassifier, baseballImages);
builder.negativeExamples(negativeImages);
Classifier newClassifier = service.createClassifier(builder.build()).execute();
try {
assertEquals(classifierName, newClassifier.getName());
boolean ready = false;
for (int x = 0; (x < 20) && !ready; x++) {
Thread.sleep(2000);
GetClassifierOptions getOptions = new GetClassifierOptions.Builder(newClassifier.getClassifierId()).build();
newClassifier = service.getClassifier(getOptions).execute();
ready = newClassifier.getStatus().equals(Status.READY);
}
assertEquals(Status.READY, newClassifier.getStatus());
ClassifyOptions options = new ClassifyOptions.Builder().imagesFile(imageToClassify).build();
ClassifiedImages classification = service.classify(options).execute();
assertNotNull(classification);
} finally {
DeleteClassifierOptions deleteOptions = new DeleteClassifierOptions.Builder(newClassifier.getClassifierId()).build();
service.deleteClassifier(deleteOptions).execute();
}
}
Aggregations