function preprocess(imageTensor) {
const widthToHeight = imageTensor.shape[1] / imageTensor.shape[0];
let squareCrop;
if (widthToHeight > 1) {
const heightToWidth = imageTensor.shape[0] / imageTensor.shape[1];
const cropTop = (1-heightToWidth) / 2;
const cropBottom = 1 - cropTop;
squareCrop = [[cropTop, 0, cropBottom, 1]];
} else {
const cropLeft = (1-widthToHeight) / 2;
const cropRight = 1 - cropLeft;
squareCrop = [[0, cropLeft, 1, cropRight]];
}
const crop = tf.image.cropAndResize(imageTensor.expandDims(), squareCrop, [0], [224, 224]);
return crop.div(255);
}