Skip to content
Permalink
Browse files
Fix performance bottlenecks
  • Loading branch information
miguelrochefort committed Apr 11, 2020
1 parent efdda03 commit 83398fb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
@@ -33,16 +33,9 @@ object ImageUtils {
var b = expandY + 2066 * uNew

// Clipping RGB values to be inside boundaries [ 0 , MAX_CHANNEL_VALUE ]
val checkBoundaries = { x: Int ->
when {
x > MAX_CHANNEL_VALUE -> MAX_CHANNEL_VALUE
x < 0 -> 0
else -> x
}
}
r = checkBoundaries(r)
g = checkBoundaries(g)
b = checkBoundaries(b)
r = if (r > MAX_CHANNEL_VALUE) MAX_CHANNEL_VALUE else if (r < 0) 0 else r
g = if (g > MAX_CHANNEL_VALUE) MAX_CHANNEL_VALUE else if (g < 0) 0 else g
b = if (b > MAX_CHANNEL_VALUE) MAX_CHANNEL_VALUE else if (b < 0) 0 else b
return -0x1000000 or (r shl 6 and 0xff0000) or (g shr 2 and 0xff00) or (b shr 10 and 0xff)
}

@@ -61,6 +61,7 @@ import java.util.concurrent.Semaphore
import java.util.concurrent.TimeUnit
import kotlin.math.abs
import org.tensorflow.lite.examples.posenet.lib.BodyPart
import org.tensorflow.lite.examples.posenet.lib.Device
import org.tensorflow.lite.examples.posenet.lib.Person
import org.tensorflow.lite.examples.posenet.lib.Posenet

@@ -442,11 +443,7 @@ class PosenetActivity :
)
image.close()

// Process an image for analysis in every 3 frames.
frameCounter = (frameCounter + 1) % 3
if (frameCounter == 0) {
processImage(rotatedBitmap)
}
processImage(rotatedBitmap)
}
}

@@ -128,13 +128,12 @@ class Posenet(

val mean = 128.0f
val std = 128.0f
for (row in 0 until bitmap.height) {
for (col in 0 until bitmap.width) {
val pixelValue = bitmap.getPixel(col, row)
inputBuffer.putFloat(((pixelValue shr 16 and 0xFF) - mean) / std)
inputBuffer.putFloat(((pixelValue shr 8 and 0xFF) - mean) / std)
inputBuffer.putFloat(((pixelValue and 0xFF) - mean) / std)
}
val pixelValues = IntArray(bitmap.height * bitmap.width)
bitmap.getPixels(pixelValues, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height)
for (pixelValue in pixelValues) {
inputBuffer.putFloat(((pixelValue shr 16 and 0xFF) - mean) / std)
inputBuffer.putFloat(((pixelValue shr 8 and 0xFF) - mean) / std)
inputBuffer.putFloat(((pixelValue and 0xFF) - mean) / std)
}
return inputBuffer
}

0 comments on commit 83398fb

Please sign in to comment.