Search in sources :

Example 6 with ClassifyOptions

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

the class VisualRecognitionExample method main.

public static void main(String[] args) {
    VisualRecognition service = new VisualRecognition("2016-05-20");
    service.setApiKey("<api-key>");
    System.out.println("Classify an image");
    ClassifyOptions options = new ClassifyOptions.Builder().imagesFile(new File("src/test/resources/visual_recognition/car.png")).imagesFilename("car.png").build();
    ClassifiedImages result = service.classify(options).execute();
    System.out.println(result);
    System.out.println("Create a classifier with positive and negative images");
    CreateClassifierOptions createOptions = new CreateClassifierOptions.Builder().name("foo").addClass("car", new File("src/test/resources/visual_recognition/car_positive.zip")).addClass("baseball", new File("src/test/resources/visual_recognition/baseball_positive.zip")).negativeExamples(new File("src/test/resources/visual_recognition/negative.zip")).build();
    Classifier foo = service.createClassifier(createOptions).execute();
    System.out.println(foo);
    System.out.println("Classify using the 'Car' classifier");
    options = new ClassifyOptions.Builder().imagesFile(new File("src/test/resources/visual_recognition/car.png")).imagesFilename("car.png").addClassifierId(foo.getClassifierId()).build();
    result = service.classify(options).execute();
    System.out.println(result);
    System.out.println("Update a classifier with more positive images");
    UpdateClassifierOptions updateOptions = new UpdateClassifierOptions.Builder().classifierId(foo.getClassifierId()).addClass("car", new File("src/test/resources/visual_recognition/car_positive.zip")).build();
    Classifier updatedFoo = service.updateClassifier(updateOptions).execute();
    System.out.println(updatedFoo);
}
Also used : CreateClassifierOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.CreateClassifierOptions) ClassifyOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions) UpdateClassifierOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.UpdateClassifierOptions) Classifier(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier) ClassifiedImages(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImages) File(java.io.File)

Example 7 with ClassifyOptions

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

the class NaturalLanguageClassifierTest method testClassifyWOptions.

@Test
public void testClassifyWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"classifier_id\": \"classifierId\", \"url\": \"url\", \"text\": \"text\", \"top_class\": \"topClass\", \"classes\": [{\"confidence\": 10, \"class_name\": \"className\"}]}";
    String classifyPath = "/v1/classifiers/testString/classify";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the ClassifyOptions model
    ClassifyOptions classifyOptionsModel = new ClassifyOptions.Builder().classifierId("testString").text("testString").build();
    // Invoke operation with valid options model (positive test)
    Response<Classification> response = naturalLanguageClassifierService.classify(classifyOptionsModel).execute();
    assertNotNull(response);
    Classification responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "POST");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNull(query);
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, classifyPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) ClassifyOptions(com.ibm.watson.natural_language_classifier.v1.model.ClassifyOptions) Classification(com.ibm.watson.natural_language_classifier.v1.model.Classification) Test(org.testng.annotations.Test)

Example 8 with ClassifyOptions

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

the class NaturalLanguageClassifierIT method dClassify.

/**
 * Test classify. Use the pre created classifier to avoid waiting for availability
 */
@Test
public void dClassify() {
    Classification classification = null;
    try {
        ClassifyOptions classifyOptions = new ClassifyOptions.Builder().classifierId(preCreatedClassifierId).text("is it hot outside?").build();
        classification = service.classify(classifyOptions).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(classification);
    assertEquals("temperature", classification.getTopClass());
}
Also used : AssumptionViolatedException(org.junit.AssumptionViolatedException) Classification(com.ibm.watson.natural_language_classifier.v1.model.Classification) ClassifyOptions(com.ibm.watson.natural_language_classifier.v1.model.ClassifyOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 9 with ClassifyOptions

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

Example 10 with ClassifyOptions

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

the class VisualRecognitionIT method testClassifyImagesFromFile.

/**
 * Test classify images from zip file.
 *
 * @throws FileNotFoundException the file not found exception
 */
@Test
public void testClassifyImagesFromFile() throws FileNotFoundException {
    File images = new File(IMAGE_FILE);
    ClassifyOptions options = new ClassifyOptions.Builder().imagesFile(images).build();
    ClassifiedImages result = service.classify(options).execute().getResult();
    assertClassifyImage(result, options);
}
Also used : ClassifyOptions(com.ibm.watson.visual_recognition.v3.model.ClassifyOptions) ClassifiedImages(com.ibm.watson.visual_recognition.v3.model.ClassifiedImages) File(java.io.File) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 ClassifyOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions)9 ClassifiedImages (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImages)8 File (java.io.File)7 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)6 ClassifiedImages (com.ibm.watson.visual_recognition.v3.model.ClassifiedImages)6 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)5 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)5 ClassifyOptions (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions)5 ClassifyOptions (com.ibm.watson.visual_recognition.v3.model.ClassifyOptions)5 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)5 MockResponse (okhttp3.mockwebserver.MockResponse)4 Classification (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classification)3 Classification (com.ibm.watson.natural_language_classifier.v1.model.Classification)3 ClassifyOptions (com.ibm.watson.natural_language_classifier.v1.model.ClassifyOptions)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 Classifier (com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier)2 CreateClassifierOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.CreateClassifierOptions)2 AssumptionViolatedException (org.junit.AssumptionViolatedException)2