Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tomekdz
Last active September 20, 2018 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomekdz/c1e8f1e1ca21a735b4ababe4a3feaf03 to your computer and use it in GitHub Desktop.
Save tomekdz/c1e8f1e1ca21a735b4ababe4a3feaf03 to your computer and use it in GitHub Desktop.
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