use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions in project java-sdk by watson-developer-cloud.
the class VisualRecognitionIT method testClassifyImagesFromFile.
/**
* Test classify images from zip file.
*/
@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();
assertClassifyImage(result, options);
}
use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions in project java-sdk by watson-developer-cloud.
the class VisualRecognitionIT method testClassifyImagesFromUrlUsingParameters.
/**
* Test classify images from url using the deprecated parameters option.
*/
@Test
public void testClassifyImagesFromUrlUsingParameters() {
String parameters = "{\"url\":\"" + IMAGE_URL + "\"}";
ClassifyOptions options = new ClassifyOptions.Builder().parameters(parameters).build();
ClassifiedImages result = service.classify(options).execute();
assertClassifyImage(result, options);
}
use of com.ibm.watson.developer_cloud.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();
assertClassifyImage(result, options);
}
use of com.ibm.watson.developer_cloud.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.developer_cloud.visual_recognition.v3.model.ClassifyOptions in project java-sdk by watson-developer-cloud.
the class VisualRecognition method classify.
/**
* Classify images.
*
* 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 response type of {@link ClassifiedImages}
*/
public ServiceCall<ClassifiedImages> classify(ClassifyOptions classifyOptions) {
Validator.notNull(classifyOptions, "classifyOptions cannot be null");
Validator.isTrue((classifyOptions.imagesFile() != null) || (classifyOptions.url() != null) || (classifyOptions.threshold() != null) || (classifyOptions.owners() != null) || (classifyOptions.classifierIds() != null) || (classifyOptions.parameters() != null), "At least one of imagesFile, url, threshold, owners, classifierIds, or parameters must be supplied.");
String[] pathSegments = { "v3/classify" };
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
builder.query(VERSION, versionDate);
if (classifyOptions.acceptLanguage() != null) {
builder.header("Accept-Language", classifyOptions.acceptLanguage());
}
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
if (classifyOptions.imagesFile() != null) {
RequestBody imagesFileBody = RequestUtils.inputStreamBody(classifyOptions.imagesFile(), classifyOptions.imagesFileContentType());
multipartBuilder.addFormDataPart("images_file", classifyOptions.imagesFilename(), imagesFileBody);
}
if (classifyOptions.parameters() != null) {
multipartBuilder.addFormDataPart("parameters", classifyOptions.parameters());
}
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());
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(ClassifiedImages.class));
}
Aggregations