Skip to content

Commit 83398fb

Browse files
Fix performance bottlenecks
1 parent efdda03 commit 83398fb

File tree

3 files changed

+11
-22
lines changed

3 files changed

+11
-22
lines changed

lite/examples/posenet/android/app/src/main/java/org/tensorflow/lite/examples/posenet/ImageUtils.kt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,9 @@ object ImageUtils {
3333
var b = expandY + 2066 * uNew
3434

3535
// Clipping RGB values to be inside boundaries [ 0 , MAX_CHANNEL_VALUE ]
36-
val checkBoundaries = { x: Int ->
37-
when {
38-
x > MAX_CHANNEL_VALUE -> MAX_CHANNEL_VALUE
39-
x < 0 -> 0
40-
else -> x
41-
}
42-
}
43-
r = checkBoundaries(r)
44-
g = checkBoundaries(g)
45-
b = checkBoundaries(b)
36+
r = if (r > MAX_CHANNEL_VALUE) MAX_CHANNEL_VALUE else if (r < 0) 0 else r
37+
g = if (g > MAX_CHANNEL_VALUE) MAX_CHANNEL_VALUE else if (g < 0) 0 else g
38+
b = if (b > MAX_CHANNEL_VALUE) MAX_CHANNEL_VALUE else if (b < 0) 0 else b
4639
return -0x1000000 or (r shl 6 and 0xff0000) or (g shr 2 and 0xff00) or (b shr 10 and 0xff)
4740
}
4841

lite/examples/posenet/android/app/src/main/java/org/tensorflow/lite/examples/posenet/PosenetActivity.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import java.util.concurrent.Semaphore
6161
import java.util.concurrent.TimeUnit
6262
import kotlin.math.abs
6363
import org.tensorflow.lite.examples.posenet.lib.BodyPart
64+
import org.tensorflow.lite.examples.posenet.lib.Device
6465
import org.tensorflow.lite.examples.posenet.lib.Person
6566
import org.tensorflow.lite.examples.posenet.lib.Posenet
6667

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

445-
// Process an image for analysis in every 3 frames.
446-
frameCounter = (frameCounter + 1) % 3
447-
if (frameCounter == 0) {
448-
processImage(rotatedBitmap)
449-
}
446+
processImage(rotatedBitmap)
450447
}
451448
}
452449

lite/examples/posenet/android/posenet/src/main/java/org/tensorflow/lite/examples/posenet/lib/Posenet.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,12 @@ class Posenet(
128128

129129
val mean = 128.0f
130130
val std = 128.0f
131-
for (row in 0 until bitmap.height) {
132-
for (col in 0 until bitmap.width) {
133-
val pixelValue = bitmap.getPixel(col, row)
134-
inputBuffer.putFloat(((pixelValue shr 16 and 0xFF) - mean) / std)
135-
inputBuffer.putFloat(((pixelValue shr 8 and 0xFF) - mean) / std)
136-
inputBuffer.putFloat(((pixelValue and 0xFF) - mean) / std)
137-
}
131+
val pixelValues = IntArray(bitmap.height * bitmap.width)
132+
bitmap.getPixels(pixelValues, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height)
133+
for (pixelValue in pixelValues) {
134+
inputBuffer.putFloat(((pixelValue shr 16 and 0xFF) - mean) / std)
135+
inputBuffer.putFloat(((pixelValue shr 8 and 0xFF) - mean) / std)
136+
inputBuffer.putFloat(((pixelValue and 0xFF) - mean) / std)
138137
}
139138
return inputBuffer
140139
}

0 commit comments

Comments
 (0)