I am trying to detect person using tensorflow segmentation in android app following the demo from github. Demo contains how to detect segmentation in camera but its only in specific size! My requirement is using static image not live preview. So, I used specific image and try to generate output from it.
MainActivity.kt:
class MainActivity : AppCompatActivity() {
private lateinit var mBinding: ActivityMainBinding
private lateinit var mContext: Activity
private val scope = CoroutineScope(Dispatchers.IO)
private var bitmap: Bitmap? = null
private val TAG = "ImageDetect>>>"
private lateinit var imageSegmentationModel: ImageSegmentationModelExecutor
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(mBinding.root)
mContext = this
imageSegmentationModel = ImageSegmentationModelExecutor(this, true)
Glide.with(this)
.load(R.drawable.sample)
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.listener(object : RequestListener<Drawable> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
setLoading(false)
return false
}
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
bitmap = (resource as BitmapDrawable).bitmap
setLoading(false)
return false
}
})
.into(mBinding.ivSample)
mBinding.btnDetect.setOnClickListener {
bitmap?.let {
setLoading(true)
try {
val bm = ImageUtils.scaleBitmapAndKeepRatio(it, 257, 257)
scope.launch {
val resultBitmap = imageSegmentationModel.execute(bm)
withContext(Dispatchers.Main) {
mBinding.ivSample.setImageBitmap(resultBitmap.bitmapMaskOnly)
setLoading(false)
}
}
} catch (exc: Exception) {
setLoading(false)
Log.e(TAG, Log.getStackTraceString(exc))
}
}
}
}
private fun setLoading(isShow: Boolean) {
if (isShow) mBinding.pbLoading.visibility = View.VISIBLE
else mBinding.pbLoading.visibility = View.GONE
}
}
ImageSegmentationModelExecutor.kt, ImageUtils.kt and ModelExecutionResult.kt are as per github code and there is no change. After that I am executing code Its throws error and catch in ImageSegmentationModelExecutor.kt class something went wrong: y must be < bitmap.height(). So, if we look into code then ImageSegmentationModelExecutor is trying to generate scaledBitmap from original and trying to generate mask from ByteBuffer. So, error must be in these two functions only. I am also understand that Tensorflow only use specific sized input for analyzing image(Here they specified 257) So, I also already converted my original bitmap to 257 size. But still throwing error for bitmap! Is there any solution for these kind of static images with diff sizes?