Skip to content
Permalink
Browse files

make interpreter global and thread safe

  • Loading branch information...
VictorAlbertos committed Mar 6, 2019
1 parent 8df6b69 commit 5e3224a0a0c9a374e472a229fc22d2cdc64617a2
Showing with 16 additions and 6 deletions.
  1. +16 −6 glimpse-core/src/main/java/glimpse/core/BitmapExtensions.kt
@@ -116,6 +116,18 @@ fun Bitmap.debugHeatMap(
}
}

private val intpreter by lazy {
Interpreter(rawModel, Interpreter.Options().apply {
setNumThreads(1)
})
}

@Synchronized
fun Interpreter.runThreadSafe(inputBuffer: ByteBuffer, output: Array<Array<Array<FloatArray>>>) {
run(inputBuffer, output)
}


@JvmOverloads
fun Bitmap.findCenter(
temperature: Float = 0.2f,
@@ -135,11 +147,8 @@ fun Bitmap.findCenter(
pixels.forEach { pixel -> inputBuffer.putFloat((pixel shr 16 and 0xFF) / 255f) }
pixels.forEach { pixel -> inputBuffer.putFloat((pixel shr 8 and 0xFF) / 255f) }
pixels.forEach { pixel -> inputBuffer.putFloat((pixel and 0xFF) / 255f) }
val intpr = Interpreter(rawModel, Interpreter.Options().apply {
setNumThreads(1)
})
intpr.run(inputBuffer, output)
intpr.close()

intpreter.runThreadSafe(inputBuffer, output)

// calculate tempered softmax
val flattened = output[0][0].flattened()
@@ -148,4 +157,5 @@ fun Bitmap.findCenter(

// get averaged center
return MathUtils.getLargestFocusArea(reshaped[0][0], lowerBound = lowerBound)
}
}

0 comments on commit 5e3224a

Please sign in to comment.
You can’t perform that action at this time.