use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier in project java-sdk by watson-developer-cloud.
the class VisualRecognition method updateClassifier.
/**
* Update a classifier.
*
* Update a custom classifier by adding new positive or negative classes (examples) or by adding new images to
* existing classes. You must supply at least one set of positive or negative examples. For details, see [Updating
* custom
* classifiers]
* (https://console.bluemix.net/docs/services/visual-recognition/customizing.html#updating-custom-classifiers).
* Encode all names in UTF-8 if they contain non-ASCII characters (.zip and image file names, and classifier and class
* names). The service assumes UTF-8 encoding if it encounters non-ASCII characters. **Important:** You can't update a
* custom classifier with an API key for a Lite plan. To update a custom classifier on a Lite plan, create another
* service instance on a Standard plan and re-create your custom classifier. **Tip:** Don't make retraining calls on a
* classifier until the status is ready. When you submit retraining requests in parallel, the last request overwrites
* the previous requests. The retrained property shows the last time the classifier retraining finished.
*
* @param updateClassifierOptions the {@link UpdateClassifierOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link Classifier}
*/
public ServiceCall<Classifier> updateClassifier(UpdateClassifierOptions updateClassifierOptions) {
Validator.notNull(updateClassifierOptions, "updateClassifierOptions cannot be null");
Validator.isTrue((updateClassifierOptions.classNames().size() > 0) || (updateClassifierOptions.negativeExamples() != null), "At least one of classnamePositiveExamples or negativeExamples must be supplied.");
String[] pathSegments = { "v3/classifiers" };
String[] pathParameters = { updateClassifierOptions.classifierId() };
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
builder.query(VERSION, versionDate);
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
// Classes
for (String className : updateClassifierOptions.classNames()) {
String dataName = className + "_positive_examples";
File positiveExamples = updateClassifierOptions.positiveExamplesByClassName(className);
RequestBody body = RequestUtils.fileBody(positiveExamples, "application/octet-stream");
multipartBuilder.addFormDataPart(dataName, positiveExamples.getName(), body);
}
if (updateClassifierOptions.negativeExamples() != null) {
RequestBody negativeExamplesBody = RequestUtils.inputStreamBody(updateClassifierOptions.negativeExamples(), "application/octet-stream");
multipartBuilder.addFormDataPart("negative_examples", updateClassifierOptions.negativeExamplesFilename(), negativeExamplesBody);
}
builder.body(multipartBuilder.build());
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Classifier.class));
}
use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier in project java-sdk by watson-developer-cloud.
the class VisualRecognition method getClassifier.
/**
* Retrieve classifier details.
*
* Retrieve information about a custom classifier.
*
* @param getClassifierOptions the {@link GetClassifierOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link Classifier}
*/
public ServiceCall<Classifier> getClassifier(GetClassifierOptions getClassifierOptions) {
Validator.notNull(getClassifierOptions, "getClassifierOptions cannot be null");
String[] pathSegments = { "v3/classifiers" };
String[] pathParameters = { getClassifierOptions.classifierId() };
RequestBuilder builder = RequestBuilder.get(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
builder.query(VERSION, versionDate);
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Classifier.class));
}
use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier in project java-sdk by watson-developer-cloud.
the class NaturalLanguageClassifierIT method aCreate.
/**
* Creates the classifier.
*
* @throws Exception the exception
*/
@Test
public void aCreate() throws Exception {
final File trainingData = new File("src/test/resources/natural_language_classifier/weather_data_train.csv");
final File metadata = new File("src/test/resources/natural_language_classifier/metadata.json");
CreateClassifierOptions createOptions = new CreateClassifierOptions.Builder().metadata(metadata).trainingData(trainingData).trainingDataFilename("weather_data_train.csv").build();
Classifier classifier = service.createClassifier(createOptions).execute();
try {
assertNotNull(classifier);
assertEquals(Status.TRAINING, classifier.getStatus());
assertEquals("test-classifier", classifier.getName());
assertEquals("en", classifier.getLanguage());
} finally {
classifierId = classifier.getClassifierId();
}
}
use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier in project java-sdk by watson-developer-cloud.
the class NaturalLanguageClassifierTest method testCreateClassifier.
/**
* Test create classifier.
*
* @throws InterruptedException the interrupted exception
*/
@Test
public void testCreateClassifier() throws InterruptedException, FileNotFoundException {
server.enqueue(jsonResponse(classifier));
File metadata = new File(RESOURCE + "metadata.json");
File trainingData = new File(RESOURCE + "weather_data_train.csv");
CreateClassifierOptions createOptions = new CreateClassifierOptions.Builder().metadata(metadata).trainingData(trainingData).trainingDataFilename("weather_data_train.csv").build();
final Classifier response = service.createClassifier(createOptions).execute();
final RecordedRequest request = server.takeRequest();
assertEquals(CLASSIFIERS_PATH, request.getPath());
assertEquals(classifier, response);
}
use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier in project java-sdk by watson-developer-cloud.
the class VisualRecognitionIT method testCreateClassifier.
/**
* Test create a classifier.
*
* @throws FileNotFoundException the file not found exception
* @throws InterruptedException the interrupted exception
*/
@Ignore
@Test
public void testCreateClassifier() 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");
InputStream negativeImages = new FileInputStream("src/test/resources/visual_recognition/negative.zip");
CreateClassifierOptions.Builder builder = new CreateClassifierOptions.Builder().name(classifierName);
builder.addClass(carClassifier, carImages);
builder.addClass(baseballClassifier, baseballImages);
builder.negativeExamples(negativeImages);
builder.negativeExamplesFilename("negative.zip");
Classifier newClass = service.createClassifier(builder.build()).execute();
try {
assertEquals(classifierName, newClass.getName());
boolean ready = false;
for (int x = 0; (x < 20) && !ready; x++) {
Thread.sleep(2000);
GetClassifierOptions getOptions = new GetClassifierOptions.Builder(newClass.getClassifierId()).build();
newClass = service.getClassifier(getOptions).execute();
ready = newClass.getStatus().equals(Status.READY);
}
assertEquals(Status.READY, newClass.getStatus());
} finally {
DeleteClassifierOptions deleteOptions = new DeleteClassifierOptions.Builder(newClass.getClassifierId()).build();
service.deleteClassifier(deleteOptions).execute();
}
}
Aggregations