1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include <jni.h>
17 
18 #include "tensorflow_lite_support/cc/task/text/nlclassifier/nl_classifier.h"
19 #include "tensorflow_lite_support/cc/utils/jni_utils.h"
20 
21 namespace tflite {
22 namespace task {
23 namespace text {
24 namespace nlclassifier {
25 
26 using ::tflite::support::utils::kAssertionError;
27 using ::tflite::support::utils::kInvalidPointer;
28 using ::tflite::support::utils::ThrowException;
29 using ::tflite::support::utils::ConvertVectorToArrayList;
30 using ::tflite::support::utils::JStringToString;
31 using ::tflite::task::core::Category;
32 using ::tflite::task::text::nlclassifier::NLClassifier;
33 
RunClassifier(JNIEnv * env,jlong native_handle,jstring text)34 jobject RunClassifier(JNIEnv* env, jlong native_handle, jstring text) {
35   auto* nl_classifier = reinterpret_cast<NLClassifier*>(native_handle);
36 
37   auto results = nl_classifier->ClassifyText(JStringToString(env, text));
38   if (!results.ok()) {
39         ThrowException(env, kAssertionError,
40                        "Error occurred when running classifier: %s",
41                        results.status().message().data());
42         return env->ExceptionOccurred();
43   }
44 
45   jclass category_class =
46       env->FindClass("org/tensorflow/lite/support/label/Category");
47   jmethodID category_init =
48       env->GetMethodID(category_class, "<init>", "(Ljava/lang/String;F)V");
49 
50   return ConvertVectorToArrayList<Category>(
51       env, results.value(),
52       [env, category_class, category_init](const Category& category) {
53         jstring class_name = env->NewStringUTF(category.class_name.data());
54         // Convert double to float as Java interface exposes float as scores.
55         jobject jcategory =
56             env->NewObject(category_class, category_init, class_name,
57                            static_cast<float>(category.score));
58         env->DeleteLocalRef(class_name);
59         return jcategory;
60       });
61 }
62 
GetModelVersionNative(JNIEnv * env,jlong native_handle)63 jstring GetModelVersionNative(JNIEnv* env, jlong native_handle) {
64   auto* nl_classifier = reinterpret_cast<NLClassifier*>(native_handle);
65   return env->NewStringUTF(nl_classifier->GetModelVersion().c_str());
66 }
67 
GetLabelsVersionNative(JNIEnv * env,jlong native_handle)68 jstring GetLabelsVersionNative(JNIEnv* env, jlong native_handle) {
69   auto* nl_classifier = reinterpret_cast<NLClassifier*>(native_handle);
70   return env->NewStringUTF(nl_classifier->GetLabelsVersion().c_str());
71 }
72 
73 }  // namespace nlclassifier
74 }  // namespace text
75 }  // namespace task
76 }  // namespace tflite
77