| 50 | } |
| 51 | |
| 52 | async function readImageTensorFromFile(filePath, height, width) { |
| 53 | return new Promise((resolve, reject) => { |
| 54 | jimp.read(filePath, (err, image) => { |
| 55 | if (err) { |
| 56 | reject(err); |
| 57 | } else { |
| 58 | const h = image.bitmap.height; |
| 59 | const w = image.bitmap.width; |
| 60 | const buffer = tf.buffer([1, h, w, 3], 'float32'); |
| 61 | image.scan(0, 0, w, h, function(x, y, index) { |
| 62 | buffer.set(image.bitmap.data[index], 0, y, x, 0); |
| 63 | buffer.set(image.bitmap.data[index + 1], 0, y, x, 1); |
| 64 | buffer.set(image.bitmap.data[index + 2], 0, y, x, 2); |
| 65 | }); |
| 66 | resolve(tf.tidy(() => tf.image.resizeBilinear( |
| 67 | buffer.toTensor(), [height, width]).div(255))); |
| 68 | } |
| 69 | }); |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | async function main() { |
| 74 | const args = parseArgs(); |