fix: [detection] change base machine learning model and configuration

This commit is contained in:
Wisnu Andika
2025-04-16 13:43:30 +07:00
parent 40fa1bfa25
commit 7d5bbf7ad5
3 changed files with 20 additions and 17 deletions

View File

@ -9,9 +9,12 @@ import org.tensorflow.lite.support.image.TensorImage
import org.tensorflow.lite.support.image.ops.ResizeOp import org.tensorflow.lite.support.image.ops.ResizeOp
import org.tensorflow.lite.support.tensorbuffer.TensorBuffer import org.tensorflow.lite.support.tensorbuffer.TensorBuffer
class ImageClassifier(private val context: Context) { class ImageClassifier(private val context: Context) {
private val imageSize = 224 private val imageSize = 224
private val classes = arrayOf("Brown Spots", "Healthy", "Unknown")
private val imageProcessor = ImageProcessor.Builder() private val imageProcessor = ImageProcessor.Builder()
.add(ResizeOp(imageSize, imageSize, ResizeOp.ResizeMethod.BILINEAR)) .add(ResizeOp(imageSize, imageSize, ResizeOp.ResizeMethod.BILINEAR))
.add(NormalizeOp(0f, 255f)) .add(NormalizeOp(0f, 255f))
@ -25,32 +28,32 @@ class ImageClassifier(private val context: Context) {
tensorImage.load(convertedBitmap) tensorImage.load(convertedBitmap)
val processedImage = imageProcessor.process(tensorImage) val processedImage = imageProcessor.process(tensorImage)
val inputFeature0 = TensorBuffer.createFixedSize( val inputFeature0 = TensorBuffer.createFixedSize(intArrayOf(1, imageSize, imageSize, 3), DataType.FLOAT32)
intArrayOf(1, imageSize, imageSize, 3),
DataType.FLOAT32
)
inputFeature0.loadBuffer(processedImage.buffer) inputFeature0.loadBuffer(processedImage.buffer)
val outputs = model.process(inputFeature0) val outputs = model.process(inputFeature0)
val outputFeature0 = outputs.outputFeature0AsTensorBuffer val outputFeature0 = outputs.outputFeature0AsTensorBuffer
val yPred = outputFeature0.floatArray[0]
val confidence = (maxOf(yPred, 1 - yPred) * 100).toFloat() val confidences = outputFeature0.floatArray
val label = when { Log.d("ImageClassifier", "Confidence: ${confidences.size}")
yPred < 0.5f -> "Brown Spots" for (i in confidences.indices) {
yPred > 0.5f -> "Healthy" Log.d("ImageClassifier", "Class $i (${classes[i]}): ${confidences[i]}")
else -> "Uncertain Input"
} }
Log.d("ImageClassifier", "y_pred: $yPred") val maxPos = confidences.indices.maxByOrNull { confidences[it] } ?: -1
Log.d("ImageClassifier", "Label: $label, Confidence: ${String.format("%.1f", confidence)}%") val maxConfidence = (maxOf(confidences[maxPos]) * 100).toFloat()
Log.d("ImageClassifier", "Max Position: $maxPos, Max Confidence: $maxConfidence")
model.close() model.close()
return if (label == "Uncertain Input") { return if (maxPos >= 0 && maxConfidence > THRESHOLD_CONFIDENCE && classes[maxPos] != "Unknown") {
null Pair(classes[maxPos], maxConfidence)
} else { } else {
Pair(label, confidence) null
} }
} }
companion object {
private const val THRESHOLD_CONFIDENCE = 0.5f
}
} }

Binary file not shown.

View File

@ -71,7 +71,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_component" android:layout_marginTop="@dimen/margin_component"
android:layout_marginHorizontal="@dimen/margin_screen" android:layout_marginHorizontal="@dimen/margin_screen"
android:text="Confidence: " android:text=""
android:textColor="@color/primaryGreen" android:textColor="@color/primaryGreen"
android:textSize="18sp" android:textSize="18sp"
android:textStyle="bold" android:textStyle="bold"