Skip to content
This repository has been archived by the owner on Apr 8, 2023. It is now read-only.

Commit

Permalink
1.3.4版本支持自行配置模型路径
Browse files Browse the repository at this point in the history
  • Loading branch information
devzwy committed Jun 17, 2020
1 parent 509b0b0 commit 3938349
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions OpenNSFW/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 29
versionCode 3
versionName "1.3.3"
versionCode 4
versionName "1.3.4"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
Expand Down
14 changes: 13 additions & 1 deletion OpenNSFW/src/main/java/com/zwy/opennsfw/core/Classifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import org.tensorflow.lite.Interpreter
import org.tensorflow.lite.gpu.GpuDelegate
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileInputStream
import java.lang.Math.max
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.MappedByteBuffer
import java.nio.channels.FileChannel


class Classifier private constructor(config: Config) {
Expand Down Expand Up @@ -65,7 +68,7 @@ class Classifier private constructor(config: Config) {
private var instance: Classifier? = null
get() {
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)
mClassifier = field
}
Expand Down Expand Up @@ -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) {
imgData.rewind()
Expand Down
6 changes: 3 additions & 3 deletions demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "com.zwy.demo"
minSdkVersion 19
targetSdkVersion 29
versionCode 3
versionName "1.3.3"
versionCode 4
versionName "1.3.4"
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
Expand Down Expand Up @@ -83,7 +83,7 @@ dependencies {
implementation "org.jetbrains.anko:anko:0.10.5"

//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 'pub.devrel:easypermissions:3.0.0'

Expand Down
4 changes: 2 additions & 2 deletions demo/src/main/java/com/zwy/demo/NSFWApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class NSFWApplication : MultiDexApplication() {

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

0 comments on commit 3938349

Please sign in to comment.