Search in sources :

Example 6 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 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().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.developer_cloud.visual_recognition.v3.model.ListClassifiersOptions) 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 7 with Classifiers

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

the class VisualRecognitionTest method testGetClassifiers.

/**
 * Test get classifiers.
 *
 * @throws InterruptedException the interrupted exception
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void testGetClassifiers() throws InterruptedException, IOException {
    Classifier mockClassifier = loadFixture(FIXTURE_CLASSIFIER, Classifier.class);
    List<Classifier> classifiers = new ArrayList<Classifier>();
    classifiers.add(mockClassifier);
    classifiers.add(mockClassifier);
    classifiers.add(mockClassifier);
    JsonObject mockResponse = new JsonObject();
    mockResponse.add("classifiers", new Gson().toJsonTree(classifiers));
    server.enqueue(new MockResponse().setBody(mockResponse.toString()));
    ListClassifiersOptions options = new ListClassifiersOptions.Builder().verbose(true).build();
    List<Classifier> serviceResponse = service.listClassifiers(options).execute().getClassifiers();
    // first request
    RecordedRequest request = server.takeRequest();
    String path = PATH_CLASSIFIERS + "?" + VERSION_DATE + "=2016-05-20&verbose=true&api_key=" + API_KEY;
    assertEquals(path, request.getPath());
    assertEquals("GET", request.getMethod());
    assertEquals(serviceResponse, classifiers);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) ListClassifiersOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ListClassifiersOptions) Classifier(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

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

/**
 * Create a classifier.
 *
 * Train a new multi-faceted classifier on the uploaded image data. Create your custom classifier with positive or
 * negative examples. 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. 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 response type of {@link Classifier}
 */
public ServiceCall<Classifier> createClassifier(CreateClassifierOptions createClassifierOptions) {
    Validator.notNull(createClassifierOptions, "createClassifierOptions cannot be null");
    String[] pathSegments = { "v3/classifiers" };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
    builder.query(VERSION, versionDate);
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    multipartBuilder.addFormDataPart("name", createClassifierOptions.name());
    // Classes
    for (String className : createClassifierOptions.classNames()) {
        String dataName = className + "_positive_examples";
        File positiveExamples = createClassifierOptions.positiveExamplesByClassName(className);
        RequestBody body = RequestUtils.fileBody(positiveExamples, "application/octet-stream");
        multipartBuilder.addFormDataPart(dataName, positiveExamples.getName(), body);
    }
    if (createClassifierOptions.negativeExamples() != null) {
        RequestBody negativeExamplesBody = RequestUtils.inputStreamBody(createClassifierOptions.negativeExamples(), "application/octet-stream");
        multipartBuilder.addFormDataPart("negative_examples", createClassifierOptions.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 9 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 classify.

/**
 * Classify images.
 *
 * 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 response type of {@link ClassifiedImages}
 */
public ServiceCall<ClassifiedImages> classify(ClassifyOptions classifyOptions) {
    Validator.notNull(classifyOptions, "classifyOptions cannot be null");
    Validator.isTrue((classifyOptions.imagesFile() != null) || (classifyOptions.url() != null) || (classifyOptions.threshold() != null) || (classifyOptions.owners() != null) || (classifyOptions.classifierIds() != null) || (classifyOptions.parameters() != null), "At least one of imagesFile, url, threshold, owners, classifierIds, or parameters must be supplied.");
    String[] pathSegments = { "v3/classify" };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
    builder.query(VERSION, versionDate);
    if (classifyOptions.acceptLanguage() != null) {
        builder.header("Accept-Language", classifyOptions.acceptLanguage());
    }
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    if (classifyOptions.imagesFile() != null) {
        RequestBody imagesFileBody = RequestUtils.inputStreamBody(classifyOptions.imagesFile(), classifyOptions.imagesFileContentType());
        multipartBuilder.addFormDataPart("images_file", classifyOptions.imagesFilename(), imagesFileBody);
    }
    if (classifyOptions.parameters() != null) {
        multipartBuilder.addFormDataPart("parameters", classifyOptions.parameters());
    }
    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());
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(ClassifiedImages.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) MultipartBody(okhttp3.MultipartBody) RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) ClassifiedImages(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImages) RequestBody(okhttp3.RequestBody)

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