aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/examples/android/jni/box_coder_jni.cc
blob: be85414fc10fcabe46f2a3df32ff9634d33cc27a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

// This file loads the box coder mappings.

#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
#include <android/bitmap.h>

#include <jni.h>
#include <pthread.h>
#include <sys/stat.h>
#include <unistd.h>
#include <map>
#include <queue>
#include <sstream>
#include <string>

#include "tensorflow/contrib/android/jni/jni_utils.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/types.h"

#include "tensorflow/examples/android/proto/box_coder.pb.h"

#define TENSORFLOW_METHOD(METHOD_NAME) \
  Java_org_tensorflow_demo_TensorFlowMultiBoxDetector_##METHOD_NAME  // NOLINT

#ifdef __cplusplus
extern "C" {
#endif  // __cplusplus

JNIEXPORT void JNICALL TENSORFLOW_METHOD(loadCoderOptions)(
    JNIEnv* env, jobject thiz, jobject java_asset_manager, jstring location,
    jfloatArray priors);

#ifdef __cplusplus
}  // extern "C"
#endif  // __cplusplus

JNIEXPORT void JNICALL TENSORFLOW_METHOD(loadCoderOptions)(
    JNIEnv* env, jobject thiz, jobject java_asset_manager, jstring location,
    jfloatArray priors) {
  AAssetManager* const asset_manager =
      AAssetManager_fromJava(env, java_asset_manager);
  LOG(INFO) << "Acquired AssetManager.";

  const std::string location_str = GetString(env, location);

  org_tensorflow_demo::MultiBoxCoderOptions multi_options;

  LOG(INFO) << "Reading file to proto: " << location_str;
  ReadFileToProtoOrDie(asset_manager, location_str.c_str(), &multi_options);

  LOG(INFO) << "Read file. " << multi_options.box_coder_size() << " entries.";

  jboolean iCopied = JNI_FALSE;
  jfloat* values = env->GetFloatArrayElements(priors, &iCopied);

  const int array_length = env->GetArrayLength(priors);
  LOG(INFO) << "Array length: " << array_length
            << " (/8 = " << (array_length / 8) << ")";
  CHECK_EQ(array_length % 8, 0);

  const int num_items =
      std::min(array_length / 8, multi_options.box_coder_size());

  for (int i = 0; i < num_items; ++i) {
    const org_tensorflow_demo::BoxCoderOptions& options =
        multi_options.box_coder(i);

    for (int j = 0; j < 4; ++j) {
      const org_tensorflow_demo::BoxCoderPrior& prior = options.priors(j);
      values[i * 8 + j * 2] = prior.mean();
      values[i * 8 + j * 2 + 1] = prior.stddev();
    }
  }
  env->ReleaseFloatArrayElements(priors, values, 0);

  LOG(INFO) << "Read " << num_items << " options";
}