Skip to content

Instantly share code, notes, and snippets.

@kongkip
Last active May 16, 2020 09:29
Show Gist options
  • Save kongkip/0cfb858f2b0afb49cbd9203dfb95d9b1 to your computer and use it in GitHub Desktop.
Save kongkip/0cfb858f2b0afb49cbd9203dfb95d9b1 to your computer and use it in GitHub Desktop.
inner class Classify: ImageAnalysis.Analyzer {
@SuppressLint("SetTextI18n")
override fun analyze(imageProxy: ImageProxy?, rotationDegrees: Int) {
// get bitmap from the camera finder view
val imgBitmap = rotateImage(viewFinder.bitmap,
rotationDegrees.toFloat())
val recognitions = imageClassifier!!.classifyImage(
imgBitmap, rotationDegrees)
runOnUiThread {
val empty = " "
textResult1!!.text = recognitions[0].title + empty +
String.format("%.2f%%", recognitions[0].confidence!! * 100.0f)
textResult2!!.text = recognitions[1].title + empty +
String.format("%.2f%%", recognitions[1].confidence!! * 100.0f)
textResult3!!.text = recognitions[2].title + empty+
String.format("%.2f%%", recognitions[2].confidence!! * 100.0f)
}
}
}
// rotate image to its original position
private fun rotateImage(source: Bitmap, angle: Float): Bitmap {
val matrix = Matrix()
matrix.postRotate(angle)
return Bitmap.createBitmap(source, 0, 0, source.width, source.height,
matrix, true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment