-
Notifications
You must be signed in to change notification settings - Fork 3
Home
CaptainDario edited this page Mar 3, 2023
·
2 revisions
Welcome to the DaKanji-Single-Kanji-Recognition wiki!
In this section the usage of this model in different languages is shown. This are only examples and are not proven to be optimal.
Used in DaKanji - desktop, checkout the source
def predict(self, image : np.array, cnt_predictions : int) -> [str]:
""" Predict a character from an input image.
Args:
image (np.array) : A numpy array with shape (1, 64, 64, 1) and dtype 'float32'
cnt_predictions (int) : How many predictions should be returned ('cnt_predictions' most likely ones)
Returns:
A list with the 'cnt_predictions' most confident predictions.
"""
# make prediction
self.kanji_interpreter.set_tensor(self.input_details[0]["index"], image)
self.kanji_interpreter.invoke()
output_data = self.kanji_interpreter.get_tensor(self.output_details[0]["index"])
out_np = np.array(output_data)
# get the 'cnt_predictions' most confident predictions
preds = []
for i in range(cnt_predictions):
pred = self.labels[out_np.argmax()]
preds.append(pred[0])
# 'remove' this prediction from all
out_np[out_np.max() == out_np] = 0.0
return preds
Checkout the source in DaKanji
class HandWritingService {
val interpreter: Interpreter;
val labels: List<Char>;
init {
interpreter = Interpreter(File(App.instance.handWritingModelLocation));
labels = File(App.instance.handWritingLabelsLocation).readText().chars().mapToObj {
Char(it)
}.collect(Collectors.toList());
}
fun recognize(bitmap: Bitmap): List<Pair<Char, Float>> {
val size = bitmap.width;
val inputBuffer = TensorBuffer.createFixedSize(intArrayOf(1, size, size, 1), DataType.FLOAT32);
val intArray = IntArray(size * size);
bitmap.getPixels(intArray, 0, size, 0, 0, size, size);
val floatArray = FloatArray(size * size);
for (i in intArray.indices) {
// we select the alpha channel of the pixel
floatArray[i] = ((intArray[i] shr 24) and 0x000000FF).toFloat();
}
inputBuffer.loadArray(floatArray);
interpreter.resizeInput(0, intArrayOf(1, size, size, 1));
val numOutputs = interpreter.getOutputTensor(0).numElements();
val probabilityBuffer = TensorBuffer.createFixedSize(intArrayOf(1, numOutputs), DataType.FLOAT32);
interpreter.run(inputBuffer.buffer, probabilityBuffer.buffer);
val result = probabilityBuffer.floatArray;
return result.mapIndexed { i, v -> Pair(labels[i], v) }.sortedByDescending { it.second };
}
}