Skip to content

Commit

Permalink
Merge pull request #58 from DSWU-Unicorn/feature/deepLearningModel1
Browse files Browse the repository at this point in the history
Feature/deep learning model1
  • Loading branch information
yujeongkimm authored May 27, 2023
2 parents 7ea24d3 + c7be418 commit b841b42
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 86 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@ dependencies {


implementation "land.sungbin:systemuicontroller:1.0.1"

// tensoorflow-lite
implementation 'org.tensorflow:tensorflow-lite:2.4.0'
implementation 'org.tensorflow:tensorflow-lite-support:0.1.0'
}
Binary file added app/src/main/assets/CNN.tflite
Binary file not shown.
3 changes: 3 additions & 0 deletions app/src/main/assets/labels.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
metal
paper
plastic
2 changes: 2 additions & 0 deletions app/src/main/assets/labels_lv2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PET
Plastic
Binary file not shown.
Binary file added app/src/main/assets/vanilaCNN_lv2_plastic.tflite
Binary file not shown.
188 changes: 116 additions & 72 deletions app/src/main/java/kr/ac/duksung/rebit/CameraActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kr.ac.duksung.rebit

import android.Manifest
import android.app.Activity
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
Expand All @@ -22,6 +23,7 @@ import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import kr.ac.duksung.rebit.databinding.ActivityCameraBinding
import kr.ac.duksung.rebit.network.RetofitClient
import kr.ac.duksung.rebit.network.RetrofitService
Expand All @@ -32,7 +34,10 @@ import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import com.google.android.gms.location.*
import kotlinx.coroutines.launch
import kr.ac.duksung.rebit.model.Classifier
import kr.ac.duksung.rebit.network.dto.RecycleDetailVO
import java.io.IOException
import java.util.*


