|
|
@ -12,9 +12,12 @@ import org.tensorflow.lite.Interpreter |
|
|
|
import org.tensorflow.lite.gpu.GpuDelegate |
|
|
|
import org.tensorflow.lite.gpu.GpuDelegate |
|
|
|
import java.io.ByteArrayOutputStream |
|
|
|
import java.io.ByteArrayOutputStream |
|
|
|
import java.io.File |
|
|
|
import java.io.File |
|
|
|
|
|
|
|
import java.io.FileInputStream |
|
|
|
import java.lang.Math.max |
|
|
|
import java.lang.Math.max |
|
|
|
import java.nio.ByteBuffer |
|
|
|
import java.nio.ByteBuffer |
|
|
|
import java.nio.ByteOrder |
|
|
|
import java.nio.ByteOrder |
|
|
|
|
|
|
|
import java.nio.MappedByteBuffer |
|
|
|
|
|
|
|
import java.nio.channels.FileChannel |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Classifier private constructor(config: Config) { |
|
|
|
class Classifier private constructor(config: Config) { |
|
|
@ -65,7 +68,7 @@ class Classifier private constructor(config: Config) { |
|
|
|
private var instance: Classifier? = null |
|
|
|
private var instance: Classifier? = null |
|
|
|
get() { |
|
|
|
get() { |
|
|
|
if (field == null) { |
|
|
|
if (field == null) { |
|
|
|
if (config.context == null) throw RuntimeException("context函数未调用,请使用Classifier.Build().context(context)初始化") |
|
|
|
// if (config.context == null) throw RuntimeException("context函数未调用,请使用Classifier.Build().context(context)初始化") |
|
|
|
field = Classifier(config) |
|
|
|
field = Classifier(config) |
|
|
|
mClassifier = field |
|
|
|
mClassifier = field |
|
|
|
} |
|
|
|
} |
|
|
@ -146,6 +149,15 @@ class Classifier private constructor(config: Config) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun loadModelFile(context: Context): MappedByteBuffer { |
|
|
|
|
|
|
|
val fileDescriptor = context.assets.openFd("nsfw.tflite") |
|
|
|
|
|
|
|
val inputStream = FileInputStream(fileDescriptor.fileDescriptor) |
|
|
|
|
|
|
|
val fileChannel = inputStream.channel |
|
|
|
|
|
|
|
val startOffset = fileDescriptor.startOffset |
|
|
|
|
|
|
|
val declaredLength = fileDescriptor.declaredLength |
|
|
|
|
|
|
|
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun convertBitmapToByteBuffer(bitmap_: Bitmap) { |
|
|
|
private fun convertBitmapToByteBuffer(bitmap_: Bitmap) { |
|
|
|
imgData.rewind() |
|
|
|
imgData.rewind() |
|
|
|