File tree Expand file tree Collapse file tree 1 file changed +25
-3
lines changed
app/src/main/java/com/fpf/smartscan/ui/screens/test Expand file tree Collapse file tree 1 file changed +25
-3
lines changed Original file line number Diff line number Diff 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)
You can’t perform that action at this time.
0 commit comments