Converting path image to new Tensor to a [1, 224, 224, 3] shape

Below is a js function that will do the same processing as in python

const tfnode = require('@tensorflow/tfjs-node')
function processImage(path) {

    const imageSize = 224
    const imageBuffer =  fs.readFileSync(path); // can also use the async readFile instead
    // get tensor out of the buffer
    image = tfnode.node.decodeImage(imageBuffer, 3);
    // dtype to float
    image = image.cast('float32').div(255);
    // resize the image
    image = tf.image.resizeBilinear(image, size = [imageSize, imageSize]); // can also use tf.image.resizeNearestNeighbor
    image = image.expandDims(); // to add the most left axis of size 1
    return image.shape
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top