* 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)
| 34 | * `[1, height, width, 3]` |
| 35 | */ |
| 36 | async 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. |
nothing calls this directly
no outgoing calls
no test coverage detected