How to get the masked image from Interactive Segmentation of Mediapipe

39 Views Asked by At

I'm working with the next github:

github mediapipe

but when I'm getting the result in the method onResults I need to build another Bitmap of the masked region only, but I'm doing something wrong because I can't build it from the ByteBuffer returned.

override fun onError(error: String) {
        showError(error)
    }

    override fun onResults(result: InteractiveSegmentationHelper.ResultBundle?) {
        // Inform the overlap view to draw over the area of significance returned
        // from the helper
        result?.let {
            activityMainBinding.overlapView.setMaskResult(
                it.byteBuffer,
                it.maskWidth,
                it.maskHeight
            )
        } ?: kotlin.run {
            activityMainBinding.overlapView.clearAll()
        }
    }

I'm using the next two ways to perform this action, but the app crash this both.

fun InteractiveSegmentationHelper.ResultBundle.toBitmap(): Bitmap {
    val byteBuffer = this.byteBuffer
    val maskWidth = this.maskWidth
    val maskHeight = this.maskHeight

    // Crear un arreglo de bytes a partir del ByteBuffer
    val byteArray = ByteArray(byteBuffer.remaining())
    byteBuffer.get(byteArray)

    // Crear un mapa de bits a partir del arreglo de bytes
    val bitmap = Bitmap.createBitmap(maskWidth, maskHeight, Bitmap.Config.ARGB_8888)
    bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(byteArray))

    return bitmap
}


fun InteractiveSegmentationHelper.ResultBundle.toBitmap(): Bitmap {
    val byteBuffer = this.byteBuffer
    val maskWidth = this.maskWidth
    val maskHeight = this.maskHeight

    // Crear un Bitmap a partir del ByteBuffer
    val bitmap = Bitmap.createBitmap(maskWidth, maskHeight, Bitmap.Config.ARGB_8888)

    // Copiar píxeles desde el ByteBuffer al Bitmap
    var copiedPixels = 0
    while (byteBuffer.hasRemaining() && copiedPixels < maskWidth * maskHeight) {
        val pixelsToCopy = minOf(byteBuffer.remaining(), maskWidth * maskHeight - copiedPixels)
        bitmap.copyPixelsFromBuffer(byteBuffer)
        copiedPixels += pixelsToCopy
    }

    // Verificar si se copiaron todos los píxeles
    if (copiedPixels < maskWidth * maskHeight) {
        throw IllegalStateException("No se copiaron todos los píxeles al Bitmap")
    }

    return bitmap
}

The error getted was:

    java.lang.RuntimeException: Buffer not large enough for pixels
at android.graphics.Bitmap.copyPixelsFromBuffer(Bitmap.java:662)...
0

There are 0 best solutions below