Search in sources :

Example 11 with Classifiers

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

the class VisualRecognitionIT method testListClassifiers.

/**
 * Test list all the classifiers.
 */
@Ignore
@Test
public void testListClassifiers() {
    ListClassifiersOptions options = new ListClassifiersOptions.Builder().verbose(true).build();
    List<Classifier> classifiers = service.listClassifiers(options).execute().getResult().getClassifiers();
    assertNotNull(classifiers);
    assertTrue(!classifiers.isEmpty());
    Classifier classifier = classifiers.get(0);
    assertNotNull(classifier.getClassifierId());
    assertNotNull(classifier.getName());
    assertNotNull(classifier.getOwner());
    assertNotNull(classifier.getStatus());
    assertNotNull(classifier.getClasses());
    assertNotNull(classifier.getCreated());
}
Also used : ListClassifiersOptions(com.ibm.watson.visual_recognition.v3.model.ListClassifiersOptions) Classifier(com.ibm.watson.visual_recognition.v3.model.Classifier) Ignore(org.junit.Ignore) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 12 with Classifiers

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

the class VisualRecognition method classify.

/**
 * Classify images.
 *
 * <p>Classify images with built-in or custom classifiers.
 *
 * @param classifyOptions the {@link ClassifyOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link ClassifiedImages}
 */
public ServiceCall<ClassifiedImages> classify(ClassifyOptions classifyOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(classifyOptions, "classifyOptions cannot be null");
    com.ibm.cloud.sdk.core.util.Validator.isTrue((classifyOptions.imagesFile() != null) || (classifyOptions.url() != null) || (classifyOptions.threshold() != null) || (classifyOptions.owners() != null) || (classifyOptions.classifierIds() != null), "At least one of imagesFile, url, threshold, owners, or classifierIds must be supplied.");
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v3/classify"));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("watson_vision_combined", "v3", "classify");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    if (classifyOptions.acceptLanguage() != null) {
        builder.header("Accept-Language", classifyOptions.acceptLanguage());
    }
    builder.query("version", String.valueOf(this.version));
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    if (classifyOptions.imagesFile() != null) {
        okhttp3.RequestBody imagesFileBody = RequestUtils.inputStreamBody(classifyOptions.imagesFile(), classifyOptions.imagesFileContentType());
        multipartBuilder.addFormDataPart("images_file", classifyOptions.imagesFilename(), imagesFileBody);
    }
    if (classifyOptions.url() != null) {
        multipartBuilder.addFormDataPart("url", classifyOptions.url());
    }
    if (classifyOptions.threshold() != null) {
        multipartBuilder.addFormDataPart("threshold", String.valueOf(classifyOptions.threshold()));
    }
    if (classifyOptions.owners() != null) {
        multipartBuilder.addFormDataPart("owners", RequestUtils.join(classifyOptions.owners(), ","));
    }
    if (classifyOptions.classifierIds() != null) {
        multipartBuilder.addFormDataPart("classifier_ids", RequestUtils.join(classifyOptions.classifierIds(), ","));
    }
    builder.body(multipartBuilder.build());
    ResponseConverter<ClassifiedImages> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<ClassifiedImages>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) MultipartBody(okhttp3.MultipartBody) ClassifiedImages(com.ibm.watson.visual_recognition.v3.model.ClassifiedImages)

Example 13 with Classifiers

use of com.ibm.watson.visual_recognition.v3.model.Classifiers 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 14 with Classifiers

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

the class VisualRecognition method listClassifiers.

/**
 * Retrieve a list of classifiers.
 *
 * @param listClassifiersOptions the {@link ListClassifiersOptions} containing the options for the
 *     call
 * @return a {@link ServiceCall} with a result of type {@link Classifiers}
 */
public ServiceCall<Classifiers> listClassifiers(ListClassifiersOptions listClassifiersOptions) {
    if (listClassifiersOptions == null) {
        listClassifiersOptions = new ListClassifiersOptions.Builder().build();
    }
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v3/classifiers"));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("watson_vision_combined", "v3", "listClassifiers");
    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));
    if (listClassifiersOptions.verbose() != null) {
        builder.query("verbose", String.valueOf(listClassifiersOptions.verbose()));
    }
    ResponseConverter<Classifiers> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Classifiers>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : Classifiers(com.ibm.watson.visual_recognition.v3.model.Classifiers) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder)

Example 15 with Classifiers

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

the class VisualRecognitionTest method testListClassifiersWOptions.

@Test
public void testListClassifiersWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"classifiers\": [{\"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 listClassifiersPath = "/v3/classifiers";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the ListClassifiersOptions model
    ListClassifiersOptions listClassifiersOptionsModel = new ListClassifiersOptions.Builder().verbose(true).build();
    // Invoke operation with valid options model (positive test)
    Response<Classifiers> response = visualRecognitionService.listClassifiers(listClassifiersOptionsModel).execute();
    assertNotNull(response);
    Classifiers 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");
    assertEquals(Boolean.valueOf(query.get("verbose")), Boolean.valueOf(true));
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, listClassifiersPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Classifiers(com.ibm.watson.visual_recognition.v3.model.Classifiers) ListClassifiersOptions(com.ibm.watson.visual_recognition.v3.model.ListClassifiersOptions) Test(org.testng.annotations.Test)

Aggregations

Classifier (com.ibm.watson.visual_recognition.v3.model.Classifier)9 MockResponse (okhttp3.mockwebserver.MockResponse)7 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)7 Test (org.testng.annotations.Test)7 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)5 InputStream (java.io.InputStream)4 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)3 ListClassifiersOptions (com.ibm.watson.visual_recognition.v3.model.ListClassifiersOptions)3 HashMap (java.util.HashMap)3 MultipartBody (okhttp3.MultipartBody)3 Ignore (org.junit.Ignore)3 Test (org.junit.Test)3 ClassifiedImages (com.ibm.watson.visual_recognition.v3.model.ClassifiedImages)2 Classifiers (com.ibm.watson.visual_recognition.v3.model.Classifiers)2 DeleteClassifierOptions (com.ibm.watson.visual_recognition.v3.model.DeleteClassifierOptions)2 GetCoreMlModelOptions (com.ibm.watson.visual_recognition.v3.model.GetCoreMlModelOptions)2 Map (java.util.Map)2 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)1 Classifiers (com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifiers)1 ClassifyOptions (com.ibm.watson.visual_recognition.v3.model.ClassifyOptions)1