Skip to content

Instantly share code, notes, and snippets.

😊
enthusiastic

Yannick Serge Obam obeshor

😊
enthusiastic
Block or report user

Report or block obeshor

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View mainActivity.kt
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"
View scaleImage.kt
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)
}
View getSortedResult.kt
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
})
View convertBitmap.kt
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++]
View recognizeImage.kt
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)
}
View recognition.kt
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 Jul 16, 2019
classifier class
View classifier.kt
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
@obeshor
obeshor / snippet.gradle
Created Jul 16, 2019
the undermentioned snippet to prevent compressing the model.
View snippet.gradle
aaptOptions {
noCompress "tflite"
}
@obeshor
obeshor / depend.gradle
Created Jul 16, 2019
Add implementation
View depend.gradle
implementation 'org.tensorflow:tensorflow-lite:1.14.0'
@obeshor
obeshor / convert.py
Created Jul 4, 2019
Convert the model to TFLite
View convert.py
# convert the model to TFLite
!mkdir "tflite_models"
TFLITE_MODEL = "tflite_models/plant_disease_model.tflite"
# Get the concrete function from the Keras model.
run_model = tf.function(lambda x : reloaded(x))
# Save the concrete function.
concrete_func = run_model.get_concrete_function(
You can’t perform that action at this time.