I have a problem when I analyze the photo and still stuck until now. I have 3 models which is Cancer, Non-cancer, and unknown. when I try to analyze the result is always “unknown” does anyone know how to solve it? I tried to change my code and asking ai too and still stuck.
Hello, I have a problem when I analyze the photo and still stuck until now. I have 3 models which is Cancer, Non-cancer, and unknown. when I try to analyze the result is always “unknown” does anyone know how to solve it? I tried to change my code and asking ai too and still stuck here is my code
ImageClassifierHelper
class ImageClassifierHelper(
private var threshold: Float = 0.5f, // Tingkatkan threshold
private var maxResult: Int = 3,
private val modelName: String = "model_unquant.tflite",
val context: Context,
val classifierListener: ClassifierListener?
) {
interface ClassifierListener {
fun onError(error: String)
fun onResults(result: MutableList<Category>?)
}
private var imageClassifier: ModelUnquant? = null
init {
setupImageClassifier()
}
private fun setupImageClassifier() {
try {
imageClassifier = ModelUnquant.newInstance(context)
} catch (e: IOException) {
classifierListener?.onError(context.getString(R.string.failed))
Log.e(TAG, e.message.toString())
}
}
companion object {
private const val TAG = "ImageClassifierHelper"
}
// Tambahkan daftar label yang sesuai dengan model Anda
private val labels = listOf("Non-Cancer", "Cancer", "Unknown")
fun classifyStaticImage(imageUri: Uri) {
if (imageClassifier == null) {
setupImageClassifier()
}
// Mengonversi Uri ke Bitmap
val bitmap = uriToBitmap(context.contentResolver, imageUri)
?: run {
classifierListener?.onError("Failed to decode image")
return
}
if (bitmap == null) {
classifierListener?.onError("Failed to decode image")
return
}
val resizedBitmap = Bitmap.createScaledBitmap(bitmap, 224, 224, true)
// Mengonversi Bitmap ke TensorImage
val tensorImage = TensorImage(DataType.FLOAT32)
tensorImage.load(resizedBitmap)
// Membuat TensorBuffer untuk input
val inputFeature0 = TensorBuffer.createFixedSize(intArrayOf(1, 224, 224, 3), DataType.FLOAT32)
inputFeature0.loadBuffer(tensorImage.buffer)
// Menjalankan inferensi model dan mendapatkan hasil
val outputs = imageClassifier?.process(inputFeature0)
val outputFeature0 = outputs?.outputFeature0AsTensorBuffer
// Mengonversi hasil ke daftar kategori
val probability = outputFeature0?.let { tensorBuffer ->
tensorBuffer.floatArray.mapIndexed { index, value ->
Category(labels.getOrElse(index) { "Unknown" }, value)
}.filter { it.score >= threshold } // Filter berdasarkan threshold
.toMutableList()
}
classifierListener?.onResults(probability)
}
private fun uriToBitmap(contentResolver: ContentResolver, uri: Uri): Bitmap? {
return try {
val inputStream = contentResolver.openInputStream(uri)
BitmapFactory.decodeStream(inputStream)
} catch (e: IOException) {
e.printStackTrace()
null
}
}
}
Lili is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.