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

Commit d8a2e07

Browse files
committed
重构库,升级到1.3.0
1 parent 66d0f48 commit d8a2e07

47 files changed

Lines changed: 1028 additions & 883 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.idea/codeStyles/Project.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.
Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,36 @@ apply plugin: 'kotlin-android'
33
apply plugin: 'kotlin-android-extensions'
44

55
android {
6-
compileSdkVersion 28
6+
compileSdkVersion 29
7+
buildToolsVersion "29.0.3"
78

89
defaultConfig {
9-
minSdkVersion 19
10-
targetSdkVersion 28
11-
versionCode 6
12-
versionName "1.2.9"
10+
minSdkVersion 16
11+
targetSdkVersion 29
12+
versionCode 1
13+
versionName "1.0"
14+
1315
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16+
consumerProguardFiles 'consumer-rules.pro'
1417
}
1518

1619
buildTypes {
17-
debug {
18-
minifyEnabled false //开启混淆®
20+
release {
21+
minifyEnabled false
1922
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
2023
}
2124
}
22-
kotlin {
23-
experimental {
24-
coroutines 'enable'
25-
}
26-
}
25+
2726
}
2827

2928
dependencies {
3029
implementation fileTree(dir: 'libs', include: ['*.jar'])
30+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
31+
//noinspection GradleCompatible
3132
implementation 'com.android.support:appcompat-v7:28.0.0'
32-
testImplementation 'junit:junit:4.12'
33+
testImplementation 'junit:junit:4.13'
3334
androidTestImplementation 'com.android.support.test:runner:1.0.2'
3435
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
35-
implementation 'org.tensorflow:tensorflow-lite:1.14.0'
36-
implementation 'org.tensorflow:tensorflow-lite-gpu:1.14.0'
37-
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
36+
implementation 'org.tensorflow:tensorflow-lite:2.1.0'
37+
implementation 'org.tensorflow:tensorflow-lite-gpu:2.1.0'
3838
}
39-
repositories {
40-
mavenCentral()
41-
}

OpenNSFW/consumer-rules.pro

Whitespace-only changes.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,3 @@
1919
# If you keep the line number information, uncomment this to
2020
# hide the original source file name.
2121
#-renamesourcefileattribute SourceFile
22-
-keep class com.zwy.nsfw.api.**{*;}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.zwy.opennsfw
2+
3+
import android.support.test.InstrumentationRegistry
4+
import android.support.test.runner.AndroidJUnit4
5+
import org.junit.Assert.assertEquals
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
/**
10+
* Instrumented test, which will execute on an Android device.
11+
*
12+
* See [testing documentation](http://d.android.com/tools/testing).
13+
*/
14+
@RunWith(AndroidJUnit4::class)
15+
class ExampleInstrumentedTest {
16+
@Test
17+
fun useAppContext() {
18+
// Context of the app under test.
19+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
20+
assertEquals("com.zwy.opennsfw.test", appContext.packageName)
21+
}
22+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<manifest
2+
package="com.zwy.opennsfw"/>
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
//package com.zwy.opennsfw
2+
//
3+
//import android.content.Context
4+
//import android.content.res.AssetManager
5+
//import android.graphics.Bitmap
6+
//import android.graphics.Color
7+
//import android.os.SystemClock
8+
//import android.util.Log
9+
//import org.tensorflow.lite.Interpreter
10+
//import org.tensorflow.lite.gpu.GpuDelegate
11+
//import java.io.FileInputStream
12+
//import java.lang.Math.max
13+
//import java.nio.ByteBuffer
14+
//import java.nio.ByteOrder
15+
//import java.nio.MappedByteBuffer
16+
//import java.nio.channels.FileChannel
17+
//
18+
//class Classifier
19+
//private constructor(assetManager: AssetManager, isGPU: Boolean?, numThreads: Int) {
20+
//
21+
// /**
22+
// * 数据宽高
23+
// */
24+
// private val INPUT_WIDTH = 224
25+
//
26+
// /**
27+
// * 数据宽高
28+
// */
29+
// private val INPUT_HEIGHT = 224
30+
//
31+
// /**
32+
// * 通道
33+
// */
34+
// private val BYTES_PER_CHANNEL_NUM = 4
35+
//
36+
// /**
37+
// * Resize后的数据源
38+
// */
39+
// private val intValues = IntArray(INPUT_WIDTH * INPUT_HEIGHT)
40+
//
41+
// /**
42+
// * 载入模型的客户端
43+
// */
44+
// private var tfliteModel: MappedByteBuffer? = null
45+
//
46+
// /**
47+
// * GPU代理
48+
// */
49+
// private var gpuDelegate: GpuDelegate? = null
50+
//
51+
// /**
52+
// * Tensorflow Lite
53+
// */
54+
// private var tflite: Interpreter? = null
55+
//
56+
// /**
57+
// * 喂入模型的最终数据源
58+
// */
59+
// private val imgData: ByteBuffer?
60+
//
61+
//
62+
// init {
63+
// tfliteModel = loadModelFile(assetManager)
64+
// val tfliteOptions = Interpreter.Options()
65+
// if (isGPU == true) {
66+
// gpuDelegate = GpuDelegate()
67+
// tfliteOptions.addDelegate(gpuDelegate)
68+
// }
69+
// tfliteOptions.setNumThreads(numThreads)
70+
// tflite = Interpreter(tfliteModel!!, tfliteOptions)
71+
//
72+
// val tensor = tflite!!.getInputTensor(tflite!!.getInputIndex("input"))
73+
// val stringBuilder = (" \n"
74+
// + "dataType : " +
75+
// tensor.dataType() +
76+
// "\n" +
77+
// "numBytes : " +
78+
// tensor.numBytes() +
79+
// "\n" +
80+
// "numDimensions : " +
81+
// tensor.numDimensions() +
82+
// "\n" +
83+
// "numElements : " +
84+
// tensor.numElements() +
85+
// "\n" +
86+
// "shape : " +
87+
// tensor.shape().size)
88+
// Log.d(TAG, stringBuilder)
89+
//
90+
// imgData = ByteBuffer.allocateDirect(
91+
// DIM_BATCH_SIZE
92+
// * INPUT_WIDTH
93+
// * INPUT_HEIGHT
94+
// * DIM_PIXEL_SIZE
95+
// * BYTES_PER_CHANNEL_NUM
96+
// )
97+
//
98+
// imgData!!.order(ByteOrder.LITTLE_ENDIAN)
99+
// Log.d(TAG, "Tensorflow Lite Image Classifier Initialization Success.")
100+
// }
101+
//
102+
// /**
103+
// * Memory-map the model file in Assets.
104+
// */
105+
// private fun loadModelFile(assetManager: AssetManager): MappedByteBuffer {
106+
// val context:Context
107+
// val fileDescriptor = assetManager.openFd("nsfw.tflite")
108+
// val inputStream = FileInputStream(fileDescriptor.fileDescriptor)
109+
// val fileChannel = inputStream.channel
110+
// val startOffset = fileDescriptor.startOffset
111+
// val declaredLength = fileDescriptor.declaredLength
112+
// return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength)
113+
// }
114+
//
115+
//
116+
// /**
117+
// * Writes Image data into a `ByteBuffer`.
118+
// */
119+
// private fun convertBitmapToByteBuffer(bitmap_: Bitmap) {
120+
// if (imgData == null || bitmap_ == null) {
121+
// return
122+
// }
123+
// imgData.rewind()
124+
// val W = bitmap_.width
125+
// val H = bitmap_.height
126+
//
127+
// val w_off = max((W - INPUT_WIDTH) / 2, 0)
128+
// val h_off = max((H - INPUT_HEIGHT) / 2, 0)
129+
//
130+
// //把每个像素的颜色值转为int 存入intValues
131+
// bitmap_.getPixels(intValues, 0, INPUT_WIDTH, h_off, w_off, INPUT_WIDTH, INPUT_HEIGHT)
132+
// // Convert the image to floating point.
133+
// val startTime = SystemClock.uptimeMillis()
134+
// for (color in intValues) {
135+
// val r1 = Color.red(color)
136+
// val g1 = Color.green(color)
137+
// val b1 = Color.blue(color)
138+
//
139+
// val rr1 = r1 - 123
140+
// val gg1 = g1 - 117
141+
// val bb1 = b1 - 104
142+
//
143+
// imgData.putFloat(bb1.toFloat())
144+
// imgData.putFloat(gg1.toFloat())
145+
// imgData.putFloat(rr1.toFloat())
146+
// }
147+
// val endTime = SystemClock.uptimeMillis()
148+
// Log.d(TAG, "Timecost to put values into ByteBuffer: " + (endTime - startTime) + "ms")
149+
// }
150+
//
151+
// fun run(bitmap: Bitmap): NsfwBean {
152+
//
153+
// val bitmap_256 = Bitmap.createScaledBitmap(bitmap, 256, 256, true)
154+
//
155+
// //Writes image data into byteBuffer
156+
// convertBitmapToByteBuffer(bitmap_256)
157+
//
158+
// val startTime = SystemClock.uptimeMillis()
159+
// // out
160+
// val outArray = Array(1) { FloatArray(2) }
161+
//
162+
// tflite!!.run(imgData, outArray)
163+
//
164+
// val endTime = SystemClock.uptimeMillis()
165+
//
166+
// Log.d(TAG, "SFW score :" + outArray[0][0] + ",NSFW score :" + outArray[0][1])
167+
// Log.d(TAG, "Timecost to run model inference: " + (endTime - startTime) + "ms")
168+
// return NsfwBean(outArray[0][0], outArray[0][1])
169+
// }
170+
//
171+
// /**
172+
// * Closes the interpreter and model to release resources.
173+
// */
174+
// fun close() {
175+
// if (tflite != null) {
176+
// tflite!!.close()
177+
// tflite = null
178+
// Log.d(TAG, "Tensorflow Lite Image Classifier close.")
179+
// }
180+
// if (gpuDelegate != null) {
181+
// gpuDelegate!!.close()
182+
// Log.d(TAG, "Tensorflow Lite Image gpuDelegate close.")
183+
// gpuDelegate = null
184+
// }
185+
// tfliteModel = null
186+
// Log.d(TAG, "Tensorflow Lite destroyed.")
187+
// }
188+
//
189+
// companion object {
190+
//
191+
// val TAG = "open_nsfw_android"
192+
// /**
193+
// * Dimensions of inputs.
194+
// */
195+
// private val DIM_BATCH_SIZE = 1
196+
//
197+
// private val DIM_PIXEL_SIZE = 3
198+
//
199+
// fun create(assetManager: AssetManager, isAddGpuDelegate: Boolean?, numThreads: Int): Classifier {
200+
// return Classifier(assetManager, isAddGpuDelegate!!, numThreads)
201+
// }
202+
//
203+
// }
204+
//
205+
//}

0 commit comments

Comments
 (0)