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

Commit fc21250

Browse files
committed
1.3.5
1 parent 3938349 commit fc21250

File tree

6 files changed

+37
-46
lines changed

6 files changed

+37
-46
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 4
13-
versionName "1.3.4"
12+
versionCode 5
13+
versionName "1.3.5"
1414

1515
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1616
consumerProguardFiles 'consumer-rules.pro'
22.5 MB
Binary file not shown.

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class Classifier private constructor(config: Config) {
6868
private var instance: Classifier? = null
6969
get() {
7070
if (field == null) {
71-
// if (config.context == null) throw RuntimeException("context函数未调用,请使用Classifier.Build().context(context)初始化")
71+
if (config.context == null) throw RuntimeException("context函数未调用,请使用Classifier.Build().context(context)初始化")
7272
field = Classifier(config)
7373
mClassifier = field
7474
}
@@ -115,18 +115,18 @@ class Classifier private constructor(config: Config) {
115115
//"/data/user/0/com.zwy.demo/files/nsfw.tflite"
116116
init {
117117

118-
val file = File(
119-
config.nsfwModuleFilePath
120-
?: throw java.lang.NullPointerException("未配置模型路径,请调用Classifier.Build().nsfwModuleFilePath(模型路径)初始化")
121-
)
122-
if (!file.exists()) throw NullPointerException("模型加载失败,请确认路径是否正确")
118+
// val file = File(
119+
// config.nsfwModuleFilePath
120+
// ?: throw java.lang.NullPointerException("未配置模型路径,请调用Classifier.Build().nsfwModuleFilePath(模型路径)初始化")
121+
// )
122+
// if (!file.exists()) throw NullPointerException("模型加载失败,请确认路径是否正确")
123123
try {
124124
tflite =
125-
Interpreter(file, getTfLiteOptions(config.isOpenGPU))
125+
Interpreter(loadModelFile(context = config.context!!), getTfLiteOptions(config.isOpenGPU))
126126
if (config.isOpenGPU) "开启GPU加速成功".d()
127127
} catch (e: Exception) {
128128
"不支持GPU加速".e()
129-
tflite = Interpreter(file, getTfLiteOptions(false))
129+
tflite = Interpreter(loadModelFile(context = config.context!!), getTfLiteOptions(false))
130130
}
131131

132132
imgData = ByteBuffer.allocateDirect(

README.md

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,38 @@
99
### C++参考:[TensorflowLite-Api](https://tensorflow.google.cn/lite/api_docs/cc)
1010
### JavaScript参考[JS相关文档](https://js.tensorflow.org/api/latest/)
1111
>>> 其中Python、C++均有两种数据喂入的方式,可根据需求选择pb模型或tfLite文件,详细请参考上面的链接.Java的目前只能加载pb模型。其他的平台可自行[百度](https://www.baidu.com)
12-
### [pb模型下载](https://github.com/devzwy/NSFW-Python/tree/master/model)
1312
### 本项目移除测试图片,请下载Demo后自行配图测试
1413
#### 即将优化Demo使用MVVM模式,可用作开发脚手架使用
1514
`Kotlin+okhttp3+rxjava2+retrofit2+koin+glide+greendao+databinding+Livedata`
1615

1716
![MVVM](https://github.com/devzwy/open_nsfw_android/blob/dev/img/4.jpg)
1817

1918

20-
#### 1.3.2版本优化说明:
21-
模型大小改动较大,原量化模型虽小(6M),但对GPU加速支持不友好,新模型大约26M,完美支持GPU加速并优化识别精度,加速效果明显。建议全部升级该版本。__新版本的GPU加速默认开启状态__,SDK默认会检测设备是否支持,不支持时会自动取消加速(老版本会奔溃)
19+
#### 1.3.5版本优化说明:
20+
模型大小改动较大,原量化模型虽小(6M),但对GPU加速支持不友好,新模型大约23M,完美支持GPU加速并优化识别精度,加速效果明显。建议全部升级该版本。__新版本的GPU加速默认开启状态__,SDK默认会检测设备是否支持,不支持时会自动取消加速(老版本会奔溃)
21+
#### 1.3.4版本特殊说明:
22+
由于模型支持GPU加速后模型文件变大,部分用户反馈Apk体积增大,考虑到这方面,在1.3.4版本中增加初始化方式,可通过传入模型文件的路径进行初始化,该模型在`/OpenNSFW/src/main/assets/nsfw.tflite`处,可自行下载并存放适当位置,比如放在后台,app端自行下载后初始化NSFW后使用。初始化方式:
23+
```
24+
Classifier.Build()
25+
// .context(this) //1.3.4版本可不用调用该代码。其他版本必须调用,否则会有异常抛出
26+
// .isOpenGPU(true)//默认不开启GPU加速,默认为true
27+
// .numThreads(100) //分配的线程数 根据手机配置设置,默认1
28+
.nsfwModuleFilePath("/data/user/0/com.zwy.demo/files/nsfw.tflite") //1.3.4版本必须配置模型存放路径,否则会有异常抛出
29+
.build()
30+
```
2231

23-
### 使用方式一 (建议)
32+
33+
### 开始使用
2434
- 添加远程仓库支持
2535
```
2636
allprojects {
2737
repositories {
28-
...
2938
maven { url 'https://jitpack.io' }
3039
}
3140
}
3241
```
3342

34-
- 配置依赖 [![](https://jitpack.io/v/devzwy/open_nsfw_android.svg)](https://jitpack.io/#devzwy/open_nsfw_android) (编译过程报错时请自行使用梯子)
43+
- 配置依赖(如果需要自行配置模型路径可适用1.3.4版本,否则请使用最新版本,版本号看右边的icon中的数字) [![](https://jitpack.io/v/devzwy/open_nsfw_android.svg)](https://jitpack.io/#devzwy/open_nsfw_android) (编译过程报错时请自行使用梯子)
3544

3645
```
3746
dependencies {
@@ -41,43 +50,25 @@
4150
4251
```
4352

44-
### 使用方式二,源码依赖方式
45-
- 克隆源码
46-
```
47-
git clone 'https://github.com/devzwy/open_nsfw_android.git'
48-
```
49-
50-
- 项目中依赖lib
51-
52-
```
53-
dependencies {
54-
implementation project(path: ':OpenNSFW')
55-
}
56-
57-
```
58-
- 扫描时报如下错误
59-
```
60-
java.lang.NullPointerException: Attempt to invoke virtual method 'com.zwy.nsfw.api.NsfwBean com.zwy.nsfw.Classifier.run(android.graphics.Bitmap)' on a null object reference
61-
```
62-
__无论哪种方式依赖项目,都需要添加如下代码__
53+
- 以下配置1.3.4版本可跳过
54+
__除1.3.4版本外,其他任何版本均需要添加如下代码,否则会有`java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed`异常抛出,因为assets下模型文件较大__
6355
```
6456
android {
65-
...
6657
aaptOptions {
6758
noCompress "tflite"
6859
}
6960
}
7061
```
7162

7263

73-
- 使用(请使用最新版本1.3.2)
7464
- 建议在Application中全局初始化
7565

7666
```
7767
Classifier.Build()
78-
.context(this) //必须调用 否则会有异常抛出
79-
// .isOpenGPU(true)//是否开启GPU加速 ,默认true
80-
// .numThreads(10) //分配的线程数 根据手机配置设置,默认1
68+
.context(this) //1.3.4版本可不用调用该代码。其他版本必须调用,否则会有异常抛出
69+
// .isOpenGPU(true)//默认不开启GPU加速,默认为true
70+
// .numThreads(100) //分配的线程数 根据手机配置设置,默认1
71+
// .nsfwModuleFilePath("/data/user/0/com.zwy.demo/files/nsfw.tflite") //1.3.4版本必须配置模型存放路径,否则会有异常抛出
8172
.build()
8273
```
8374
- 使用:

demo/build.gradle

Lines changed: 4 additions & 4 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 4
15-
versionName "1.3.4"
14+
versionCode 5
15+
versionName "1.3.5"
1616
multiDexEnabled true
1717
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1818
}
@@ -83,8 +83,8 @@ dependencies {
8383
implementation "org.jetbrains.anko:anko:0.10.5"
8484

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

9090
implementation 'com.github.LuckSiege.PictureSelector:picture_library:2.2.5'

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) //1.3.4版本可不用调用该代码。其他版本必须调用,否则会有异常抛出
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") //1.3.4版本必须配置模型存放路径,否则会有异常抛出
3535
.build()
3636
//全局注入对象
3737
startKoin {

0 commit comments

Comments
 (0)