Skip to content

Instantly share code, notes, and snippets.

@makorowy
makorowy / MainActivity.kt
Last active February 25, 2018 18:29
TensorFlow - Hot or Not example. Taking a photo.
private const val REQUEST_PERMISSIONS = 1
private const val REQUEST_TAKE_PICTURE = 2
class MainActivity : AppCompatActivity() {
private var photoFilePath = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
@makorowy
makorowy / UriHelper.kt
Created February 25, 2018 18:46
TensorFlow - Hot or Not example. Creating an Uri from a file path.
object UriHelper {
fun getUriFromFilePath(context: Context, filePath: String): Uri {
val file = File(filePath)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
FileProvider.getUriForFile(
context,
context.applicationContext.packageName + ".uri",
file)
} else {
@makorowy
makorowy / MainActivity.kt
Created February 25, 2018 19:04
TensorFlow - Hot or Not example. Creating a classifier.
class MainActivity : AppCompatActivity() {
private lateinit var classifier: Classifier
//other properties
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
checkPermissions()
@makorowy
makorowy / Classifier.kt
Created February 25, 2018 19:07
TensorFlow - Hot or Not example. Creating a classifier interface.
interface Classifier {
fun recognizeImage(bitmap: Bitmap): Result
}
@makorowy
makorowy / Result.kt
Created February 25, 2018 19:07
TensorFlow - Hot or Not example. Result data class.
data class Result(val result: String, val confidence: Float)
@makorowy
makorowy / Constants.kt
Created February 25, 2018 19:37
TensorFlow - Hot or Not example. Constants.
const val GRAPH_FILE_PATH = "file:///android_asset/graph.pb"
const val LABELS_FILE_PATH = "file:///android_asset/labels.txt"
const val GRAPH_INPUT_NAME = "input"
const val GRAPH_OUTPUT_NAME = "final_result"
const val IMAGE_SIZE = 224
@makorowy
makorowy / MainActivity.kt
Last active March 1, 2018 07:14
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)
@makorowy
makorowy / ImageClassifierFactory.kt
Last active January 17, 2019 09:39
TensorFlow Hot or Not example. Classifier factory.
object ImageClassifierFactory {
fun create(
assetManager: AssetManager,
graphFilePath: String,
labelsFilePath: String,
imageSize: Int,
inputName: String,
outputName: String
): Classifier {
@makorowy
makorowy / ImageClassifier.kt
Created February 28, 2018 06:57
TensorFlow Hot or Not example. Image classifier.
private const val ENABLE_LOG_STATS = false
class ImageClassifier (
private val inputName: String,
private val outputName: String,
private val imageSize: Long,
private val labels: List<String>,
private val imageBitmapPixels: IntArray,
private val imageNormalizedPixels: FloatArray,
private val results: FloatArray,
@makorowy
makorowy / MainActivity.kt
Created March 1, 2018 06:35
TensorFlow Hot or Not example. Show the result.
class MainActivity : AppCompatActivity {
//...
private fun classifyAndShowResult(croppedBitmap: Bitmap) {
runInBackground(
Runnable {
val result = classifier.recognizeImage(croppedBitmap)
showResult(result)
})