import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
+import android.widget.CompoundButton;
+import android.widget.NumberPicker;
+import android.widget.ToggleButton;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
private boolean runClassifier = false;
private boolean checkedPermissions = false;
private TextView textView;
+ private ToggleButton toggle;
+ private NumberPicker np;
private ImageClassifier classifier;
/** Max preview width that is guaranteed by Camera2 API */
public void onViewCreated(final View view, Bundle savedInstanceState) {
textureView = (AutoFitTextureView) view.findViewById(R.id.texture);
textView = (TextView) view.findViewById(R.id.text);
+ toggle = (ToggleButton) view.findViewById(R.id.button);
+
+ toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+ classifier.setUseNNAPI(isChecked);
+ }
+ });
+
+ np = (NumberPicker) view.findViewById(R.id.np);
+ np.setMinValue(1);
+ np.setMaxValue(10);
+ np.setWrapSelectorWheel(true);
+ np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
+ @Override
+ public void onValueChange(NumberPicker picker, int oldVal, int newVal){
+ classifier.setNumThreads(newVal);
+ }
+ });
}
/** Load the model and labels. */
}
}
+ public void setUseNNAPI(Boolean nnapi) {
+ if (tflite != null)
+ tflite.setUseNNAPI(nnapi);
+ }
+
+ public void setNumThreads(int num_threads) {
+ if (tflite != null)
+ tflite.setNumThreads(num_threads);
+ }
+
/** Closes tflite to release resources. */
public void close() {
tflite.close();
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
+ android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<FrameLayout
android:id="@+id/control"
android:layout_width="match_parent"
- android:layout_height="112dp"
+ android:layout_height="135dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
+ android:layout_alignParentLeft="true"
+ android:layout_alignParentEnd="true"
+ android:layout_alignParentRight="true"
+ android:layout_marginEnd="150dp"
+ android:layout_marginRight="150dp"
android:background="@color/control_background">
- <TextView android:id="@+id/text"
+ <TextView
+ android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
- android:paddingLeft="80dp"
+ android:paddingLeft="20dp"
android:textColor="#FFF"
android:textSize="20sp"
android:textStyle="bold" />
</FrameLayout>
+ <RelativeLayout
+ android:id="@+id/control2"
+ android:layout_width="match_parent"
+ android:layout_height="135dp"
+ android:layout_alignParentLeft="true"
+ android:layout_alignParentStart="true"
+ android:layout_alignTop="@+id/control"
+ android:layout_marginLeft="300dp"
+ android:layout_marginStart="300dp"
+ android:background="@color/control_background">
+
+ <ToggleButton
+ android:id="@+id/button"
+ android:textOff="@string/tflite"
+ android:textOn="@string/nnapi"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_alignParentLeft="true"
+ android:layout_alignParentStart="true" />
+
+ <NumberPicker
+ android:id="@+id/np"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_below="@+id/button"
+ android:visibility="visible" />
+ </RelativeLayout>
+
</RelativeLayout>
<string name="toggle_turn_on">NN:On</string>
<string name="toggle_turn_off">NN:Off</string>
<string name="toggle">Use NNAPI</string>
+ <string name="tflite">tflite</string>
+ <string name="nnapi">NNAPI</string>
</resources>
}
}
+ public void setNumThreads(int num_threads) {
+ if (wrapper == null) {
+ throw new IllegalStateException("The interpreter has already been closed.");
+ }
+ wrapper.setNumThreads(num_threads);
+ }
+
/** Release resources associated with the {@code Interpreter}. */
@Override
public void close() {
useNNAPI(interpreterHandle, useNNAPI);
}
+ void setNumThreads(int num_threads) {
+ numThreads(interpreterHandle, num_threads);
+ }
+
/** Gets index of an input given its name. */
int getInputIndex(String name) {
if (inputsIndexes == null) {
private static native void useNNAPI(long interpreterHandle, boolean state);
+ private static native void numThreads(long interpreterHandle, int num_threads);
+
private static native long createErrorReporter(int size);
private static native long createModel(String modelPathOrBuffer, long errorHandle);
interpreter->UseNNAPI(static_cast<bool>(state));
}
+JNIEXPORT void JNICALL
+Java_org_tensorflow_lite_NativeInterpreterWrapper_numThreads(JNIEnv* env,
+ jclass clazz,
+ jlong handle,
+ jint num_threads) {
+ tflite::Interpreter* interpreter = convertLongToInterpreter(env, handle);
+ if (interpreter == nullptr) return;
+ interpreter->SetNumThreads(static_cast<int>(num_threads));
+}
+
JNIEXPORT jlong JNICALL
Java_org_tensorflow_lite_NativeInterpreterWrapper_createErrorReporter(
JNIEnv* env, jclass clazz, jint size) {
/*
* Class: org_tensorflow_lite_NativeInterpreterWrapper
* Method:
- * Signature: (JZ)
+ * Signature: (JZ)V
*/
JNIEXPORT void JNICALL
Java_org_tensorflow_lite_NativeInterpreterWrapper_useNNAPI(JNIEnv* env,
/*
* Class: org_tensorflow_lite_NativeInterpreterWrapper
* Method:
+ * Signature: (JI)V
+ */
+JNIEXPORT void JNICALL
+Java_org_tensorflow_lite_NativeInterpreterWrapper_numThreads(JNIEnv* env,
+ jclass clazz,
+ jlong handle,
+ jint num_threads);
+/*
+ * Class: org_tensorflow_lite_NativeInterpreterWrapper
+ * Method:
* Signature: (I)J
*/
JNIEXPORT jlong JNICALL