This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]) ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implementation 'org.tensorflow:tensorflow-lite:1.14.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
aaptOptions { | |
noCompress "tflite" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Recognition( | |
var id: String = "", | |
var title: String = "", | |
var confidence: Float = 0F | |
) { | |
override fun toString(): String { | |
return "Title = $title, Confidence = $confidence)" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |