Search in sources :

Example 6 with ClassifyOptions

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

the class VisualRecognitionTest method testClassifyWithBytes.

/**
 * Test classify with bytes or stream.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testClassifyWithBytes() throws IOException, InterruptedException {
    ClassifiedImages mockResponse = loadFixture(FIXTURE_CLASSIFICATION, ClassifiedImages.class);
    server.enqueue(new MockResponse().setBody(mockResponse.toString()));
    // execute request
    File images = new File(SINGLE_IMAGE_FILE);
    InputStream fileStream = new FileInputStream(images);
    ClassifyOptions options = new ClassifyOptions.Builder().imagesFile(fileStream).classifierIds(Arrays.asList("car")).build();
    ClassifiedImages serviceResponse = service.classify(options).execute();
    // first request
    RecordedRequest request = server.takeRequest();
    String path = PATH_CLASSIFY + "?" + VERSION_DATE + "=2016-05-20&api_key=" + API_KEY;
    assertEquals(path, request.getPath());
    assertEquals("POST", request.getMethod());
    assertEquals(serviceResponse, mockResponse);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ClassifyOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions) ClassifiedImages(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImages) File(java.io.File) FileInputStream(java.io.FileInputStream) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 7 with ClassifyOptions

use of com.ibm.watson.developer_cloud.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 8 with ClassifyOptions

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

the class NaturalLanguageClassifierExample method main.

public static void main(String[] args) {
    NaturalLanguageClassifier service = new NaturalLanguageClassifier();
    service.setUsernameAndPassword("<username>", "<password>");
    ClassifyOptions classifyOptions = new ClassifyOptions.Builder().classifierId("<classifierId>").text("Is it sunny?").build();
    Classification classification = service.classify(classifyOptions).execute();
    System.out.println(classification);
}
Also used : ClassifyOptions(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions) Classification(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classification)

Example 9 with ClassifyOptions

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

the class NaturalLanguageClassifierTest method testClassify.

/**
 * Test classify.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testClassify() throws InterruptedException {
    final JsonObject contentJson = new JsonObject();
    contentJson.addProperty(TEXT, classification.getText());
    final String path = String.format(CLASSIFY_PATH, classifierId);
    server.enqueue(jsonResponse(classification));
    ClassifyOptions classifyOptions = new ClassifyOptions.Builder().classifierId(classifierId).text(classification.getText()).build();
    final Classification result = service.classify(classifyOptions).execute();
    final RecordedRequest request = server.takeRequest();
    assertEquals(path, request.getPath());
    assertEquals("POST", request.getMethod());
    assertEquals(contentJson.toString(), request.getBody().readUtf8());
    assertEquals(classification, result);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) ClassifyOptions(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions) Classification(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classification) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest)

Example 10 with ClassifyOptions

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

the class VisualRecognitionIT method testClassifyImagesFromUrl.

/**
 * Test classify images from url.
 */
@Test
public void testClassifyImagesFromUrl() {
    ClassifyOptions options = new ClassifyOptions.Builder().url(IMAGE_URL).build();
    ClassifiedImages result = service.classify(options).execute();
    assertClassifyImage(result, options);
}
Also used : ClassifyOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions) ClassifiedImages(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImages) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)11 ClassifiedImages (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImages)9 ClassifyOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions)9 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)6 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)5 ClassifyOptions (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions)5 File (java.io.File)5 Classification (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classification)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)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 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 JsonObject (com.google.gson.JsonObject)1 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)1 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)1 VisualRecognition (com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition)1 ClassResult (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassResult)1 ClassifiedImage (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImage)1