Skip to content

Commit

Permalink
优化demo,修复异步扫描时多线程同事操作一块数据时引发的异常。
Browse files Browse the repository at this point in the history
  • Loading branch information
devzwy committed May 20, 2019
1 parent af62081 commit 801bebc
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 76 deletions.
18 changes: 10 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,24 @@ android {
aaptOptions {
noCompress "tflite"
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
assets.srcDir("main/assets")
}
}
// sourceSets {
// main {
// jniLibs.srcDirs = ['libs']
// assets.srcDir("main/assets")
// }
// }
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
// implementation project(path: ':nsfw')
implementation 'com.github.devzwy:open_nsfw_android:1.2.2'
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
implementation project(path: ':nsfw')
// implementation 'com.github.devzwy:open_nsfw_android:1.2.3'
}
Binary file removed app/src/main/assets/img/nsfw_test_01.jpg
Binary file not shown.
Binary file added app/src/main/assets/img/nsfw_test_01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 19 additions & 45 deletions app/src/main/java/com/example/open_nsfw_android/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import android.annotation.SuppressLint
import android.graphics.BitmapFactory
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.support.v7.widget.LinearLayoutManager
import com.zwy.nsfw.api.NsfwHelper
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {
var nsfwHelper: NsfwHelper? = null


var mainAdapter: MainAdapter? = null
var index = 0
val listData: ArrayList<MyNsfwBean> = ArrayList<MyNsfwBean>()
@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -21,51 +22,24 @@ class MainActivity : AppCompatActivity() {
// val b = BitmapFactory.decodeStream(resources.assets.open("img/06 (1).jpg"))
// iv.setImageBitmap(b)
nsfwHelper = NsfwHelper.getInstance(this, true, 1)

bt_.setOnClickListener {
//同步识别
mainAdapter = MainAdapter(null)
rv.layoutManager = LinearLayoutManager(this)
rv.adapter = mainAdapter
tv_start.setOnClickListener {
for (a in resources.assets.list("img")) {
val b = BitmapFactory.decodeStream(resources.assets.open("img/${a}"))
val nsfwBean = nsfwHelper?.scanBitmapSyn(b)
Log.d("demo", nsfwBean.toString() + " - ${a}")
tvv.text = "识别成功:\n\tSFW score : ${nsfwBean?.sfw}\n\tNSFW score : ${nsfwBean?.nsfw},- ${a}"
if (nsfwBean?.nsfw ?: 0f > 0.7) {
tvv.text = "${tvv.text} \n \t - 色情图片"
} else {
tvv.text = "${tvv.text} \n \t - 正常图片"
val path = "img/${a}"
val b = BitmapFactory.decodeStream(resources.assets.open(path))
listData.add(MyNsfwBean(0f, 0f, path, b))
nsfwHelper?.scanBitmap(b) { sfw, nsfw ->
listData[index].sfw = sfw
listData[index].nsfw = nsfw
mainAdapter?.addData(listData[index])
mainAdapter?.notifyItemInserted(index)
index++
}
}
// val nsfwBean = nsfwHelper?.scanBitmapSyn(b)
// Log.d("demo", nsfwBean.toString())
// tvv.text = "识别成功:\n\tSFW score : ${nsfwBean?.sfw}\n\tNSFW score : ${nsfwBean?.nsfw}"
// if (nsfwBean?.nsfw ?: 0f > 0.7) {
// tvv.text = "${tvv.text} \n \t - 色情图片"
// } else {
// tvv.text = "${tvv.text} \n \t - 正常图片"
// }
// //异步识别,接口回调识别结果
// nsfwHelper?.scanBitmap(b) { sfw, nsfw ->
// Log.d("demo", "sfw:$sfw,nsfw:$nsfw")
// }
}
}

// override fun onResume() {
// super.onResume()
// if (!OpenCVLoader.initDebug()) {
// OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
// } else {
// mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
// }
// }
}

// internal var mLoaderCallback: LoaderCallbackInterface = object : LoaderCallbackInterface {
// override fun onManagerConnected(status: Int) {
//
// }
//
// override fun onPackageInstall(operation: Int, callback: InstallCallbackInterface) {
//
// }
// }
}
}
36 changes: 36 additions & 0 deletions app/src/main/java/com/example/open_nsfw_android/MainAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.open_nsfw_android

import android.annotation.SuppressLint
import android.support.v4.content.ContextCompat
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder

class MainAdapter(val nsfwList: List<MyNsfwBean>?) :
BaseQuickAdapter<MyNsfwBean, BaseViewHolder>(R.layout.main_item, nsfwList) {

@SuppressLint("SetTextI18n")
override fun convert(helper: BaseViewHolder, item: MyNsfwBean) {
val textView = helper.getView<TextView>(R.id.tv_text)
val imageView = helper.getView<ImageView>(R.id.iv)
val view = helper.getView<RelativeLayout>(R.id.view)
var nsfwStr = "色情图片"
var color = ContextCompat.getColor(mContext,R.color.nsfw1)
when (item.nsfw) {
in 0.0..0.3 -> {
nsfwStr = "正常图片"
color = ContextCompat.getColor(mContext,R.color.nsfw3)
}
in 0.3..0.7 -> {
nsfwStr = "👙比基尼"
color = ContextCompat.getColor(mContext,R.color.nsfw2)
}
}
textView.text =
"path = ${"img/${item.path}"} \n\nSFW score: ${item.sfw}\nNSFW score: ${item.nsfw} \n\n 鉴定结果: ${nsfwStr}"
imageView.setImageBitmap(item.bitmap)
view.setBackgroundColor(color)
}
}
8 changes: 8 additions & 0 deletions app/src/main/java/com/example/open_nsfw_android/MyNsfwBean.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.open_nsfw_android

