Skip to content

Commit 6b47060

Browse files
committed
add same classification checks used in Organiser
1 parent 1ffb41e commit 6b47060

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

app/src/main/java/com/fpf/smartscan/ui/screens/test/TestViewModel.kt

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,32 @@ class TestViewModel(application: Application) : AndroidViewModel(application){
5757
val bitmap = getBitmapFromUri(context, uri)
5858
val imageEmbedding = embeddingsHandler?.generateImageEmbedding(bitmap) ?: return@launch
5959
val similarities = getSimilarities(imageEmbedding, prototypeEmbeddings.map { it.embeddings })
60-
val bestIndex = getTopN(similarities, 1, 0.4f).firstOrNull() ?: -1
60+
val top2 = getTopN(similarities, 2)
61+
if(top2.isEmpty()) {
62+
_predictedClass.postValue("No match")
63+
return@launch
64+
}
65+
66+
val bestIndex = top2[0]
67+
val bestSim = similarities[bestIndex]
68+
val secondSim = top2.getOrNull(1)?.let { similarities[it] } ?: 0f
69+
70+
val threshold = 0.4f
71+
val minMargin = 0.05f
72+
73+
if (bestSim < threshold) {
74+
_predictedClass.postValue("No match")
75+
return@launch
76+
}
77+
78+
if((bestSim - secondSim) < minMargin) {
79+
_predictedClass.postValue("No match")
80+
return@launch
81+
}
82+
6183
val result = prototypeEmbeddings.getOrNull(bestIndex)?.id?.let {
62-
DocumentFile.fromTreeUri(context, it.toUri())?.name ?: "Unknown"
63-
} ?: "Unknown"
84+
DocumentFile.fromTreeUri(context, it.toUri())?.name ?: "No match"
85+
} ?: "No match"
6486
_predictedClass.postValue(result)
6587
} catch (e: Exception) {
6688
Log.e("TestViewModelError", "Inference failed: ${e.message}", e)

0 commit comments

Comments
 (0)