Search in sources :

Example 1 with UpdateClassifierOptions

use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.UpdateClassifierOptions 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));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) MultipartBody(okhttp3.MultipartBody) RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) Classifier(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier) File(java.io.File) RequestBody(okhttp3.RequestBody)

Example 2 with UpdateClassifierOptions

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

the class VisualRecognitionTest method testUpdateClassifier.

/**
 * Test update classifier.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testUpdateClassifier() throws IOException, InterruptedException {
    Classifier mockResponse = loadFixture(FIXTURE_CLASSIFIER, Classifier.class);
    server.enqueue(new MockResponse().setBody(mockResponse.toString()));
    // execute request
    File images = new File(IMAGE_FILE);
    String class1 = "class1";
    String classifierId = "foo123";
    UpdateClassifierOptions options = new UpdateClassifierOptions.Builder(classifierId).addClass(class1, images).build();
    Classifier serviceResponse = service.updateClassifier(options).execute();
    // first request
    String path = String.format(PATH_CLASSIFIER, classifierId);
    RecordedRequest request = server.takeRequest();
    path += "?" + VERSION_DATE + "=2016-05-20&api_key=" + API_KEY;
    assertEquals(path, request.getPath());
    assertEquals("POST", request.getMethod());
    String body = request.getBody().readUtf8();
    String contentDisposition = "Content-Disposition: form-data; name=\"class1_positive_examples\"; filename=\"test.zip\"";
    assertTrue(body.contains(contentDisposition));
    assertTrue(!body.contains("Content-Disposition: form-data; name=\"name\""));
    assertEquals(serviceResponse, mockResponse);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) UpdateClassifierOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.UpdateClassifierOptions) Classifier(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier) File(java.io.File) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 3 with UpdateClassifierOptions

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

Classifier (com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier)3 File (java.io.File)3 UpdateClassifierOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.UpdateClassifierOptions)2 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)1 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)1 ClassifiedImages (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImages)1 ClassifyOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions)1 CreateClassifierOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.CreateClassifierOptions)1 MultipartBody (okhttp3.MultipartBody)1 RequestBody (okhttp3.RequestBody)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 Test (org.junit.Test)1