Search in sources :

Example 21 with ClassifyOptions

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

the class VisualRecognitionTest method testClassifyWithFile.

/**
 * Test classify with file.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testClassifyWithFile() throws IOException, InterruptedException {
    ClassifiedImages mockResponse = loadFixture(FIXTURE_CLASSIFICATION, ClassifiedImages.class);
    server.enqueue(new MockResponse().setBody(mockResponse.toString()));
    // execute request
    File images = new File(IMAGE_FILE);
    ClassifyOptions options = new ClassifyOptions.Builder().imagesFile(images).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) 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) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 22 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) {
    Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
    NaturalLanguageClassifier service = new NaturalLanguageClassifier(authenticator);
    ClassifyOptions classifyOptions = new ClassifyOptions.Builder().classifierId("<classifierId>").text("Is it sunny?").build();
    Classification classification = service.classify(classifyOptions).execute().getResult();
    System.out.println(classification);
}
Also used : IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) ClassifyOptions(com.ibm.watson.natural_language_classifier.v1.model.ClassifyOptions) Classification(com.ibm.watson.natural_language_classifier.v1.model.Classification) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator)

Example 23 with ClassifyOptions

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

the class VisualRecognition method classify.

/**
 * Classify images.
 *
 * <p>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 result of type {@link ClassifiedImages}
 */
public ServiceCall<ClassifiedImages> classify(ClassifyOptions classifyOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(classifyOptions, "classifyOptions cannot be null");
    com.ibm.cloud.sdk.core.util.Validator.isTrue((classifyOptions.imagesFile() != null) || (classifyOptions.url() != null) || (classifyOptions.threshold() != null) || (classifyOptions.owners() != null) || (classifyOptions.classifierIds() != null), "At least one of imagesFile, url, threshold, owners, or classifierIds must be supplied.");
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v3/classify"));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("watson_vision_combined", "v3", "classify");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    if (classifyOptions.acceptLanguage() != null) {
        builder.header("Accept-Language", classifyOptions.acceptLanguage());
    }
    builder.query("version", String.valueOf(this.version));
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    if (classifyOptions.imagesFile() != null) {
        okhttp3.RequestBody imagesFileBody = RequestUtils.inputStreamBody(classifyOptions.imagesFile(), classifyOptions.imagesFileContentType());
        multipartBuilder.addFormDataPart("images_file", classifyOptions.imagesFilename(), imagesFileBody);
    }
    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());
    ResponseConverter<ClassifiedImages> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<ClassifiedImages>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) MultipartBody(okhttp3.MultipartBody) ClassifiedImages(com.ibm.watson.visual_recognition.v3.model.ClassifiedImages)

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