Skip to content

Commit

Permalink
Make OSS Task library support ODML image.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 384370940
  • Loading branch information
xunkai55 authored and tflite-support-robot committed Jul 13, 2021
1 parent 6c687e1 commit bcf85b3
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions WORKSPACE
Expand Up @@ -420,6 +420,7 @@ maven_install(
"androidx.test:core:jar:1.3.0",
"androidx.test.ext:junit:jar:1.1.2",
"androidx.test:runner:jar:1.3.0",
"com.google.android.odml:image:aar:1.0.0-beta1",
"com.google.truth:truth:jar:1.1",
"commons-io:commons-io:jar:2.8.0",
# Mockito >= 3.4.6 cannot pass bazel desugar.
Expand Down
2 changes: 2 additions & 0 deletions tensorflow_lite_support/java/BUILD
Expand Up @@ -30,6 +30,7 @@ android_library(
deps = [
"@com_google_auto_value",
"@maven//:androidx_annotation_annotation",
"@maven//:com_google_android_odml_image",
"@org_checkerframework_qual",
"@org_tensorflow//tensorflow/lite/java:tensorflowlite",
],
Expand All @@ -44,6 +45,7 @@ android_library(
deps = [
"@com_google_auto_value",
"@maven//:androidx_annotation_annotation",
"@maven//:com_google_android_odml_image",
"@org_checkerframework_qual",
"@org_tensorflow//tensorflow/lite/java:tensorflowlite_java",
],
Expand Down
Expand Up @@ -30,6 +30,7 @@ android_library_with_tflite(
"//tensorflow_lite_support/java/src/java/org/tensorflow/lite/task/core:base_task_api",
"@com_google_auto_value",
"@maven//:androidx_annotation_annotation",
"@maven//:com_google_android_odml_image",
"@org_tensorflow//tensorflow/lite/java:tensorflowlite_java",
],
# LINT.ThenChange(<INTERNAL>/release/build_task_pom.sh:dep)
Expand Down
Expand Up @@ -45,6 +45,7 @@ android_library(
"//tensorflow_lite_support/java:tensorflowlite_support_java",
"//tensorflow_lite_support/java/src/java/org/tensorflow/lite/task/core:base_task_api",
"@com_google_auto_value",
"@maven//:com_google_android_odml_image",
"@org_tensorflow//tensorflow/lite/java:tensorflowlite_java",
],
)
Expand Down
Expand Up @@ -18,6 +18,7 @@
import android.content.Context;
import android.graphics.Rect;
import android.os.ParcelFileDescriptor;
import com.google.android.odml.image.MlImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
Expand All @@ -26,6 +27,7 @@
import java.util.Collections;
import java.util.List;
import org.tensorflow.lite.annotations.UsedByReflection;
import org.tensorflow.lite.support.image.MlImageAdapter;
import org.tensorflow.lite.support.image.TensorImage;
import org.tensorflow.lite.task.core.TaskJniUtils;
import org.tensorflow.lite.task.core.TaskJniUtils.EmptyHandleProvider;
Expand Down Expand Up @@ -432,6 +434,44 @@ public List<Classifications> run(
options);
}

/**
* Performs actual classification on the provided {@code MlImage}.
*
* @param image an {@code MlImage} object that represents an image
* @throws AssertionError if error occurs when classifying the image from the native code
* @throws IllegalArgumentException if the storage type or format of the image is unsupported
*/
public List<Classifications> classify(MlImage image) {
return classify(image, ImageProcessingOptions.builder().build());
}

/**
* Performs actual classification on the provided {@code MlImage} with {@link
* ImageProcessingOptions}.
*
* <p>{@link ImageClassifier} supports the following options:
*
* <ul>
* <li>Region of interest (ROI) (through {@link ImageProcessingOptions.Builder#setRoi}). It
* defaults to the entire image.
* <li>image rotation (through {@link ImageProcessingOptions.Builder#setOrientation}). It
* defaults to {@link ImageProcessingOptions.Orientation#TOP_LEFT}. {@link
* MlImage#getRotation()} is not effective.
* </ul>
*
* @param image a {@code MlImage} object that represents an image
* @param options configures options including ROI and rotation
* @throws AssertionError if error occurs when classifying the image from the native code
* @throws IllegalArgumentException if the storage type or format of the image is unsupported
*/
public List<Classifications> classify(MlImage image, ImageProcessingOptions options) {
image.getInternal().acquire();
TensorImage tensorImage = MlImageAdapter.createTensorImageFrom(image);
List<Classifications> result = classify(tensorImage, options);
image.close();
return result;
}

private List<Classifications> classify(
long frameBufferHandle, int width, int height, ImageProcessingOptions options) {
checkNotClosed();
Expand Down
Expand Up @@ -42,6 +42,7 @@ android_library(
"//tensorflow_lite_support/java:tensorflowlite_support_java",
"//tensorflow_lite_support/java/src/java/org/tensorflow/lite/task/core:base_task_api",
"@com_google_auto_value",
"@maven//:com_google_android_odml_image",
"@org_tensorflow//tensorflow/lite/java:tensorflowlite_java",
],
)
Expand Down
Expand Up @@ -17,6 +17,7 @@

import android.content.Context;
import android.os.ParcelFileDescriptor;
import com.google.android.odml.image.MlImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
Expand All @@ -25,6 +26,7 @@
import java.util.Collections;
import java.util.List;
import org.tensorflow.lite.annotations.UsedByReflection;
import org.tensorflow.lite.support.image.MlImageAdapter;
import org.tensorflow.lite.support.image.TensorImage;
import org.tensorflow.lite.task.core.TaskJniUtils;
import org.tensorflow.lite.task.core.TaskJniUtils.EmptyHandleProvider;
Expand Down Expand Up @@ -449,6 +451,42 @@ public List<Detection> run(
options);
}

