Search in sources :

Example 1 with GetClassifierOptions

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

the class VisualRecognitionIT method testCreateClassifier.

/**
 * Test create a classifier.
 *
 * @throws FileNotFoundException the file not found exception
 * @throws InterruptedException the interrupted exception
 */
@Ignore
@Test
public void testCreateClassifier() throws FileNotFoundException, InterruptedException {
    String classifierName = "integration-test-java-sdk";
    String carClassifier = "car";
    String baseballClassifier = "baseball";
    File carImages = new File("src/test/resources/visual_recognition/car_positive.zip");
    File baseballImages = new File("src/test/resources/visual_recognition/baseball_positive.zip");
    InputStream negativeImages = new FileInputStream("src/test/resources/visual_recognition/negative.zip");
    CreateClassifierOptions.Builder builder = new CreateClassifierOptions.Builder().name(classifierName);
    builder.addClass(carClassifier, carImages);
    builder.addClass(baseballClassifier, baseballImages);
    builder.negativeExamples(negativeImages);
    builder.negativeExamplesFilename("negative.zip");
    Classifier newClass = service.createClassifier(builder.build()).execute();
    try {
        assertEquals(classifierName, newClass.getName());
        boolean ready = false;
        for (int x = 0; (x < 20) && !ready; x++) {
            Thread.sleep(2000);
            GetClassifierOptions getOptions = new GetClassifierOptions.Builder(newClass.getClassifierId()).build();
            newClass = service.getClassifier(getOptions).execute();
            ready = newClass.getStatus().equals(Status.READY);
        }
        assertEquals(Status.READY, newClass.getStatus());
    } finally {
        DeleteClassifierOptions deleteOptions = new DeleteClassifierOptions.Builder(newClass.getClassifierId()).build();
        service.deleteClassifier(deleteOptions).execute();
    }
}
Also used : DeleteClassifierOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.DeleteClassifierOptions) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Classifier(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier) FileInputStream(java.io.FileInputStream) GetClassifierOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.GetClassifierOptions) CreateClassifierOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.CreateClassifierOptions) File(java.io.File) Ignore(org.junit.Ignore) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Example 2 with GetClassifierOptions

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

Example 3 with GetClassifierOptions

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

the class NaturalLanguageClassifierTest method testGetClassifierWOptions.

@Test
public void testGetClassifierWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"name\": \"name\", \"url\": \"url\", \"status\": \"Non Existent\", \"classifier_id\": \"classifierId\", \"created\": \"2019-01-01T12:00:00.000Z\", \"status_description\": \"statusDescription\", \"language\": \"language\"}";
    String getClassifierPath = "/v1/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 = naturalLanguageClassifierService.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);
    assertNull(query);
    // 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.natural_language_classifier.v1.model.GetClassifierOptions) Classifier(com.ibm.watson.natural_language_classifier.v1.model.Classifier) Test(org.testng.annotations.Test)

Example 4 with GetClassifierOptions

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

the class NaturalLanguageClassifierIT method bGetClassifier.

/**
 * Test get classifier.
 */
@Test
public void bGetClassifier() {
    final Classifier classifier;
    try {
        GetClassifierOptions getOptions = new GetClassifierOptions.Builder().classifierId(classifierId).build();
        classifier = service.getClassifier(getOptions).execute().getResult();
    } catch (NotFoundException e) {
        // The build should not fail here, because this is out of our control.
        throw new AssumptionViolatedException(e.getMessage(), e);
    }
    assertNotNull(classifier);
    assertEquals(classifierId, classifier.getClassifierId());
    assertEquals(Classifier.Status.TRAINING, classifier.getStatus());
}
Also used : GetClassifierOptions(com.ibm.watson.natural_language_classifier.v1.model.GetClassifierOptions) AssumptionViolatedException(org.junit.AssumptionViolatedException) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Classifier(com.ibm.watson.natural_language_classifier.v1.model.Classifier) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 5 with GetClassifierOptions

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

Aggregations

Test (org.junit.Test)8 Classifier (com.ibm.watson.visual_recognition.v3.model.Classifier)4 File (java.io.File)4 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)4 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)3 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)3 Classifier (com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier)3 GetClassifierOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.GetClassifierOptions)3 GetClassifierOptions (com.ibm.watson.visual_recognition.v3.model.GetClassifierOptions)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 Ignore (org.junit.Ignore)3 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 Classifier (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classifier)2 GetClassifierOptions (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.GetClassifierOptions)2 CreateClassifierOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.CreateClassifierOptions)2 DeleteClassifierOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.DeleteClassifierOptions)2 Classifier (com.ibm.watson.natural_language_classifier.v1.model.Classifier)2 GetClassifierOptions (com.ibm.watson.natural_language_classifier.v1.model.GetClassifierOptions)2 CreateClassifierOptions (com.ibm.watson.visual_recognition.v3.model.CreateClassifierOptions)2 DeleteClassifierOptions (com.ibm.watson.visual_recognition.v3.model.DeleteClassifierOptions)2