1.3.4版本支持自行配置模型路径

pull/34/head 1.3.4
Jason 5 years ago
parent 509b0b0f52
commit 3938349e08
  1. 4
      OpenNSFW/build.gradle
  2. 14
      OpenNSFW/src/main/java/com/zwy/opennsfw/core/Classifier.kt
  3. 6
      demo/build.gradle
  4. 4
      demo/src/main/java/com/zwy/demo/NSFWApplication.kt

@ -9,8 +9,8 @@ android {
defaultConfig { defaultConfig {
minSdkVersion 16 minSdkVersion 16
targetSdkVersion 29 targetSdkVersion 29
versionCode 3 versionCode 4
versionName "1.3.3" versionName "1.3.4"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro' consumerProguardFiles 'consumer-rules.pro'

@ -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()

@ -11,8 +11,8 @@ android {
applicationId "com.zwy.demo" applicationId "com.zwy.demo"
minSdkVersion 19 minSdkVersion 19
targetSdkVersion 29 targetSdkVersion 29
versionCode 3 versionCode 4
versionName "1.3.3" versionName "1.3.4"
multiDexEnabled true multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
} }
@ -83,7 +83,7 @@ dependencies {
implementation "org.jetbrains.anko:anko:0.10.5" implementation "org.jetbrains.anko:anko:0.10.5"
//NSFW鉴黄库 //NSFW鉴黄库
// implementation 'com.github.devzwy:open_nsfw_android:1.3.2' // implementation 'com.github.devzwy:open_nsfw_android:1.3.3'
implementation project(path: ':OpenNSFW') implementation project(path: ':OpenNSFW')
implementation 'pub.devrel:easypermissions:3.0.0' implementation 'pub.devrel:easypermissions:3.0.0'

@ -28,10 +28,10 @@ class NSFWApplication : MultiDexApplication() {
//初始化鉴黄库NSFW //初始化鉴黄库NSFW
Classifier.Build() Classifier.Build()
.context(this) //必须调用 否则会有异常抛出 // .context(this) //必须调用 否则会有异常抛出
// .isOpenGPU(true)//默认不开启GPU加速,默认为true // .isOpenGPU(true)//默认不开启GPU加速,默认为true
// .numThreads(100) //分配的线程数 根据手机配置设置,默认1 // .numThreads(100) //分配的线程数 根据手机配置设置,默认1
// .nsfwModuleFilePath("/data/user/0/com.zwy.demo/files/nsfw.tflite") .nsfwModuleFilePath("/data/user/0/com.zwy.demo/files/nsfw.tflite")
.build() .build()
//全局注入对象 //全局注入对象
startKoin { startKoin {

Loading…
Cancel
Save