pull/34/head
jason 6 years ago
parent 6594cbee85
commit 613b0fa191
  1. 2
      .idea/misc.xml
  2. 11
      app/src/main/java/com/example/open_nsfw_android/MainActivity.kt
  3. 28
      nsfw/src/main/java/com/zwy/nsfw/Classifier.java
  4. 10
      nsfw/src/main/java/com/zwy/nsfw/api/NsfwHelper.java

@ -33,7 +33,7 @@
</profile-state>
</entry>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

@ -76,7 +76,7 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
}
private fun initNsfwHelper() {
nsfwHelper = NsfwHelper.getInstance(this, false, 1)
nsfwHelper = NsfwHelper.getInstance(this, true, 4)
}
private fun reScFromImgs(list: List<LocalMedia>) {
@ -117,4 +117,13 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
}
}
}
override fun onBackPressed() {
finish()
}
override fun onDestroy() {
super.onDestroy()
nsfwHelper?.destroy()
}
}

@ -95,6 +95,25 @@ public class Classifier {
}
tfliteOptions.setNumThreads(numThreads);
tflite = new Interpreter(tfliteModel, tfliteOptions);
Tensor tensor = tflite.getInputTensor(tflite.getInputIndex("input"));
String stringBuilder = " \n"
+"dataType : " +
tensor.dataType() +
"\n" +
"numBytes : " +
tensor.numBytes() +
"\n" +
"numDimensions : " +
tensor.numDimensions() +
"\n" +
"numElements : " +
tensor.numElements() +
"\n" +
"shape : " +
tensor.shape().length;
Log.d(TAG, stringBuilder);
imgData =
ByteBuffer.allocateDirect(
DIM_BATCH_SIZE
@ -185,12 +204,6 @@ public class Classifier {
long startTime = SystemClock.uptimeMillis();
// out
float[][] outArray = new float[1][2];
Tensor aa = tflite.getInputTensor(tflite.getInputIndex("input"));
Log.d(TAG, "dataType : " + aa.dataType());
Log.d(TAG, "numBytes : " + aa.numBytes());
Log.d(TAG, "numDimensions : " + aa.numDimensions());
Log.d(TAG, "numElements : " + aa.numElements());
Log.d(TAG, "shape : " + aa.shape().length);
Log.d(TAG, "lastImgData : " + imgData);
tflite.run(imgData, outArray);
long endTime = SystemClock.uptimeMillis();
@ -206,12 +219,15 @@ public class Classifier {
if (tflite != null) {
tflite.close();
tflite = null;
Log.d(TAG,"Tensorflow Lite Image Classifier close.");
}
if (gpuDelegate != null) {
gpuDelegate.close();
Log.d(TAG,"Tensorflow Lite Image gpuDelegate close.");
gpuDelegate = null;
}
tfliteModel = null;
Log.d(TAG,"Tensorflow Lite destroyed.");
}
}

@ -14,6 +14,14 @@ public class NsfwHelper {
private Activity activity;
private Classifier classifier;
/**
* Creates a classifier with the provided configuration.
*
* @param activity The current Activity.
* @param isAddGpuDelegate Add gpu delegate
* @param numThreads The number of threads to use for classification.
* @return A classifier with the desired configuration.
*/
public static NsfwHelper getInstance(Activity activity, Boolean isAddGpuDelegate, int numThreads) {
synchronized (NsfwHelper.class) {
if (nsfwHelper == null) {
@ -74,7 +82,7 @@ public class NsfwHelper {
nsfwHelper = null;
}
public static interface OnScanBitmapListener {
public interface OnScanBitmapListener {
void onSuccess(float sfw, float nsfw);
}

Loading…
Cancel
Save