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

Commit 2d2fd8b

Browse files
committed
修复部分图片识别不准确的问题。
1 parent 2f5053f commit 2d2fd8b

File tree

187 files changed

+184
-249
lines changed

Some content is hidden

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

187 files changed

+184
-249
lines changed

app/build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ dependencies {
4444
testImplementation 'junit:junit:4.12'
4545
androidTestImplementation 'com.android.support.test:runner:1.0.2'
4646
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
47+
48+
4749
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
48-
// implementation project(path: ':nsfw')
49-
implementation 'com.github.devzwy:open_nsfw_android:1.2.4'
50+
implementation 'com.github.LuckSiege.PictureSelector:picture_library:v2.2.3'
51+
implementation project(path: ':nsfw')
52+
// implementation 'com.github.devzwy:open_nsfw_android:1.2.4'
5053
}

app/debug/output.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.example.open_nsfw_android">
4-
<uses-permission android:name="android.permission.CAMERA"/>
54
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
65
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
76
<application
@@ -11,6 +10,7 @@
1110
android:roundIcon="@mipmap/icon"
1211
android:supportsRtl="true"
1312
android:theme="@style/AppTheme">
13+
1414
<activity android:name=".MainActivity">
1515
<intent-filter>
1616
<action android:name="android.intent.action.MAIN" />
Lines changed: 90 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,120 @@
11
package com.example.open_nsfw_android
22

3-
import android.annotation.SuppressLint
3+
import android.content.Intent
44
import android.graphics.BitmapFactory
55
import android.os.Bundle
66
import android.support.v7.app.AppCompatActivity
77
import android.support.v7.widget.LinearLayoutManager
8+
import android.view.View
9+
import com.luck.picture.lib.PictureSelector
10+
import com.luck.picture.lib.config.PictureConfig
11+
import com.luck.picture.lib.config.PictureMimeType
12+
import com.luck.picture.lib.entity.LocalMedia
813
import com.zwy.nsfw.api.NsfwHelper
914
import kotlinx.android.synthetic.main.activity_main.*
1015

1116

