Search in sources :

Example 16 with Classifier

use of com.ibm.watson.natural_language_classifier.v1.model.Classifier 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/v3/car_positive.zip");
    File baseballImages = new File("src/test/resources/visual_recognition/v3/baseball_positive.zip");
    File negativeImages = new File("src/test/resources/visual_recognition/v3/negative.zip");
    File imageToClassify = new File("src/test/resources/visual_recognition/v3/car.png");
    CreateClassifierOptions.Builder builder = new CreateClassifierOptions.Builder().name(classifierName);
    builder.addPositiveExamples(carClassifier, carImages);
    builder.addPositiveExamples(baseballClassifier, baseballImages);
    builder.negativeExamples(negativeImages);
    Classifier newClassifier = service.createClassifier(builder.build()).execute().getResult();
    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().getResult();
            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().getResult();
        assertNotNull(classification);
    } finally {
        DeleteClassifierOptions deleteOptions = new DeleteClassifierOptions.Builder(newClassifier.getClassifierId()).build();
        service.deleteClassifier(deleteOptions).execute();
    }
}
Also used : DeleteClassifierOptions(com.ibm.watson.visual_recognition.v3.model.DeleteClassifierOptions) ClassifyOptions(com.ibm.watson.visual_recognition.v3.model.ClassifyOptions) Classifier(com.ibm.watson.visual_recognition.v3.model.Classifier) GetClassifierOptions(com.ibm.watson.visual_recognition.v3.model.GetClassifierOptions) CreateClassifierOptions(com.ibm.watson.visual_recognition.v3.model.CreateClassifierOptions) ClassifiedImages(com.ibm.watson.visual_recognition.v3.model.ClassifiedImages) File(java.io.File) Ignore(org.junit.Ignore) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 17 with Classifier

use of com.ibm.watson.natural_language_classifier.v1.model.Classifier in project java-sdk by watson-developer-cloud.

the class VisualRecognitionIT method testDeleteAllClassifiers.

/**
 * Delete all the visual classifiers.
 */
@Test
@Ignore
public void testDeleteAllClassifiers() {
    List<Classifier> classifiers = service.listClassifiers(null).execute().getResult().getClassifiers();
    for (Classifier classifier : classifiers) {
        if (!classifier.getClassifierId().equals(classifierId)) {
            DeleteClassifierOptions deleteOptions = new DeleteClassifierOptions.Builder(classifier.getClassifierId()).build();
            service.deleteClassifier(deleteOptions).execute();
        }
    }
}
Also used : DeleteClassifierOptions(com.ibm.watson.visual_recognition.v3.model.DeleteClassifierOptions) Classifier(com.ibm.watson.visual_recognition.v3.model.Classifier) Ignore(org.junit.Ignore) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 18 with Classifier

use of com.ibm.watson.natural_language_classifier.v1.model.Classifier in project java-sdk by watson-developer-cloud.

the class VisualRecognition method updateClassifier.

/**
 * Update a classifier.
 *
 * <p>Update a custom classifier by adding new positive or negative classes 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://cloud.ibm.com/docs/visual-recognition?topic=visual-recognition-customizing#updating-custom-classifiers).
 *
 * <p>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.
 *
 * <p>**Tips about retraining:**
 *
 * <p>- You can't update the classifier if the **X-Watson-Learning-Opt-Out** header parameter was
 * set to `true` when the classifier was created. Training images are not stored in that case.
 * Instead, create another classifier. For more information, see [Data
 * collection](#data-collection).
 *
 * <p>- 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 result of type {@link Classifier}
 */
