Skip to content

Instantly share code, notes, and snippets.

View neonankiti's full-sized avatar

Yuki Nanri neonankiti

  • FoodTech Capital.Inc, Bison Holdings.Inc
  • Tokyo
  • X @neonankiti
View GitHub Profile
@neonankiti
neonankiti / ImageClassifier.kt
Last active December 5, 2019 07:11
Pose Estimation with TensorFlow Lite in Android Sample
init {
imgData = ByteBuffer.allocateDirect(
DIM_BATCH_SIZE
* imageSizeX
* imageSizeY
* DIM_PIXEL_SIZE // 3
* numBytesPerChannel
)
imgData!!.order(ByteOrder.nativeOrder())
}
@neonankiti
neonankiti / DrawView.kt
Created December 4, 2019 03:23
Pose Estimation with TensorFlow Lite in Android Sample
fun setDrawPoint(
point: Array<FloatArray>,
ratio: Float
) {
var tempX: Float
var tempY: Float
for (i in 0..13) {
tempX = point[0][i] / ratio / mRatioX
tempY = point[1][i] / ratio / mRatioY
mDrawPoint.add(PointF(tempX, tempY))
@neonankiti
neonankiti / ImageClassifierFloatInception.kt
Created December 4, 2019 03:11
Pose Estimation with TensorFlow Lite in Android Sample
override fun runInference() {
val tempArray = FloatArray(outputW * outputH)
val outTempArray = FloatArray(outputW * outputH)
for (i in 0..13) {
// doing filtering and getting maximum possibility for key points.
mPrintPointArray!![0][i] = maxX
mPrintPointArray!![1][i] = maxY
}
}
@neonankiti
neonankiti / ImageClassifierFloatInception.kt
Created December 4, 2019 03:09
Pose Estimation with TensorFlow Lite in Android Sample
override fun runInference() {
tflite?.run(imgData!!, heatMapArray)
mPrintPointArray = Array(2) { FloatArray(14) }
// Gaussian Filter 5*5
mMat = Mat(outputW, outputH, CvType.CV_32F)
val tempArray = FloatArray(outputW * outputH)
val outTempArray = FloatArray(outputW * outputH)
}
@neonankiti
neonankiti / ImageClassifier.kt
Created December 4, 2019 02:37
Pose Estimation with TensorFlow Lite in Android Sample
fun addPixelValue(pixelValue: Int) {
val red = (pixelValue and 0x00FF0000 shr 16) / 255f * 2.0f - 1.0f
val green = (pixelValue and 0x0000FF00 shr 8) / 255f * 2.0f - 1.0f
val blue = (pixelValue and 0x000000FF) / 255f * 2.0f - 1.0f
}
@neonankiti
neonankiti / ImageClassifier.kt
Created December 4, 2019 00:17
Pose Estimation with TensorFlow Lite in Android Sample
fun addPixelValue(pixelValue: Int) {
//bgr
imgData!!.putFloat((pixelValue and 0xFF).toFloat())
imgData!!.putFloat((pixelValue shr 8 and 0xFF).toFloat())
imgData!!.putFloat((pixelValue shr 16 and 0xFF).toFloat())
}
@neonankiti
neonankiti / ImageClassifier.kt
Created December 4, 2019 00:16
Pose Estimation with TensorFlow Lite in Android Sample
/** Writes Image data into a `ByteBuffer`. */
private fun convertBitmapToByteBuffer(bitmap: Bitmap) {
imgData!!.rewind()
// intValues are input image size memory like IntArray(bitmap.width * bitmap.height)
bitmap.getPixels(intValues, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height)
// Convert the image to floating point.
var pixel = 0
for (i in 0 until imageSizeX) {
for (j in 0 until imageSizeY) {
val v = intValues[pixel++]
@neonankiti
neonankiti / ImageClassifier.kt
Last active December 3, 2019 15:00
Pose Estimation with TensorFlow Lite in Android Sample
@Throws(IOException::class)
private fun loadModelFile(activity: Activity): MappedByteBuffer {
// modelPath would be something like "image_classifier.tflite"
val fileDescriptor = activity.assets.openFd(modelPath)
val inputStream = FileInputStream(fileDescriptor.fileDescriptor)
val fileChannel = inputStream.channel
val startOffset = fileDescriptor.startOffset
val declaredLength = fileDescriptor.declaredLength
return fileChannel.map(MapMode.READ_ONLY, startOffset, declaredLength)
}
@neonankiti
neonankiti / ImageClassifierFloatInception.kt
Last active December 3, 2019 14:36
Pose Estimation with TensorFlow Lite in Android Sample
fun create(
activity: Activity,
imageSizeX: Int = 192,
imageSizeY: Int = 192,
outputW: Int = 96,
outputH: Int = 96,
modelPath: String = "model.tflite",
numBytesPerChannel: Int = 4
): ImageClassifierFloatInception =
ImageClassifierFloatInception(
@neonankiti
neonankiti / Camera2BasicFragment.kt
Created December 3, 2019 14:21
Pose Estimation with TensorFlow Lite in Android Sample
private fun classifyFrame() {
// Get a bitmap with requested width and height.
// Also, this bitmap uses ARGB_8888 format.
val bitmap = textureView!!.getBitmap(classifier!!.imageSizeX, classifier!!.imageSizeY)
// This classifier's function internally has the byte data converted from this bitmap.
classifier!!.classifyFrame(bitmap)
bitmap.recycle()
// mPrintPointArray is the output(results) of inference from input(byte data above)
drawView!!.setDrawPoint(classifier!!.mPrintPointArray!!, 0.5f)
}