Expand All @@ -48,14 +53,17 @@ class CameraActivity : AppCompatActivity() {
private lateinit var mAlertDialog : AlertDialog

private lateinit var content : String
private lateinit var dataLabel : String
lateinit var bitmap: Bitmap
lateinit var imageView: ImageView

private lateinit var textView : TextView
private lateinit var textView2 : TextView
private lateinit var geocoder: Geocoder

// 모델 연결
private lateinit var classifier: Classifier
private var dataLabel : String = ""

private lateinit var binding: ActivityCameraBinding

// 권한을 확인할때의 권한 확인을 위함
Expand All @@ -70,8 +78,9 @@ class CameraActivity : AppCompatActivity() {

geocoder = Geocoder(this)

// dataLabel 설정
dataLabel = "페트병"

// 모델 연결
initClassifier()

// 서버 연결
initRetrofit()
Expand All @@ -85,38 +94,6 @@ class CameraActivity : AppCompatActivity() {



// 분리수거 방법 통신
retrofitService.getRecycle(dataLabel)?.enqueue(object :
Callback<ApiResponse<RecycleVO>> {
override fun onResponse(
call: Call<ApiResponse<RecycleVO>>,
response: Response<ApiResponse<RecycleVO>>
) {
if(response.isSuccessful) {
//정상적으로 통신 성공
val result : ApiResponse<RecycleVO>? = response.body();
val data = result?.getResult();

Log.d("Recycle" ,"onresponse 성공: "+ result?.toString() )
Log.d("Recycle", "data : "+ data)
Log.d("Recycle", "content : "+ data?.content)
content = data!!.content
Log.d("content", "content1 : "+ content)


} else {
//통신 실패(응답코드 3xx, 4xx 등)
Log.d("YMC", "onResponse 실패" + response.errorBody().toString())
}
}

override fun onFailure(call: Call<ApiResponse<RecycleVO>>, t: Throwable) {
//통신 실패(인터넷 끊김, 예외 발생 등 시스템적인 이유)
Log.d("YMC", "onFailure 에러: " + t.message.toString());
}

})

//객체 생성
imageView = findViewById(R.id.imageView)
// 촬영버튼
Expand All @@ -128,54 +105,70 @@ class CameraActivity : AppCompatActivity() {

binding.picBtn.setOnClickListener() {
CallCamera()
binding.startButton.setOnClickListener {
}

// Dialog만들기
val mDialogView =
LayoutInflater.from(this).inflate(R.layout.after_recycle_model_dialog, null)
val mBuilder = AlertDialog.Builder(this)
.setView(mDialogView)
binding.startButton.setOnClickListener {
// Dialog만들기
val mDialogView =
LayoutInflater.from(this).inflate(R.layout.after_recycle_model_dialog, null)
val mBuilder = AlertDialog.Builder(this)
.setView(mDialogView)

mAlertDialog = mBuilder.show()
mAlertDialog = mBuilder.show()

val howto_text = mDialogView.findViewById<TextView>(R.id.howto_text)
val title_text = mDialogView.findViewById<TextView>(R.id.title_text)
howto_text.text = content
title_text.text = dataLabel + "-분리수거 방법"
Log.d("content", "content1 : "+ howto_text)
val howto_text = mDialogView.findViewById<TextView>(R.id.howto_text)
val title_text = mDialogView.findViewById<TextView>(R.id.title_text)
howto_text.text = content
title_text.text = dataLabel + "-분리수거 방법"
Log.d("content", "content1 : "+ howto_text)

/*
val okButton = mDialogView.findViewById<Button>(R.id.successButton)
okButton.setOnClickListener {
postUserPointByRecycle(1L)
/*
val okButton = mDialogView.findViewById<Button>(R.id.successButton)
okButton.setOnClickListener {
postUserPointByRecycle(1L)
Toast.makeText(this, "포인트 획득했습니다!", Toast.LENGTH_SHORT).show()
mAlertDialog.dismiss()
Toast.makeText(this, "포인트 획득했습니다!", Toast.LENGTH_SHORT).show()
mAlertDialog.dismiss()
/**
* 포인트 획득 버튼 누르면 Unity로 돌아가게끔 수정 !
*/
}
/**
* 포인트 획득 버튼 누르면 Unity로 돌아가게끔 수정 !
*/
}
*/

val okButton = mDialogView.findViewById<Button>(R.id.successButton)
okButton.setOnClickListener {
// 현재 위치 찾기
if (checkPermissionForLocation(this)) {
startLocationUpdates()
}

val okButton = mDialogView.findViewById<Button>(R.id.successButton)
okButton.setOnClickListener {
// 현재 위치 찾기
if (checkPermissionForLocation(this)) {
startLocationUpdates()
}

}


val noButton = mDialogView.findViewById<Button>(R.id.AgainButton)
noButton.setOnClickListener {
CallCamera()
}
} //setOnClickListener


val noButton = mDialogView.findViewById<Button>(R.id.AgainButton)
noButton.setOnClickListener {
CallCamera()
}
} //setOnClickListener
}
}//onCreate

private fun initClassifier() {
classifier = Classifier(this, Classifier.IMAGENET_CLASSIFY_MODEL)
try {
classifier.init()
} catch (exception: IOException) {
Toast.makeText(this, "Can not init Classifier!!", Toast.LENGTH_SHORT).show()
}
}

override fun onDestroy() {
classifier.finish()
super.onDestroy()
}

fun checkPermission(permissions: Array<out String>): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
for (permission in permissions) {
Expand Down Expand Up @@ -231,13 +224,22 @@ class CameraActivity : AppCompatActivity() {

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

// 모델 연결
if (resultCode == Activity.RESULT_OK) {
when (requestCode) {
CAMERA_CODE -> {
if (data?.extras?.get("data") != null) {
val img = data?.extras?.get("data") as Bitmap
binding.imageView.setImageBitmap(img)
val output = classifier.classify(img)
val resultStr =
String.format(Locale.ENGLISH, "class : %s", output.first)
binding.run {
binding.textResult.text = resultStr
Log.d("MODEL_RESLT: " , resultStr)
dataLabel = resultStr
binding.imageView.setImageBitmap(img)
getRecycle()
}
}
}
}
Expand All @@ -251,6 +253,48 @@ class CameraActivity : AppCompatActivity() {
retrofitService = retrofit.create(RetrofitService::class.java)
}

private fun getRecycle() {
// 분리수거 방법 통신
lifecycleScope.launch {
try {
Log.d("RECYCLE_DATA_LABEL: ", dataLabel)
retrofitService.getRecycle("Plastic")?.enqueue(object :
Callback<ApiResponse<RecycleVO>> {
override fun onResponse(
call: Call<ApiResponse<RecycleVO>>,
response: Response<ApiResponse<RecycleVO>>
) {
if(response.isSuccessful) {
//정상적으로 통신 성공
val result : ApiResponse<RecycleVO>? = response.body();
val data = result?.getResult();

Log.d("RECYCLE_CAMERA" ,"onresponse 성공: "+ result?.toString() )
Log.d("RECYCLE_CAMERA", "data : "+ data)
Log.d("RECYCLE_CAMERA", "content : "+ data?.content)
content = data!!.content
Log.d("RECYCLE_CAMERA_CONTENT", "content1 : "+ content)


} else {
//통신 실패(응답코드 3xx, 4xx 등)
Log.d("YMC", "onResponse 실패" + response.errorBody().toString())
}
}

override fun onFailure(call: Call<ApiResponse<RecycleVO>>, t: Throwable) {
//통신 실패(인터넷 끊김, 예외 발생 등 시스템적인 이유)
Log.d("YMC", "onFailure 에러: " + t.message.toString());
}

})
} catch (e: Exception) {
// Exception handling
Log.e(ContentValues.TAG, "Exception: ${e.message}", e)
}
}
}

private fun startLocationUpdates() {

//FusedLocationProviderClient의 인스턴스를 생성.
Expand Down
31 changes: 17 additions & 14 deletions app/src/main/java/kr/ac/duksung/rebit/StoreDetailActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ class StoreDetailActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_store_detail)

// activity_store_detail.xml에 설정했던 id 값 사용가능
binding = ActivityStoreDetailBinding.inflate(layoutInflater)
pic_btn.setOnClickListener {
Toast.makeText(this, "내 용기가 맞을까? 확인하러 가기", Toast.LENGTH_SHORT).show()
val intent = Intent(this, CameraActivity::class.java)
startActivity(intent)
}

//setValues()
setupEvents()
Expand All @@ -54,6 +47,16 @@ class StoreDetailActivity : AppCompatActivity() {
val data = intent.getStringExtra("store_id")
val rand = data?.let { Integer.parseInt(it) }
Log.d("store_id", rand.toString())

// activity_store_detail.xml에 설정했던 id 값 사용가능
binding = ActivityStoreDetailBinding.inflate(layoutInflater)
pic_btn.setOnClickListener {
Toast.makeText(this, "내 용기가 맞을까? 확인하러 가기", Toast.LENGTH_SHORT).show()
val intent = Intent(this, YonggiCameraActivity::class.java)
intent.putExtra("store_id", data)
startActivity(intent)
}

// val data = intent.getStringExtra("store_id")
// val rand = data?.let { Integer.parseInt(it) }
// Log.d("store_id", rand.toString())
Expand Down Expand Up @@ -86,13 +89,13 @@ class StoreDetailActivity : AppCompatActivity() {

//storeImageArea.setImageResource(imgId[rand.toInt()])

// val pic_btn = findViewById<Button>(R.id.pic_btn)
// pic_btn.setOnClickListener {
// Toast.makeText(this, "내 용기가 맞을까? 확인하러 가기", Toast.LENGTH_SHORT).show()
//
// val intent = Intent(this, YonggiCameraActivity::class.java)
// intent.putExtra("store_id", data)
// startActivity(intent)
// val pic_btn = findViewById<Button>(R.id.pic_btn)
// pic_btn.setOnClickListener {
// Toast.makeText(this, "내 용기가 맞을까? 확인하러 가기", Toast.LENGTH_SHORT).show()
// //
// val intent = Intent(this, YonggiCameraActivity::class.java)
// intent.putExtra("store_id", data)
// startActivity(intent)
// }
// // review view
val goto_review_btn = findViewById<Button>(R.id.goto_review_btn)
Expand Down
Loading

0 comments on commit b841b42

Please sign in to comment.