Search in sources :

Example 1 with Classifiers

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

use of com.ibm.watson.developer_cloud.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 response type of {@link Classifiers}
 */
public ServiceCall<Classifiers> listClassifiers(ListClassifiersOptions listClassifiersOptions) {
    String[] pathSegments = { "v3/classifiers" };
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
    builder.query(VERSION, versionDate);
    if (listClassifiersOptions != null) {
        if (listClassifiersOptions.verbose() != null) {
            builder.query("verbose", String.valueOf(listClassifiersOptions.verbose()));
        }
    }
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Classifiers.class));
}
Also used : Classifiers(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifiers) RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder)

Example 3 with Classifiers

use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifiers 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));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) Classifier(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier)

Example 4 with Classifiers

use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifiers 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().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.developer_cloud.visual_recognition.v3.model.DeleteClassifierOptions) Classifier(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier) Ignore(org.junit.Ignore) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Example 5 with Classifiers

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

the class VisualRecognitionIT method testGetCoreMlModel.

/**
 * Test getting the Core ML file for a classifier.
 */
@Ignore
@Test
public void testGetCoreMlModel() {
    ListClassifiersOptions options = new ListClassifiersOptions.Builder().verbose(true).build();
    List<Classifier> classifiers = service.listClassifiers(options).execute().getClassifiers();
    for (Classifier classifier : classifiers) {
        if (classifier.isCoreMlEnabled()) {
            GetCoreMlModelOptions getCoreMlModelOptions = new GetCoreMlModelOptions.Builder().classifierId(classifier.getClassifierId()).build();
            InputStream coreMlFile = service.getCoreMlModel(getCoreMlModelOptions).execute();
            assertNotNull(coreMlFile);
            break;
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ListClassifiersOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ListClassifiersOptions) Classifier(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier) GetCoreMlModelOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.GetCoreMlModelOptions) Ignore(org.junit.Ignore) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Aggregations

Classifier (com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier)7 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)5 Test (org.junit.Test)4 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)3 ListClassifiersOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ListClassifiersOptions)3 MultipartBody (okhttp3.MultipartBody)3 RequestBody (okhttp3.RequestBody)3 Ignore (org.junit.Ignore)3 File (java.io.File)2 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)1 ClassifiedImages (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImages)1 Classifiers (com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifiers)1 DeleteClassifierOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.DeleteClassifierOptions)1 GetCoreMlModelOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.GetCoreMlModelOptions)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 MockResponse (okhttp3.mockwebserver.MockResponse)1