Skip to content

Instantly share code, notes, and snippets.

@tomekdz tomekdz/run.kt
Last active Sep 20, 2018

Embed
What would you like to do?
override fun process(bitmap: Bitmap): List<Detection> {
convertBitmapToByteBuffer(bitmap)
tensorflow.run(imgData, output)
return processOutput(output)
}
private fun convertBitmapToByteBuffer(bitmap: Bitmap) {
imgData.rewind()
val resizedBitmap = Bitmap.createScaledBitmap(bitmap, 416, 416, true)
resizedBitmap.getPixels(intValues, 0, resizedBitmap.width, 0, 0, resizedBitmap.width, resizedBitmap.height)
// Convert the image to floating point.
var pixel = 0
for (i in 0 until 416) {
for (j in 0 until 416) {
val value = intValues[pixel++]
addPixelValue(value)
}
}
}
private fun addPixelValue(pixelValue: Int) {
imgData.putFloat(((pixelValue shr 16 and 0xFF) - IMAGE_MEAN) / IMAGE_STD)
imgData.putFloat(((pixelValue shr 8 and 0xFF) - IMAGE_MEAN) / IMAGE_STD)
imgData.putFloat(((pixelValue and 0xFF) - IMAGE_MEAN) / IMAGE_STD)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.