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