MCPcopy Create free account
hub / github.com/tensorflow/tfjs-examples / readImageTensorFromFile

Function readImageTensorFromFile

visualize-convnet/utils.js:36–55  ·  view source on GitHub ↗

* Read an image file as a TensorFlow.js tensor. * * Image resizing is performed with tf.image.resizeBilinear. * * @param {string} filePath Path to the input image file. * @param {number} height Desired height of the output image tensor, in pixels. * @param {number} width Desired width of the o

(filePath, height, width)

Source from the content-addressed store, hash-verified

34 * `[1, height, width, 3]`
35 */
36async function readImageTensorFromFile(filePath, height, width) {
37 return new Promise((resolve, reject) => {
38 jimp.read(filePath, (err, image) => {
39 if (err) {
40 reject(err);
41 } else {
42 const h = image.bitmap.height;
43 const w = image.bitmap.width;
44 const buffer = tf.buffer([1, h, w, 3], 'float32');
45 image.scan(0, 0, w, h, function(x, y, index) {
46 buffer.set(image.bitmap.data[index], 0, y, x, 0);
47 buffer.set(image.bitmap.data[index + 1], 0, y, x, 1);
48 buffer.set(image.bitmap.data[index + 2], 0, y, x, 2);
49 });
50 resolve(tf.tidy(
51 () => tf.image.resizeBilinear(buffer.toTensor(), [height, width])));
52 }
53 });
54 });
55}
56
57/**
58 * Write an image tensor to a image file.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected