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

Commit

Permalink
修复部分图片识别不准确的问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
devzwy committed May 22, 2019
1 parent 2f5053f commit 2d2fd8b
Show file tree
Hide file tree
Showing 187 changed files with 184 additions and 249 deletions.
7 changes: 5 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ dependencies {
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 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
// implementation project(path: ':nsfw')
implementation 'com.github.devzwy:open_nsfw_android:1.2.4'
implementation 'com.github.LuckSiege.PictureSelector:picture_library:v2.2.3'
implementation project(path: ':nsfw')
// implementation 'com.github.devzwy:open_nsfw_android:1.2.4'
}
1 change: 0 additions & 1 deletion app/debug/output.json

This file was deleted.

2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.open_nsfw_android">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
Expand All @@ -11,6 +10,7 @@
android:roundIcon="@mipmap/icon"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
105 changes: 90 additions & 15 deletions app/src/main/java/com/example/open_nsfw_android/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,45 +1,120 @@
package com.example.open_nsfw_android

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.zwy.nsfw.api.NsfwHelper
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {
class MainActivity : AppCompatActivity(), View.OnClickListener {

var nsfwHelper: NsfwHelper? = null
var mainAdapter: MainAdapter? = null
var index = 0
val listData: ArrayList<MyNsfwBean> = ArrayList<MyNsfwBean>()
@SuppressLint("SetTextI18n")
var listData: ArrayList<MyNsfwBean> = ArrayList<MyNsfwBean>()
var selectList: List<LocalMedia>? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//assets 目录下的timg-10.jpeg为正常静态图片 ccc.gif 为动态正常图片 可用作测试
// val b = BitmapFactory.decodeStream(resources.assets.open("img/06 (1).jpg"))
// iv.setImageBitmap(b)
nsfwHelper = NsfwHelper.getInstance(this, true, 1)
initNsfwHelper()
initAdapter()
initClickListener()
}

override fun onClick(v: View) {
when (v.id) {
R.id.bt_sc_assets -> {
reScAssetsImgs()
}
R.id.bt_sc_from_other -> {
PictureSelector.create(this)
.openGallery(PictureMimeType.ofImage())//全部.ofAll()、图片.、视频.ofVideo()、音频.ofAudio()
.maxSelectNum(20)// 最大图片选择数量 int
.minSelectNum(1)// 最小选择数量 int
.imageSpanCount(3)// 每行显示个数 int
.selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选 or PictureConfig.SINGLE
.previewImage(true)// 是否可预览图片 true or false
.isCamera(false)// 是否显示拍照按钮 true or false
.isZoomAnim(true)// 图片列表点击 缩放效果 默认true
.selectionMedia(selectList)
.sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
.previewEggs(true)// 预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中) true or false
.forResult(0x01);//结果回调onActivityResult code
}
}
}


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 0x01 && resultCode == RESULT_OK) {
selectList = PictureSelector.obtainMultipleResult(data)
if (selectList != null && selectList?.size ?: 0 > 0)
reScFromImgs(selectList!!)
}
}


private fun initClickListener() {
bt_sc_assets.setOnClickListener(this)
bt_sc_from_other.setOnClickListener(this)
}

private fun initAdapter() {
mainAdapter = MainAdapter(null)
rv.layoutManager = LinearLayoutManager(this)
rv.adapter = mainAdapter
tv_start.setOnClickListener {
for (a in resources.assets.list("img")) {
val path = "img/${a}"
val b = BitmapFactory.decodeStream(resources.assets.open(path))
listData.add(MyNsfwBean(0f, 0f, path, b))
nsfwHelper?.scanBitmap(b) { sfw, nsfw ->
}

private fun initNsfwHelper() {
nsfwHelper = NsfwHelper.getInstance(this, false, 1)
}

private fun reScFromImgs(list: List<LocalMedia>) {
index = 0
mainAdapter?.setNewData(null)
listData = ArrayList<MyNsfwBean>()
Thread(Runnable {
for (lm in list) {
val bitmap = BitmapFactory.decodeFile(lm.path)
listData.add(MyNsfwBean(0.0f, 0.0f, lm.path, bitmap))
nsfwHelper?.scanBitmap(bitmap) { sfw, nsfw ->
listData[index].sfw = sfw
listData[index].nsfw = nsfw
mainAdapter?.addData(listData[index])
mainAdapter?.notifyItemInserted(index)
rv.scrollToPosition(index)
index++
}
}
}).start()
}

private fun reScAssetsImgs() {
index = 0
mainAdapter?.setNewData(null)
listData = ArrayList<MyNsfwBean>()
for (a in resources.assets.list("img")) {
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)
rv.scrollToPosition(index)
index++
}
}

}
}
10 changes: 5 additions & 5 deletions app/src/main/java/com/example/open_nsfw_android/MainAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder

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

@SuppressLint("SetTextI18n")
Expand All @@ -17,15 +17,15 @@ class MainAdapter(val nsfwList: List<MyNsfwBean>?) :
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)
var color = ContextCompat.getColor(mContext, R.color.nsfw1)
when (item.nsfw) {
in 0.0..0.3 -> {
nsfwStr = "正常图片"
color = ContextCompat.getColor(mContext,R.color.nsfw3)
color = ContextCompat.getColor(mContext, R.color.nsfw3)
}
in 0.3..0.7 -> {
in 0.3..0.6 -> {
nsfwStr = "👙比基尼"
color = ContextCompat.getColor(mContext,R.color.nsfw2)
color = ContextCompat.getColor(mContext, R.color.nsfw2)
}
}
textView.text =
Expand Down
54 changes: 20 additions & 34 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,26 @@
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:textSize="18sp"
android:id="@+id/tv_start"
android:gravity="center"
android:layout_gravity="center"
android:padding="20dp"
android:text="点击开始识别Assets下的测试图片"/>
<LinearLayout android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<Button android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp"
android:id="@+id/bt_sc_assets"
android:gravity="center"
android:layout_gravity="center"
android:padding="20dp"
android:text="@string/str_bt1"/>
<Button android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp"
android:id="@+id/bt_sc_from_other"
android:gravity="center"
android:layout_gravity="center"
android:padding="20dp"
android:text="从相册选取"/>
</LinearLayout>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="sfw:适宜在公共场所浏览,nsfw:不适宜在公共场所浏览,两者都是0-1中间的浮点型值,nsfw数值越大表示色情程度越高,sfw反之"
android:padding="15dp"
Expand All @@ -19,31 +32,4 @@
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>-->


</LinearLayout>
3 changes: 1 addition & 2 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="semi_transparent">#66000000</color>
<color name="nsfw1">#20EE1E0D</color>
<color name="nsfw1">#56FF0000</color>
<color name="nsfw2">#20FD9904</color>
<color name="nsfw3">#FFFFFF</color>
<color name="colorAccent">#D81B60</color>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">离线鉴黄</string>
<string name="str_bt1">识别Assets下图片</string>
</resources>
2 changes: 0 additions & 2 deletions nsfw/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
<manifest package="com.zwy.nsfw" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
Loading

0 comments on commit 2d2fd8b

Please sign in to comment.