I'm developing a project where an app should send an image (base64) to the server and do the recognition of the number entered. I'm using MNIST data for training and Tensorflow.js on NodeJS, but I'm having difficulty pre-processing the image before making the prediction. I am using this function to decode base64
const decodeImage = source => {
const buf = Buffer.from(source, 'base64')
return jpeg.decode(buf, true)
}
Then convert image to gray scale array
const convertToGrayscale = image => {
const dataGrayscale = [];
const { data, width, height } = image;
for (let i = 0; i < width * height; i += 1) {
const j = i * 4;
const avg = (data[j + 0] + data[j + 1] + data[j + 2]) / 3;
const normalized = avg / 255.0;
dataGrayscale.push(normalized);
}
return dataGrayscale
}
But the result of the prediction is not going as expected, what other processes should I do to get the correct result? And which is the most correct, white background and black number or the reverse?