Skip to content

Instantly share code, notes, and snippets.

View obeshor's full-sized avatar
😊
enthusiastic

Yannick Serge Obam obeshor

😊
enthusiastic
View GitHub Profile
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
})
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++]
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)
}
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
@obeshor
obeshor / snippet.gradle
Created July 16, 2019 09:49
the undermentioned snippet to prevent compressing the model.
aaptOptions {
noCompress "tflite"
}
@obeshor
obeshor / depend.gradle
Created July 16, 2019 09:45
Add implementation
implementation 'org.tensorflow:tensorflow-lite:1.14.0'
@obeshor
obeshor / convert.py
Created July 4, 2019 18:51
Convert the model to TFLite
# 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(
# Import OpenCV
import cv2
# Utility
import itertools
import random
from collections import Counter
from glob import iglob
def load_image(filename):
img = cv2.imread(os.path.join(data_dir, validation_dir, filename))
img = cv2.resize(img, (IMAGE_SIZE[0], IMAGE_SIZE[1]) )
@obeshor
obeshor / plot.py
Created June 25, 2019 22:42
Check performance
import matplotlib.pylab as plt
import numpy as np
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(EPOCHS)
plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')