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

Commit 3938349

Browse files
committed
1.3.4版本支持自行配置模型路径
1 parent 509b0b0 commit 3938349

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

OpenNSFW/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99
defaultConfig {
1010
minSdkVersion 16
1111
targetSdkVersion 29
12-
versionCode 3
13-
versionName "1.3.3"
12+
versionCode 4
13+
versionName "1.3.4"
1414

1515
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1616
consumerProguardFiles 'consumer-rules.pro'

OpenNSFW/src/main/java/com/zwy/opennsfw/core/Classifier.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ import org.tensorflow.lite.Interpreter
1212
import org.tensorflow.lite.gpu.GpuDelegate
1313
import java.io.ByteArrayOutputStream
1414
import java.io.File
15+
import java.io.FileInputStream
1516
import java.lang.Math.max
1617
import java.nio.ByteBuffer
1718
import java.nio.ByteOrder
19+
import java.nio.MappedByteBuffer
20+
import java.nio.channels.FileChannel
1821

1922

2023
class Classifier private constructor(config: Config) {
@@ -65,7 +68,7 @@ class Classifier private constructor(config: Config) {
6568
private var instance: Classifier? = null
6669
get() {
6770
if (field == null) {
68-
if (config.context == null) throw RuntimeException("context函数未调用,请使用Classifier.Build().context(context)初始化")
71+
// if (config.context == null) throw RuntimeException("context函数未调用,请使用Classifier.Build().context(context)初始化")
6972
field = Classifier(config)
7073
mClassifier = field
7174
}
@@ -146,6 +149,15 @@ class Classifier private constructor(config: Config) {
146149
}
147150
}
148151

152+
private fun loadModelFile(context: Context): MappedByteBuffer {
153+
val fileDescriptor = context.assets.openFd("nsfw.tflite")
154+
val inputStream = FileInputStream(fileDescriptor.fileDescriptor)
155+
val fileChannel = inputStream.channel
156+
val startOffset = fileDescriptor.startOffset
157+
val declaredLength = fileDescriptor.declaredLength
158+
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength)
159+
}
160+
149161

150162
private fun convertBitmapToByteBuffer(bitmap_: Bitmap) {
151163
imgData.rewind()

demo/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ android {
1111
applicationId "com.zwy.demo"
1212
minSdkVersion 19
1313
targetSdkVersion 29
14-
versionCode 3
15-
versionName "1.3.3"
14+
versionCode 4
15+
versionName "1.3.4"
1616
multiDexEnabled true
1717
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1818
}
@@ -83,7 +83,7 @@ dependencies {
8383
implementation "org.jetbrains.anko:anko:0.10.5"
8484

8585
//NSFW鉴黄库
86-
// implementation 'com.github.devzwy:open_nsfw_android:1.3.2'
86+
// implementation 'com.github.devzwy:open_nsfw_android:1.3.3'
8787
implementation project(path: ':OpenNSFW')
8888
implementation 'pub.devrel:easypermissions:3.0.0'
8989

demo/src/main/java/com/zwy/demo/NSFWApplication.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class NSFWApplication : MultiDexApplication() {
2828

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

0 commit comments

Comments
 (0)