Open arofrau opened 5 months ago
fun bestBox(output: FloatArray): List<Box> {
val boxes = mutableListOf<Box>()
for (i in output.indices step 6) {
val x = output[i]
val y = output[i + 1]
val width = output[i + 2]
val height = output[i + 3]
val conf = output[i + 4]
val clsConf = output[i + 5]
val cls = output[i + 6].toInt()
if (cls >= labels.size) {
Log.e("Detector", "Class index $cls is out of bounds for labels array with size ${labels.size}")
continue
}
boxes.add(Box(x, y, width, height, conf, clsConf, cls))
}
return boxes
}
fun detect(image: Bitmap): List<Detection> {
val output = Array(1) { FloatArray(25200) }
interpreter.run(image, output)
val bestBoxes = bestBox(output[0])
val detections = mutableListOf<Detection>()
for (box in bestBoxes) {
if (box.cls >= labels.size) {
Log.e("Detector", "Class index ${box.cls} is out of bounds for labels array with size ${labels.size}")
continue
}
val clsName = labels[box.cls]
detections.add(Detection(box, clsName))
}
return detections
}
I think I have a bug when calculating the maxConf and maxIdx, as this causes the following error to be generated when I open the app: FATAL EXCEPTION: pool-2-thread-1 Process: com.surendramaran.yolov8tflite, PID: 25591 java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 1
On the line (Detector.kt:126:
val clsName = labels[maxIdx]l
and Detector.kt:95 :val bestBoxes = bestBox(output.floatArray)