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);
}
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);
}
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);
}
Aggregations