Skip to content

Commit

Permalink
Implement the Java API for image classifier
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 320294241
  • Loading branch information
lu-wang-g authored and tflite-support-robot committed Jul 13, 2020
1 parent abd2a99 commit 9359e16
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ message ImageClassifierOptions {
// returned.
optional int32 max_results = 2 [default = -1];

// Score threshold in [0,1[, overrides the ones provided in the model metadata
// Score threshold in [0,1], overrides the ones provided in the model metadata
// (if any). Results below this value are rejected.
optional float score_threshold = 3;

Expand Down
16 changes: 16 additions & 0 deletions tensorflow_lite_support/cc/utils/jni_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ absl::string_view GetMappedFileBuffer(JNIEnv* env, const jobject& file_buffer) {
static_cast<size_t>(env->GetDirectBufferCapacity(file_buffer)));
}

void ThrowException(JNIEnv* env, const char* clazz, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
const size_t max_msg_len = 512;
auto* message = static_cast<char*>(malloc(max_msg_len));
if (message && (vsnprintf(message, max_msg_len, fmt, args) >= 0)) {
env->ThrowNew(env->FindClass(clazz), message);
} else {
env->ThrowNew(env->FindClass(clazz), "");
}
if (message) {
free(message);
}
va_end(args);
}

} // namespace utils
} // namespace support
} // namespace tflite
9 changes: 9 additions & 0 deletions tensorflow_lite_support/cc/utils/jni_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ namespace tflite {
namespace support {
namespace utils {

const char kIllegalArgumentException[] = "java/lang/IllegalArgumentException";
const char kIllegalStateException[] = "java/lang/IllegalStateException";
const char kNullPointerException[] = "java/lang/NullPointerException";
const char kIndexOutOfBoundsException[] = "java/lang/IndexOutOfBoundsException";
const char kUnsupportedOperationException[] =
"java/lang/UnsupportedOperationException";
const char kAssertionError[] = "java/lang/AssertionError";

// Check if t is nullptr, throw IllegalStateException if it is.
// Used to verify different types of jobjects are correctly created from jni.
Expand Down Expand Up @@ -67,6 +73,9 @@ std::vector<std::string> StringListToVector(JNIEnv* env, jobject list_object);

// Gets a mapped file buffer from a java object representing a file.
absl::string_view GetMappedFileBuffer(JNIEnv* env, const jobject& file_buffer);

void ThrowException(JNIEnv* env, const char* clazz, const char* fmt, ...);

} // namespace utils
} // namespace support
} // namespace tflite
Expand Down
3 changes: 2 additions & 1 deletion tensorflow_lite_support/java/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ package(
licenses = ["notice"], # Apache 2.0
)

exports_files(["AndroidManifest.xml"])

android_library(
name = "tensorflowlite_support",
srcs = glob(
["src/java/org/tensorflow/lite/support/**/*.java"],
exclude = ["src/java/org/tensorflow/lite/support/task/**/*.java"],
),
javacopts = JAVACOPTS,
manifest = "AndroidManifest.xml",
Expand Down

0 comments on commit 9359e16

Please sign in to comment.