MCPcopy Index your code
hub / github.com/tensorflow/tfjs-examples / readImageAsTensor

Function readImageAsTensor

electron/image_utils.js:35–54  ·  view source on GitHub ↗
(filePath, height, width)

Source from the content-addressed store, hash-verified

33 * `[1, height, width, 3]`
34 */
35export 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
56export const IMAGE_EXTENSION_NAMES = ['jpg', 'jpeg', 'png'];
57

Callers 1

searchFromFilesMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected