Search in sources :

Example 11 with ClassifyOptions

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

the class VisualRecognitionTest method testClassifyWOptions.

@Test
public void testClassifyWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"custom_classes\": 13, \"images_processed\": 15, \"images\": [{\"source_url\": \"sourceUrl\", \"resolved_url\": \"resolvedUrl\", \"image\": \"image\", \"error\": {\"code\": 4, \"description\": \"description\", \"error_id\": \"errorId\"}, \"classifiers\": [{\"name\": \"name\", \"classifier_id\": \"classifierId\", \"classes\": [{\"class\": \"xClass\", \"score\": 0, \"type_hierarchy\": \"typeHierarchy\"}]}]}], \"warnings\": [{\"warning_id\": \"warningId\", \"description\": \"description\"}]}";
    String classifyPath = "/v3/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().imagesFile(TestUtilities.createMockStream("This is a mock file.")).imagesFilename("testString").imagesFileContentType("testString").url("testString").threshold(Float.valueOf("36.0")).owners(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).classifierIds(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).acceptLanguage("en").build();
    // Invoke operation with valid options model (positive test)
    Response<ClassifiedImages> response = visualRecognitionService.classify(classifyOptionsModel).execute();
    assertNotNull(response);
    ClassifiedImages 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);
    assertNotNull(query);
    // Get query params
    assertEquals(query.get("version"), "testString");
    // 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.visual_recognition.v3.model.ClassifyOptions) ClassifiedImages(com.ibm.watson.visual_recognition.v3.model.ClassifiedImages) Test(org.testng.annotations.Test)

Example 12 with ClassifyOptions

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

the class VisualRecognitionIT method testClassifyImagesFromBytes.

/**
 * Test classify a single jpg image.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void testClassifyImagesFromBytes() throws IOException {
    InputStream imagesStream = new FileInputStream(SINGLE_IMAGE_FILE);
    ClassifyOptions options = new ClassifyOptions.Builder().imagesFile(imagesStream).imagesFilename("car.png").build();
    ClassifiedImages result = service.classify(options).execute().getResult();
    assertClassifyImage(result, options);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ClassifyOptions(com.ibm.watson.visual_recognition.v3.model.ClassifyOptions) ClassifiedImages(com.ibm.watson.visual_recognition.v3.model.ClassifiedImages) FileInputStream(java.io.FileInputStream) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 13 with ClassifyOptions

use of com.ibm.watson.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().getResult();
    assertClassifyImage(result, options);
}
Also used : ClassifyOptions(com.ibm.watson.visual_recognition.v3.model.ClassifyOptions) ClassifiedImages(com.ibm.watson.visual_recognition.v3.model.ClassifiedImages) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 14 with ClassifyOptions

use of com.ibm.watson.visual_recognition.v3.model.ClassifyOptions in project jbpm-work-items by kiegroup.

the class ClassifyImageWorkitemHandler method executeWorkItem.

public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
    try {
        RequiredParameterValidator.validate(this.getClass(), workItem);
        Document classificationImage = (Document) workItem.getParameter("ImageToClassify");
        Map<String, Object> widResults = new HashMap<String, Object>();
        VisualRecognition service = auth.getService(apiKey);
        ByteArrayInputStream imageStream = new ByteArrayInputStream(classificationImage.getContent());
        ClassifyOptions classifyOptions = new ClassifyOptions.Builder().imagesFile(imageStream).imagesFilename(classificationImage.getName()).parameters("{\"owners\": [\"me\"]}").build();
        ClassifiedImage result = service.classify(classifyOptions).execute().getImages().get(0);
        if (result.getError() != null) {
            ErrorInfo errorInfo = result.getError();
            logger.error("Error classifying image: " + errorInfo.getDescription());
            workItemManager.abortWorkItem(workItem.getId());
        } else {
            List<ImageClassificationResult> resultList = new ArrayList<>();
            for (ClassifierResult classification : result.getClassifiers()) {
                for (ClassResult classResult : classification.getClasses()) {
                    resultList.add(new ImageClassificationResult(classification, classResult));
                }
                widResults.put(RESULT_VALUE, resultList);
            }
            workItemManager.completeWorkItem(workItem.getId(), widResults);
        }
    } catch (Exception e) {
        handleException(e);
    }
}
Also used : HashMap(java.util.HashMap) VisualRecognition(com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition) ClassifyOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions) ErrorInfo(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ErrorInfo) ArrayList(java.util.ArrayList) Document(org.jbpm.document.Document) ImageClassificationResult(org.jbpm.process.workitem.ibm.watson.result.ImageClassificationResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ClassifiedImage(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImage) ClassifierResult(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifierResult) ClassResult(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassResult)

Example 15 with ClassifyOptions

use of com.ibm.watson.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)

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