public ServiceCall<Classifier> updateClassifier(UpdateClassifierOptions updateClassifierOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(updateClassifierOptions, "updateClassifierOptions cannot be null");
    com.ibm.cloud.sdk.core.util.Validator.isTrue((updateClassifierOptions.positiveExamples() != null) || (updateClassifierOptions.negativeExamples() != null), "At least one of positiveExamples or negativeExamples must be supplied.");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("classifier_id", updateClassifierOptions.classifierId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v3/classifiers/{classifier_id}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("watson_vision_combined", "v3", "updateClassifier");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("version", String.valueOf(this.version));
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    if (updateClassifierOptions.positiveExamples() != null) {
        for (Map.Entry<String, InputStream> entry : updateClassifierOptions.positiveExamples().entrySet()) {
            String partName = String.format("%s_positive_examples", entry.getKey());
            okhttp3.RequestBody part = RequestUtils.inputStreamBody(entry.getValue(), "application/octet-stream");
            multipartBuilder.addFormDataPart(partName, entry.getKey(), part);
        }
    }
    if (updateClassifierOptions.negativeExamples() != null) {
        okhttp3.RequestBody negativeExamplesBody = RequestUtils.inputStreamBody(updateClassifierOptions.negativeExamples(), "application/octet-stream");
        multipartBuilder.addFormDataPart("negative_examples", updateClassifierOptions.negativeExamplesFilename(), negativeExamplesBody);
    }
    builder.body(multipartBuilder.build());
    ResponseConverter<Classifier> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Classifier>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) InputStream(java.io.InputStream) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) Classifier(com.ibm.watson.visual_recognition.v3.model.Classifier) MultipartBody(okhttp3.MultipartBody) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with Classifier

use of com.ibm.watson.natural_language_classifier.v1.model.Classifier in project java-sdk by watson-developer-cloud.

the class VisualRecognition method getClassifier.

/**
 * Retrieve classifier details.
 *
 * <p>Retrieve information about a custom classifier.
 *
 * @param getClassifierOptions the {@link GetClassifierOptions} containing the options for the
 *     call
 * @return a {@link ServiceCall} with a result of type {@link Classifier}
 */
public ServiceCall<Classifier> getClassifier(GetClassifierOptions getClassifierOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(getClassifierOptions, "getClassifierOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("classifier_id", getClassifierOptions.classifierId());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v3/classifiers/{classifier_id}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("watson_vision_combined", "v3", "getClassifier");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("version", String.valueOf(this.version));
    ResponseConverter<Classifier> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Classifier>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) Classifier(com.ibm.watson.visual_recognition.v3.model.Classifier)

Example 20 with Classifier

use of com.ibm.watson.natural_language_classifier.v1.model.Classifier in project java-sdk by watson-developer-cloud.

the class VisualRecognitionTest method testCreateClassifierWOptions.

@Test
public void testCreateClassifierWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"classifier_id\": \"classifierId\", \"name\": \"name\", \"owner\": \"owner\", \"status\": \"ready\", \"core_ml_enabled\": false, \"explanation\": \"explanation\", \"created\": \"2019-01-01T12:00:00.000Z\", \"classes\": [{\"class\": \"xClass\"}], \"retrained\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\"}";
    String createClassifierPath = "/v3/classifiers";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the CreateClassifierOptions model
    CreateClassifierOptions createClassifierOptionsModel = new CreateClassifierOptions.Builder().name("testString").positiveExamples(mockStreamMap).negativeExamples(TestUtilities.createMockStream("This is a mock file.")).negativeExamplesFilename("testString").build();
    // Invoke operation with valid options model (positive test)
    Response<Classifier> response = visualRecognitionService.createClassifier(createClassifierOptionsModel).execute();
    assertNotNull(response);
    Classifier responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "POST");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    // Get query params
    assertEquals(query.get("version"), "testString");
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, createClassifierPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CreateClassifierOptions(com.ibm.watson.visual_recognition.v3.model.CreateClassifierOptions) Classifier(com.ibm.watson.visual_recognition.v3.model.Classifier) Test(org.testng.annotations.Test)

Aggregations

Test (org.junit.Test)24 Classifier (com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier)13 File (java.io.File)12 Classifier (com.ibm.watson.visual_recognition.v3.model.Classifier)11 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)11 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)10 MockResponse (okhttp3.mockwebserver.MockResponse)9 Ignore (org.junit.Ignore)9 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)8 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)7 Classifier (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classifier)7 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)6 Classifier (com.ibm.watson.natural_language_classifier.v1.model.Classifier)6 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)6 MultipartBody (okhttp3.MultipartBody)6 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)5 Test (org.testng.annotations.Test)5 CreateClassifierOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.CreateClassifierOptions)4 FileInputStream (java.io.FileInputStream)4