Search in sources :

Example 41 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 createClassifier.

/**
 * Create a classifier.
 *
 * <p>Train a new multi-faceted classifier on the uploaded image data. Create your custom
 * classifier with positive or negative example training images. Include at least two sets of
 * examples, either two positive example files or one positive and one negative file. You can
 * upload a maximum of 256 MB per call.
 *
 * <p>**Tips when creating:**
 *
 * <p>- If you set the **X-Watson-Learning-Opt-Out** header parameter to `true` when you create a
 * classifier, the example training images are not stored. Save your training images locally. For
 * more information, see [Data collection](#data-collection).
 *
 * <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.
 *
 * @param createClassifierOptions the {@link CreateClassifierOptions} containing the options for
 *     the call
 * @return a {@link ServiceCall} with a result of type {@link Classifier}
 */
public ServiceCall<Classifier> createClassifier(CreateClassifierOptions createClassifierOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(createClassifierOptions, "createClassifierOptions cannot be null");
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v3/classifiers"));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("watson_vision_combined", "v3", "createClassifier");
    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);
    multipartBuilder.addFormDataPart("name", createClassifierOptions.name());
    for (Map.Entry<String, InputStream> entry : createClassifierOptions.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() + ".zip", part);
    }
    if (createClassifierOptions.negativeExamples() != null) {
        okhttp3.RequestBody negativeExamplesBody = RequestUtils.inputStreamBody(createClassifierOptions.negativeExamples(), "application/octet-stream");
        String negativeExamplesFilename = createClassifierOptions.negativeExamplesFilename();
        if (!negativeExamplesFilename.contains(".")) {
            negativeExamplesFilename += ".zip";
        }
        multipartBuilder.addFormDataPart("negative_examples", 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) 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 42 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 testGetClassifierWOptions.

@Test
public void testGetClassifierWOptions() 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 getClassifierPath = "/v3/classifiers/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the GetClassifierOptions model
    GetClassifierOptions getClassifierOptionsModel = new GetClassifierOptions.Builder().classifierId("testString").build();
    // Invoke operation with valid options model (positive test)
    Response<Classifier> response = visualRecognitionService.getClassifier(getClassifierOptionsModel).execute();
    assertNotNull(response);
    Classifier responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "GET");
    // 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, getClassifierPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) GetClassifierOptions(com.ibm.watson.visual_recognition.v3.model.GetClassifierOptions) 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