Skip to content

Instantly share code, notes, and snippets.

@makorowy
Last active March 1, 2018 07:14
Show Gist options
  • Save makorowy/cc10970e8688fa108568e7558073befa to your computer and use it in GitHub Desktop.
Save makorowy/cc10970e8688fa108568e7558073befa to your computer and use it in GitHub Desktop.
TensorFlow Hot or Not example. Classification of the taken photo.
class MainActivity : AppCompatActivity {
//...
private val handler = Handler()
private var photoFilePath = ""
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
val file = File(photoFilePath)
if (requestCode == REQUEST_TAKE_PICTURE && file.exists()) {
classifyPhoto(file)
}
}
private fun classifyPhoto(file: File) {
val photoBitmap = BitmapFactory.decodeFile(file.absolutePath)
val croppedBitmap = ImageUtils.getCroppedBitmap(photoBitmap)
classifyAndShowResult(croppedBitmap)
imagePhoto.setImageBitmap(photoBitmap)
}
private fun classifyAndShowResult(croppedBitmap: Bitmap) {
runInBackground(
Runnable {
val result = classifier.recognizeImage(croppedBitmap)
//show result
})
}
@Synchronized
private fun runInBackground(runnable: Runnable) {
handler.post(runnable)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment