Skip to content

Commit bd5173f

Browse files
feat : handle cloud and on device processing separately
1 parent a51b3b1 commit bd5173f

File tree

5 files changed

+113
-66
lines changed

5 files changed

+113
-66
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.github.the_dagger.mlkit
2+
3+
import android.content.Context
4+
import android.support.v4.content.ContextCompat
5+
import android.support.v7.widget.RecyclerView
6+
import android.view.LayoutInflater
7+
import android.view.View
8+
import android.view.ViewGroup
9+
import com.google.firebase.ml.vision.cloud.label.FirebaseVisionCloudLabel
10+
import com.google.firebase.ml.vision.label.FirebaseVisionLabel
11+
import kotlinx.android.synthetic.main.item_row.view.*
12+
13+
class ItemAdapter(private val firebaseVisionList: List<Any>, private val isCloud: Boolean) : RecyclerView.Adapter<ItemAdapter.ItemHolder>() {
14+
lateinit var context: Context
15+
inner class ItemHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
16+
17+
fun bindCloud(currentItem: FirebaseVisionCloudLabel) {
18+
when {
19+
currentItem.confidence > .70 -> itemView.itemAccuracy.setTextColor(ContextCompat.getColor(context, R.color.green))
20+
currentItem.confidence < .30 -> itemView.itemAccuracy.setTextColor(ContextCompat.getColor(context, R.color.red))
21+
else -> itemView.itemAccuracy.setTextColor(ContextCompat.getColor(context, R.color.orange))
22+
}
23+
itemView.itemName.text = currentItem.label
24+
itemView.itemAccuracy.text = "Accuracy : ${(currentItem.confidence * 100).toInt()}%"
25+
}
26+
27+
fun bindDevice(currentItem: FirebaseVisionLabel) {
28+
when {
29+
currentItem.confidence > .70 -> itemView.itemAccuracy.setTextColor(ContextCompat.getColor(context, R.color.green))
30+
currentItem.confidence < .30 -> itemView.itemAccuracy.setTextColor(ContextCompat.getColor(context, R.color.red))
31+
else -> itemView.itemAccuracy.setTextColor(ContextCompat.getColor(context, R.color.orange))
32+
}
33+
itemView.itemName.text = currentItem.label
34+
itemView.itemAccuracy.text = "Accuracy : ${(currentItem.confidence * 100).toInt()}%"
35+
}
36+
37+
}
38+
39+
40+
override fun onBindViewHolder(holder: ItemHolder, position: Int) {
41+
val currentItem = firebaseVisionList[position]
42+
if (isCloud)
43+
holder.bindCloud(currentItem as FirebaseVisionCloudLabel)
44+
else
45+
holder.bindDevice(currentItem as FirebaseVisionLabel)
46+
}
47+
48+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
49+
context = parent.context
50+
return ItemHolder(LayoutInflater.from(context).inflate(R.layout.item_row, parent, false))
51+
}
52+
53+
override fun getItemCount() = firebaseVisionList.size
54+
}

app/src/main/java/io/github/the_dagger/mlkit/ItemsAdapter.kt

Lines changed: 0 additions & 41 deletions
This file was deleted.

app/src/main/java/io/github/the_dagger/mlkit/MainActivity.kt

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,35 @@ package io.github.the_dagger.mlkit
22

33
import android.graphics.Bitmap
44
import android.os.Bundle
5-
import android.support.design.widget.FloatingActionButton
65
import android.support.v7.app.AppCompatActivity
76
import com.google.firebase.ml.vision.FirebaseVision
87
import com.google.firebase.ml.vision.common.FirebaseVisionImage
98
import com.wonderkiln.camerakit.CameraKitImage
109
import android.support.design.widget.BottomSheetBehavior
10+
import android.support.design.widget.Snackbar
1111
import android.support.v7.widget.LinearLayoutManager
12-
import com.google.firebase.ml.vision.label.FirebaseVisionLabel
1312
import kotlinx.android.synthetic.main.activity_main.*
1413
import kotlinx.android.synthetic.main.layout_bottom_sheet.*
1514
import android.view.View
16-
import android.R.attr.bitmap
17-
import com.google.firebase.ml.vision.cloud.label.FirebaseVisionCloudLabelDetector
18-
import android.support.annotation.NonNull
19-
import com.google.android.gms.tasks.OnFailureListener
20-
import com.google.firebase.ml.vision.cloud.label.FirebaseVisionCloudLabel
21-
import com.google.android.gms.tasks.OnSuccessListener
22-
import com.google.android.gms.tasks.Task
15+
import android.widget.Toast
2316

2417

2518
class MainActivity : AppCompatActivity() {
2619

2720
private var itemsList: ArrayList<Any> = ArrayList()
28-
29-
private val itemsAdapter: ItemsAdapter by lazy {
30-
ItemsAdapter(itemsList)
31-
}
21+
private lateinit var itemAdapter: ItemAdapter
3222

3323
private lateinit var sheetBehavior: BottomSheetBehavior<*>
3424

3525
override fun onCreate(savedInstanceState: Bundle?) {
3626
super.onCreate(savedInstanceState)
3727
setContentView(R.layout.activity_main)
38-
val floatingActionButton = findViewById<FloatingActionButton>(R.id.fab_take_photo)
39-
4028
rvLabel.layoutManager = LinearLayoutManager(this)
41-
rvLabel.adapter = itemsAdapter
29+
btnRetry.setOnClickListener {
30+
if (cameraView.visibility == View.VISIBLE) showPreview() else hidePreview()
31+
}
4232

4333
sheetBehavior = BottomSheetBehavior.from(bottomLayout)
44-
4534
sheetBehavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
4635
override fun onStateChanged(bottomSheet: View, newState: Int) {}
4736

@@ -50,10 +39,14 @@ class MainActivity : AppCompatActivity() {
5039
}
5140
})
5241

53-
floatingActionButton.setOnClickListener {
42+
fab_take_photo.setOnClickListener {
5443
fabProgressCircle.show()
55-
camera.captureImage { cameraKitImage ->
44+
cameraView.captureImage { cameraKitImage ->
5645
getLabelsFromClod(captureImage(cameraKitImage))
46+
runOnUiThread {
47+
showPreview()
48+
imagePreview.setImageBitmap(cameraKitImage.bitmap)
49+
}
5750
}
5851
}
5952
}
@@ -67,12 +60,14 @@ class MainActivity : AppCompatActivity() {
6760
// Task completed successfully
6861
fabProgressCircle.hide()
6962
itemsList.addAll(it)
70-
itemsAdapter.notifyDataSetChanged()
63+
itemAdapter = ItemAdapter(itemsList,false)
64+
rvLabel.adapter = itemAdapter
7165
sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
7266
}
7367
.addOnFailureListener {
7468
// Task failed with an exception
7569
fabProgressCircle.hide()
70+
Toast.makeText(baseContext,"Sorry, something went wrong!",Toast.LENGTH_SHORT).show()
7671
}
7772
}
7873

@@ -81,30 +76,41 @@ class MainActivity : AppCompatActivity() {
8176
val detector = FirebaseVision.getInstance()
8277
.visionCloudLabelDetector
8378
itemsList.clear()
84-
val result = detector.detectInImage(image)
79+
detector.detectInImage(image)
8580
.addOnSuccessListener {
8681
// Task completed successfully
8782
fabProgressCircle.hide()
8883
itemsList.addAll(it)
89-
itemsAdapter.notifyDataSetChanged()
84+
itemAdapter = ItemAdapter(itemsList,true)
85+
rvLabel.adapter = itemAdapter
9086
sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
9187
}
9288
.addOnFailureListener {
9389
// Task failed with an exception
9490
fabProgressCircle.hide()
91+
Toast.makeText(baseContext,"Sorry, something went wrong!",Toast.LENGTH_SHORT).show()
9592
}
9693
}
9794

9895
override fun onResume() {
9996
super.onResume()
100-
camera.start()
97+
cameraView.start()
10198
}
10299

103100
override fun onPause() {
104-
camera.stop()
101+
cameraView.stop()
105102
super.onPause()
106103
}
107104

105+
private fun showPreview() {
106+
framePreview.visibility = View.VISIBLE
107+
cameraView.visibility = View.GONE
108+
}
109+
110+
private fun hidePreview() {
111+
framePreview.visibility = View.GONE
112+
cameraView.visibility = View.VISIBLE
113+
}
108114

109115
private fun captureImage(cameraKitImage: CameraKitImage): Bitmap {
110116
return cameraKitImage.bitmap
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#FFFFFF"
2+
android:viewportHeight="24.0" android:viewportWidth="24.0"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="#FF000000" android:pathData="M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z"/>
5+
</vector>

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,40 @@
55
android:layout_height="match_parent">
66

77
<com.wonderkiln.camerakit.CameraView
8-
android:id="@+id/camera"
8+
android:id="@+id/cameraView"
99
android:layout_width="match_parent"
1010
android:layout_height="wrap_content"
1111
android:adjustViewBounds="true" />
1212

13+
<FrameLayout
14+
android:visibility="gone"
15+
android:id="@+id/framePreview"
16+
android:layout_width="match_parent"
17+
android:layout_height="match_parent">
18+
19+
<ImageView
20+
android:layout_width="match_parent"
21+
android:layout_height="match_parent"
22+
android:id="@+id/imagePreview"/>
23+
24+
<ImageButton
25+
android:layout_gravity="center"
26+
android:background="@null"
27+
android:scaleType="centerCrop"
28+
android:layout_width="120dp"
29+
android:layout_height="120dp"
30+
android:id="@+id/btnRetry"
31+
android:src="@drawable/ic_refresh"/>
32+
33+
</FrameLayout>
34+
1335
<include
1436
android:id="@+id/bottomLayout"
1537
layout="@layout/layout_bottom_sheet" />
1638

1739
<com.github.jorgecastilloprz.FABProgressCircle
1840
android:id="@+id/fabProgressCircle"
41+
app:arcWidth="4dp"
1942
android:layout_width="wrap_content"
2043
app:layout_anchor="@id/bottomLayout"
2144
app:layout_anchorGravity="end"

0 commit comments

Comments
 (0)