Skip to content

Commit

Permalink
added byte array support to classify an in memory image
Browse files Browse the repository at this point in the history
  • Loading branch information
MJonker committed Nov 22, 2016
1 parent b6de93a commit f55253c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ public ServiceCall<VisualClassification> classify(ClassifyImagesOptions options)
RequestBody requestBody = RequestBody.create(HttpMediaType.BINARY_FILE, options.images());
bodyBuilder.addFormDataPart(PARAM_IMAGES_FILE, options.images().getName(), requestBody);
}

if (options.imagesBinary() != null) {
RequestBody requestBody = RequestBody.create(HttpMediaType.BINARY_FILE, options.imagesBinary());
bodyBuilder.addFormDataPart(PARAM_IMAGES_FILE, options.imageName(), requestBody);
}

bodyBuilder.addFormDataPart(PARAM_PARAMETERS, getParametersAsJson(options).toString());

RequestBuilder requestBuilder = RequestBuilder.post(PATH_CLASSIFY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ public class ClassifyImagesOptions {

private ClassifyImagesOptions(Builder builder) {
imagesFile = builder.imagesFile;
imagesBinary = builder.imagesBinary;
url = builder.url;
classifierIds = builder.classifierIds;
threshold = builder.threshold;
imageName = builder.imageName;
}

private File imagesFile;
private byte[] imagesBinary;
private HttpUrl url;
private List<String> classifierIds;
private Double threshold;
private String imageName="placeholder.png";

/**
* Classify Images Request Builder.
Expand All @@ -45,11 +49,14 @@ public static class Builder {
private HttpUrl url;
private List<String> classifierIds;
private Double threshold;
private byte[] imagesBinary;
private String imageName="placeholder.png";


private Builder(ClassifyImagesOptions options) {
this();
imagesFile = options.imagesFile;
imagesBinary = options.imagesBinary;
url = options.url;
classifierIds = new ArrayList<String>(options.classifierIds);
threshold = options.threshold;
Expand All @@ -66,6 +73,19 @@ public Builder images(File imagesFile) {
this.imagesFile = imagesFile;
return this;
}

/**
* Sets the images.
*
* @param imagesBinary the images bytes
* @return the builder
*/
public Builder images(byte[] imagesBinary, String imageName) {
Validator.notNull(imagesBinary, "'imagesBinary' cannot be null");
this.imagesBinary = imagesBinary;
this.imageName = imageName;
return this;
}

/**
* Sets the image url.
Expand Down Expand Up @@ -139,7 +159,7 @@ public Builder() { }
* @return the profile options
*/
public ClassifyImagesOptions build() {
Validator.isTrue((url != null) || (imagesFile != null), "url or imagesFile should be specified");
Validator.isTrue((url != null) || (imagesFile != null || imagesBinary!=null), "url or imagesFile or imagesBinary should be specified");
return new ClassifyImagesOptions(this);
}

Expand All @@ -153,6 +173,19 @@ public ClassifyImagesOptions build() {
public File images() {
return imagesFile;
}

/**
* Returns the images binary.
*
* @return the images binary
*/
public byte[] imagesBinary() {
return imagesBinary;
}

public String imageName() {
return imageName;
}

/**
* Returns the {@link HttpUrl}.
Expand Down

0 comments on commit f55253c

Please sign in to comment.