12-
class MainActivity : AppCompatActivity() {
17+
class MainActivity : AppCompatActivity(), View.OnClickListener {
18+
1319
var nsfwHelper: NsfwHelper? = null
1420
var mainAdapter: MainAdapter? = null
1521
var index = 0
16-
val listData: ArrayList<MyNsfwBean> = ArrayList<MyNsfwBean>()
17-
@SuppressLint("SetTextI18n")
22+
var listData: ArrayList<MyNsfwBean> = ArrayList<MyNsfwBean>()
23+
var selectList: List<LocalMedia>? = null
24+
1825
override fun onCreate(savedInstanceState: Bundle?) {
1926
super.onCreate(savedInstanceState)
2027
setContentView(R.layout.activity_main)
21-
//assets 目录下的timg-10.jpeg为正常静态图片 ccc.gif 为动态正常图片 可用作测试
22-
// val b = BitmapFactory.decodeStream(resources.assets.open("img/06 (1).jpg"))
23-
// iv.setImageBitmap(b)
24-
nsfwHelper = NsfwHelper.getInstance(this, true, 1)
28+
initNsfwHelper()
29+
initAdapter()
30+
initClickListener()
31+
}
32+
33+
override fun onClick(v: View) {
34+
when (v.id) {
35+
R.id.bt_sc_assets -> {
36+
reScAssetsImgs()
37+
}
38+
R.id.bt_sc_from_other -> {
39+
PictureSelector.create(this)
40+
.openGallery(PictureMimeType.ofImage())//全部.ofAll()、图片.、视频.ofVideo()、音频.ofAudio()
41+
.maxSelectNum(20)// 最大图片选择数量 int
42+
.minSelectNum(1)// 最小选择数量 int
43+
.imageSpanCount(3)// 每行显示个数 int
44+
.selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选 or PictureConfig.SINGLE
45+
.previewImage(true)// 是否可预览图片 true or false
46+
.isCamera(false)// 是否显示拍照按钮 true or false
47+
.isZoomAnim(true)// 图片列表点击 缩放效果 默认true
48+
.selectionMedia(selectList)
49+
.sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
50+
.previewEggs(true)// 预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中) true or false
51+
.forResult(0x01);//结果回调onActivityResult code
52+
}
53+
}
54+
}
55+
56+
57+
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
58+
super.onActivityResult(requestCode, resultCode, data)
59+
if (requestCode == 0x01 && resultCode == RESULT_OK) {
60+
selectList = PictureSelector.obtainMultipleResult(data)
61+
if (selectList != null && selectList?.size ?: 0 > 0)
62+
reScFromImgs(selectList!!)
63+
}
64+
}
65+
66+
67+
private fun initClickListener() {
68+
bt_sc_assets.setOnClickListener(this)
69+
bt_sc_from_other.setOnClickListener(this)
70+
}
71+
72+
private fun initAdapter() {
2573
mainAdapter = MainAdapter(null)
2674
rv.layoutManager = LinearLayoutManager(this)
2775
rv.adapter = mainAdapter
28-
tv_start.setOnClickListener {
29-
for (a in resources.assets.list("img")) {
30-
val path = "img/${a}"
31-
val b = BitmapFactory.decodeStream(resources.assets.open(path))
32-
listData.add(MyNsfwBean(0f, 0f, path, b))
33-
nsfwHelper?.scanBitmap(b) { sfw, nsfw ->
76+
}
77+
78+
private fun initNsfwHelper() {
79+
nsfwHelper = NsfwHelper.getInstance(this, false, 1)
80+
}
81+
82+
private fun reScFromImgs(list: List<LocalMedia>) {
83+
index = 0
84+
mainAdapter?.setNewData(null)
85+
listData = ArrayList<MyNsfwBean>()
86+
Thread(Runnable {
87+
for (lm in list) {
88+
val bitmap = BitmapFactory.decodeFile(lm.path)
89+
listData.add(MyNsfwBean(0.0f, 0.0f, lm.path, bitmap))
90+
nsfwHelper?.scanBitmap(bitmap) { sfw, nsfw ->
3491
listData[index].sfw = sfw
3592
listData[index].nsfw = nsfw
3693
mainAdapter?.addData(listData[index])
3794
mainAdapter?.notifyItemInserted(index)
95+
rv.scrollToPosition(index)
3896
index++
3997
}
4098
}
99+
}).start()
100+
}
41101

102+
private fun reScAssetsImgs() {
103+
index = 0
104+
mainAdapter?.setNewData(null)
105+
listData = ArrayList<MyNsfwBean>()
106+
for (a in resources.assets.list("img")) {
107+
val path = "img/${a}"
108+
val b = BitmapFactory.decodeStream(resources.assets.open(path))
109+
listData.add(MyNsfwBean(0f, 0f, path, b))
110+
nsfwHelper?.scanBitmap(b) { sfw, nsfw ->
111+
listData[index].sfw = sfw
112+
listData[index].nsfw = nsfw
113+
mainAdapter?.addData(listData[index])
114+
mainAdapter?.notifyItemInserted(index)
115+
rv.scrollToPosition(index)
116+
index++
117+
}
42118
}
43-
44119
}
45120
}

app/src/main/java/com/example/open_nsfw_android/MainAdapter.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import android.widget.TextView
88
import com.chad.library.adapter.base.BaseQuickAdapter
99
import com.chad.library.adapter.base.BaseViewHolder
1010

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

1414
@SuppressLint("SetTextI18n")
@@ -17,15 +17,15 @@ class MainAdapter(val nsfwList: List<MyNsfwBean>?) :
1717
val imageView = helper.getView<ImageView>(R.id.iv)
1818
val view = helper.getView<RelativeLayout>(R.id.view)
1919
var nsfwStr = "色情图片"
20-
var color = ContextCompat.getColor(mContext,R.color.nsfw1)
20+
var color = ContextCompat.getColor(mContext, R.color.nsfw1)
2121
when (item.nsfw) {
2222
in 0.0..0.3 -> {
2323
nsfwStr = "正常图片"
24-
color = ContextCompat.getColor(mContext,R.color.nsfw3)
24+
color = ContextCompat.getColor(mContext, R.color.nsfw3)
2525
}
26-
in 0.3..0.7 -> {
26+
in 0.3..0.6 -> {
2727
nsfwStr = "👙比基尼"
28-
color = ContextCompat.getColor(mContext,R.color.nsfw2)
28+
color = ContextCompat.getColor(mContext, R.color.nsfw2)
2929
}
3030
}
3131
textView.text =

app/src/main/res/layout/activity_main.xml

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,26 @@
44
android:orientation="vertical"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent">
7-
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
8-
android:textSize="18sp"
9-
android:id="@+id/tv_start"
10-
android:gravity="center"
11-
android:layout_gravity="center"
12-
android:padding="20dp"
13-
android:text="点击开始识别Assets下的测试图片"/>
7+
<LinearLayout android:layout_width="match_parent"
8+
android:orientation="horizontal"
9+
android:layout_height="wrap_content">
10+
<Button android:layout_width="0dp" android:layout_height="wrap_content"
11+
android:layout_weight="1"
12+
android:textSize="16sp"
13+
android:id="@+id/bt_sc_assets"
14+
android:gravity="center"
15+
android:layout_gravity="center"
16+
android:padding="20dp"
17+
android:text="@string/str_bt1"/>
18+
<Button android:layout_width="0dp" android:layout_height="wrap_content"
19+
android:layout_weight="1"
20+
android:textSize="16sp"
21+
android:id="@+id/bt_sc_from_other"
22+
android:gravity="center"
23+
android:layout_gravity="center"
24+
android:padding="20dp"
25+
android:text="从相册选取"/>
26+
</LinearLayout>
1427
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
1528
android:text="sfw:适宜在公共场所浏览,nsfw:不适宜在公共场所浏览,两者都是0-1中间的浮点型值,nsfw数值越大表示色情程度越高,sfw反之"
1629
android:padding="15dp"
@@ -19,31 +32,4 @@
1932
android:id="@+id/rv"
2033
android:layout_width="match_parent"
2134
android:layout_height="wrap_content"/>
22-
<!--<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">-->
23-
<!--<LinearLayout android:layout_width="match_parent"-->
24-
<!--android:orientation="vertical" android:layout_height="match_parent">-->
25-
<!--<Button android:layout_width="wrap_content"-->
26-
<!--android:layout_centerHorizontal="true"-->
27-
<!--android:text="点击开始识别Assets下的测试图片"-->
28-
<!--android:layout_gravity="center"-->
29-
<!--android:id="@+id/bt_"-->
30-
<!--android:gravity="center"-->
31-
<!--android:layout_height="wrap_content"/>-->
32-
<!--<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"-->
33-
<!--android:layout_below="@+id/bt_"-->
34-
<!--android:id="@+id/tv_"-->
35-
<!--android:padding="20dp"-->
36-
<!--android:text="sfw:适宜在公共场所浏览,nsfw:不适宜在公共场所浏览,两者都是0-1中间的浮点型值,nsfw数值越大表示色情程度越高,sfw反之"/>-->
37-
<!--<TextView-->
38-
<!--android:id="@+id/tvv"-->
39-
<!--android:layout_width="match_parent"-->
40-
<!--android:padding="20dp"-->
41-
<!--android:textColor="@color/colorAccent"-->
42-
<!--android:textSize="16sp"-->
43-
<!--android:layout_height="match_parent"/>-->
44-
45-
<!--</LinearLayout>-->
46-
<!--</ScrollView>-->
47-
48-
4935
</LinearLayout>

app/src/main/res/values/colors.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
<color name="colorPrimary">#008577</color>
44
<color name="colorPrimaryDark">#00574B</color>
55
<color name="semi_transparent">#66000000</color>
6-
<color name="nsfw1">#20EE1E0D</color>
6+
<color name="nsfw1">#56FF0000</color>
77
<color name="nsfw2">#20FD9904</color>
88
<color name="nsfw3">#FFFFFF</color>
99
<color name="colorAccent">#D81B60</color>
10-
1110
</resources>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<resources>
22
<string name="app_name">离线鉴黄</string>
3+
<string name="str_bt1">识别Assets下图片</string>
34
</resources>

nsfw/src/main/AndroidManifest.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
<manifest package="com.zwy.nsfw" xmlns:android="http://schemas.android.com/apk/res/android">
2-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
3-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
42
</manifest>

0 commit comments

Comments
 (0)