import android.graphics.Bitmap
import com.zwy.nsfw.api.NsfwBean

data class MyNsfwBean(var sfw: Float,var nsfw: Float, val path: String,val bitmap:Bitmap) {

}
66 changes: 44 additions & 22 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:layout_width="match_parent"

android:id="@+id/iv" android:layout_height="300dp"/>
<Button android:layout_width="wrap_content"
android:layout_below="@+id/iv"
android:layout_centerHorizontal="true"
android:text="点击识别图片"
android:id="@+id/bt_"
android:gravity="center"
android:layout_height="wrap_content"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@+id/bt_"
android:id="@+id/tv_"
android:textSize="18sp"
android:id="@+id/tv_start"
android:gravity="center"
android:layout_gravity="center"
android:padding="20dp"
android:text="sfw:适宜在公共场所浏览,nsfw:不适宜在公共场所浏览,两者都是0-1中间的浮点型值,nsfw数值越大表示色情程度越高,sfw反之"/>
<TextView
android:id="@+id/tvv"
android:layout_below="@+id/tv_" android:layout_width="match_parent" android:layout_height="match_parent"/>
android:text="点击开始识别Assets下的测试图片"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="sfw:适宜在公共场所浏览,nsfw:不适宜在公共场所浏览,两者都是0-1中间的浮点型值,nsfw数值越大表示色情程度越高,sfw反之"
android:padding="15dp"
android:textSize="14sp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!--<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">-->
<!--<LinearLayout android:layout_width="match_parent"-->
<!--android:orientation="vertical" android:layout_height="match_parent">-->
<!--<Button android:layout_width="wrap_content"-->
<!--android:layout_centerHorizontal="true"-->
<!--android:text="点击开始识别Assets下的测试图片"-->
<!--android:layout_gravity="center"-->
<!--android:id="@+id/bt_"-->
<!--android:gravity="center"-->
<!--android:layout_height="wrap_content"/>-->
<!--<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"-->
<!--android:layout_below="@+id/bt_"-->
<!--android:id="@+id/tv_"-->
<!--android:padding="20dp"-->
<!--android:text="sfw:适宜在公共场所浏览,nsfw:不适宜在公共场所浏览,两者都是0-1中间的浮点型值,nsfw数值越大表示色情程度越高,sfw反之"/>-->
<!--<TextView-->
<!--android:id="@+id/tvv"-->
<!--android:layout_width="match_parent"-->
<!--android:padding="20dp"-->
<!--android:textColor="@color/colorAccent"-->
<!--android:textSize="16sp"-->
<!--android:layout_height="match_parent"/>-->

<!--</LinearLayout>-->
<!--</ScrollView>-->


</RelativeLayout>
</LinearLayout>
18 changes: 18 additions & 0 deletions app/src/main/res/layout/main_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/view"
android:layout_marginTop="5dp"
android:padding="20dp"

android:layout_height="wrap_content">
<TextView android:layout_width="match_parent"
android:id="@+id/tv_text"
android:layout_toLeftOf="@+id/iv"
android:layout_height="wrap_content"/>
<ImageView android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/iv"
android:scaleType="centerCrop"
android:layout_alignParentRight="true"/>
</RelativeLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="semi_transparent">#66000000</color>
<color name="nsfw1">#20EE1E0D</color>
<color name="nsfw2">#20FD9904</color>
<color name="nsfw3">#FFFFFF</color>
<color name="colorAccent">#D81B60</color>

</resources>
Binary file modified img/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions nsfw/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn(nativeLibsToJar)
// implementation project(path: ':openCVLibrary340')
}
17 changes: 16 additions & 1 deletion nsfw/src/main/java/com/zwy/nsfw/api/NsfwHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,26 @@ private NsfwHelper(Activity activity, Boolean isAddGpuDelegate, int numThreads)
}
}


/**
* 同步扫描
*
* @param bitmap
* @return nsfw/sfw
*/
public NsfwBean scanBitmapSyn(Bitmap bitmap) {
return classifier.run(bitmap);
synchronized (NsfwHelper.class) {
return classifier.run(bitmap);
}
}


/**
* 异步扫描
*
* @param bitmap
* @param onScanBitmapListener void onSuccess(float sfw, float nsfw);
*/
public void scanBitmap(final Bitmap bitmap, final OnScanBitmapListener onScanBitmapListener) {
new Thread(new Runnable() {
@Override
Expand Down

0 comments on commit 801bebc

Please sign in to comment.