• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tensorflow_lite_support/java/src/native/task/vision/jni_utils.h"
17 
18 #include "absl/strings/str_cat.h"
19 #include "tensorflow_lite_support/cc/utils/jni_utils.h"
20 
21 namespace tflite {
22 namespace task {
23 namespace vision {
24 
25 using ::tflite::support::utils::kAssertionError;
26 using ::tflite::support::utils::ThrowException;
27 
28 constexpr char kCategoryClassName[] =
29     "org/tensorflow/lite/support/label/Category";
30 constexpr char kStringClassName[] = "Ljava/lang/String;";
31 constexpr char kEmptyString[] = "";
32 
ConvertToCategory(JNIEnv * env,const Class & classification)33 jobject ConvertToCategory(JNIEnv* env, const Class& classification) {
34   // jclass and init of Category.
35   jclass category_class = env->FindClass(kCategoryClassName);
36   jmethodID category_create = env->GetStaticMethodID(
37       category_class, "create",
38       absl::StrCat("(", kStringClassName, kStringClassName, "F)L",
39                    kCategoryClassName, ";")
40           .c_str());
41 
42   std::string label_string = classification.has_class_name()
43                                  ? classification.class_name()
44                                  : std::to_string(classification.index());
45   jstring label = env->NewStringUTF(label_string.c_str());
46   std::string display_name_string = classification.has_display_name()
47                                         ? classification.display_name()
48                                         : kEmptyString;
49   jstring display_name = env->NewStringUTF(display_name_string.c_str());
50   jobject jcategory =
51       env->CallStaticObjectMethod(category_class, category_create, label,
52                                   display_name, classification.score());
53   return jcategory;
54 }
55 
ConvertToFrameBufferOrientation(JNIEnv * env,jint jorientation)56 FrameBuffer::Orientation ConvertToFrameBufferOrientation(JNIEnv* env,
57                                                          jint jorientation) {
58   switch (jorientation) {
59     case 0:
60       return FrameBuffer::Orientation::kTopLeft;
61     case 1:
62       return FrameBuffer::Orientation::kTopRight;
63     case 2:
64       return FrameBuffer::Orientation::kBottomRight;
65     case 3:
66       return FrameBuffer::Orientation::kBottomLeft;
67     case 4:
68       return FrameBuffer::Orientation::kLeftTop;
69     case 5:
70       return FrameBuffer::Orientation::kRightTop;
71     case 6:
72       return FrameBuffer::Orientation::kRightBottom;
73     case 7:
74       return FrameBuffer::Orientation::kLeftBottom;
75   }
76   // Should never happen.
77   ThrowException(env, kAssertionError,
78                  "The FrameBuffer Orientation type is unsupported: %d",
79                  jorientation);
80   return FrameBuffer::Orientation::kTopLeft;
81 }
82 
83 }  // namespace vision
84 }  // namespace task
85 }  // namespace tflite
86