/**
* Performs actual detection on the provided {@code MlImage}.
*
* @param image an {@code MlImage} object that represents an image
* @throws AssertionError if error occurs when processing the image from the native code
* @throws IllegalArgumentException if the storage type or format of the image is unsupported
*/
public List<Detection> detect(MlImage image) {
return detect(image, ImageProcessingOptions.builder().build());
}

/**
* Performs actual detection on the provided {@code MlImage} with {@link
* ImageProcessingOptions}.
*
* <p>{@link ObjectDetector} supports the following options:
*
* <ul>
* <li>image rotation (through {@link ImageProcessingOptions.Builder#setOrientation}). It
* defaults to {@link ImageProcessingOptions.Orientation#TOP_LEFT}. {@link
* MlImage#getRotation()} is not effective.
* </ul>
*
* @param image an {@code MlImage} object that represents an image
* @param options the options to configure how to preprocess the image
* @throws AssertionError if error occurs when classifying the image from the native code
* @throws IllegalArgumentException if the storage type or format of the image is unsupported
*/
public List<Detection> detect(MlImage image, ImageProcessingOptions options) {
image.getInternal().acquire();
TensorImage tensorImage = MlImageAdapter.createTensorImageFrom(image);
List<Detection> result = detect(tensorImage, options);
image.close();
return result;
}

private List<Detection> detect(long frameBufferHandle, ImageProcessingOptions options) {
checkNotClosed();

Expand Down
Expand Up @@ -43,6 +43,7 @@ android_library(
"//tensorflow_lite_support/java/src/java/org/tensorflow/lite/task/core:base_task_api",
"@com_google_auto_value",
"@maven//:androidx_annotation_annotation",
"@maven//:com_google_android_odml_image",
"@org_tensorflow//tensorflow/lite/java:tensorflowlite_java",
],
)
Expand Down
Expand Up @@ -18,6 +18,7 @@
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.os.ParcelFileDescriptor;
import com.google.android.odml.image.MlImage;
import com.google.auto.value.AutoValue;
import java.io.File;
import java.io.IOException;
Expand All @@ -27,6 +28,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.tensorflow.lite.support.image.MlImageAdapter;
import org.tensorflow.lite.support.image.TensorImage;
import org.tensorflow.lite.task.core.TaskJniUtils;
import org.tensorflow.lite.task.core.TaskJniUtils.EmptyHandleProvider;
Expand Down Expand Up @@ -317,6 +319,50 @@ public List<Segmentation> run(
options);
}

/**
* Performs actual segmentation on the provided {@code MlImage}.
*
* @param image an {@code MlImage} to segment.
* @return results of performing image segmentation. Note that at the time, a single {@link
* Segmentation} element is expected to be returned. The result is stored in a {@link List}
* for later extension to e.g. instance segmentation models, which may return one segmentation
* per object.
* @throws AssertionError if error occurs when segmenting the image from the native code
* @throws IllegalArgumentException if the storage type or format of the image is unsupported
*/
public List<Segmentation> segment(MlImage image) {
return segment(image, ImageProcessingOptions.builder().build());
}

/**
* Performs actual segmentation on the provided {@code MlImage} with {@link
* ImageProcessingOptions}.
*
* <p>{@link ImageSegmenter} supports the following options:
*
* <ul>
* <li>image rotation (through {@link ImageProcessingOptions.Builder#setOrientation}). It
* defaults to {@link ImageProcessingOptions.Orientation#TOP_LEFT}. {@link
* MlImage#getRotation()} is not effective.
* </ul>
*
* @param image an {@code MlImage} to segment.
* @param options the options configure how to preprocess the image.
* @return results of performing image segmentation. Note that at the time, a single {@link
* Segmentation} element is expected to be returned. The result is stored in a {@link List}
* for later extension to e.g. instance segmentation models, which may return one segmentation
* per object.
* @throws AssertionError if error occurs when segmenting the image from the native code
* @throws IllegalArgumentException if the color space type of image is unsupported
*/
public List<Segmentation> segment(MlImage image, ImageProcessingOptions options) {
image.getInternal().acquire();
TensorImage tensorImage = MlImageAdapter.createTensorImageFrom(image);
List<Segmentation> result = segment(tensorImage, options);
image.close();
return result;
}

public List<Segmentation> segment(long frameBufferHandle, ImageProcessingOptions options) {
checkNotClosed();

Expand Down
1 change: 1 addition & 0 deletions tensorflow_lite_support/tools/ci_build/build_all.sh
Expand Up @@ -41,6 +41,7 @@ bazel build -c opt --config=monolithic \
bazel build -c opt --config=monolithic \
//tensorflow_lite_support/examples/task/audio/desktop:audio_classifier_demo

bazel clean --expunge
# Build Coral plugin.
bazel build --sandbox_debug --subcommands --define=darwinn_portable=1 \
//tensorflow_lite_support/acceleration/configuration:edgetpu_coral_plugin
2 changes: 2 additions & 0 deletions tensorflow_lite_support/tools/ci_build/test_all.sh
Expand Up @@ -20,6 +20,8 @@

set -ex

bazel clean --expunge

source tensorflow_lite_support/tools/ci_build/tests/run_metadata_tests.sh
source tensorflow_lite_support/tools/ci_build/tests/run_support_lib_tests.sh

Expand Down

0 comments on commit bcf85b3

Please sign in to comment.