Skip to content

Instantly share code, notes, and snippets.

View obeshor's full-sized avatar
😊
enthusiastic

Yannick Serge Obam obeshor

😊
enthusiastic
View GitHub Profile
@obeshor
obeshor / importing_librairies.py
Last active July 4, 2019 18:53
Importing librairies
# Install nightly package for some functionalities that aren't in alpha
!pip install tensorflow-gpu==2.0.0-beta1
# Install TF Hub for TF2
!pip install 'tensorflow-hub == 0.5'
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras.layers import Dense, Flatten, Conv2D
@obeshor
obeshor / depend.gradle
Created July 16, 2019 09:45
Add implementation
implementation 'org.tensorflow:tensorflow-lite:1.14.0'
@obeshor
obeshor / snippet.gradle
Created July 16, 2019 09:49
the undermentioned snippet to prevent compressing the model.
aaptOptions {
noCompress "tflite"
}
data class Recognition(
var id: String = "",
var title: String = "",
var confidence: Float = 0F
) {
override fun toString(): String {
return "Title = $title, Confidence = $confidence)"
}
}
@obeshor
obeshor / classifier.kt
Last active July 16, 2019 13:19
classifier class
class Classifier(assetManager: AssetManager, modelPath: String, labelPath: String, inputSize: Int) {
private var INTERPRETER: Interpreter
private var LABEL_LIST: List<String>
private val INPUT_SIZE: Int = inputSize
private val PIXEL_SIZE: Int = 3
private val IMAGE_MEAN = 0
private val IMAGE_STD = 255.0f
private val MAX_RESULTS = 3
private val THRESHOLD = 0.4f
fun recognizeImage(bitmap: Bitmap): List<Classifier.Recognition> {
val scaledBitmap = Bitmap.createScaledBitmap(bitmap, INPUT_SIZE, INPUT_SIZE, false)
val byteBuffer = convertBitmapToByteBuffer(scaledBitmap)
val result = Array(1) { FloatArray(LABEL_LIST.size) }
INTERPRETER.run(byteBuffer, result)
return getSortedResult(result)
}
private fun convertBitmapToByteBuffer(bitmap: Bitmap): ByteBuffer {
val byteBuffer = ByteBuffer.allocateDirect(4 * INPUT_SIZE * INPUT_SIZE * PIXEL_SIZE)
byteBuffer.order(ByteOrder.nativeOrder())
val intValues = IntArray(INPUT_SIZE * INPUT_SIZE)
bitmap.getPixels(intValues, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height)
var pixel = 0
for (i in 0 until INPUT_SIZE) {
for (j in 0 until INPUT_SIZE) {
val `val` = intValues[pixel++]
private fun getSortedResult(labelProbArray: Array<FloatArray>): List<Classifier.Recognition> {
Log.d("Classifier", "List Size:(%d, %d, %d)".format(labelProbArray.size,labelProbArray[0].size,LABEL_LIST.size))
val pq = PriorityQueue(
MAX_RESULTS,
Comparator<Classifier.Recognition> {
(_, _, confidence1), (_, _, confidence2)
-> java.lang.Float.compare(confidence1, confidence2) * -1
})
fun scaleImage(bitmap: Bitmap?): Bitmap {
val orignalWidth = bitmap!!.width
val originalHeight = bitmap.height
val scaleWidth = mInputSize.toFloat() / orignalWidth
val scaleHeight = mInputSize.toFloat() / originalHeight
val matrix = Matrix()
matrix.postScale(scaleWidth, scaleHeight)
return Bitmap.createBitmap(bitmap, 0, 0, orignalWidth, originalHeight, matrix, true)
}
class MainActivity : AppCompatActivity() {
private lateinit var mClassifier: Classifier
private lateinit var mBitmap: Bitmap
private val mCameraRequestCode = 0
private val mGalleryRequestCode = 2
private val mInputSize = 224
private val mModelPath = "plant_disease_model.tflite"
private val mLabelPath = "plant_labels.txt"