Folder structure:
- data
the script succeeded to scan the "data" directory and it's sub folder, resize the images, save them in the corresponding folder in "resized" directory and return "images" array with "label" and "path".
let path = require('path');
let fs = require('fs');
let sd = require('scandirectory');
let sharp = require('sharp');
// params
const IMG_HEIGHT = 150
const IMG_WIDTH = 150
const options = {}
let uniq_labels = []
let images = []
// let images_tensor = {}
const data_path = path.join(__dirname, '../data/');
const resized_path = path.join(__dirname, '../resized/');
// resize images
sd(data_path, options, scanDirCb);
function scanDirCb(err, list, tree) {
let labels = []
if (err) {
console.log('Unable to scan directory: ' + err);
return
} else {
// loop
Object.entries(tree).forEach(([folder, label]) => {
// create sub folder
if (!fs.existsSync(resized_path + folder)) {
fs.mkdirSync(resized_path + folder);
}
Object.entries(label).forEach(([label_name, content]) => {
// new image
let new_image = {
"label": label_name
};
// save labels
labels.push(label_name);
if (!fs.existsSync(resized_path + folder + '/' + label_name)) {
fs.mkdirSync(resized_path + folder + '/' + label_name);
}
// saving images
for (let image in content) {
if (content.hasOwnProperty(image)) {
// console.log(`Resizing image: ${image}`)
// save image path
new_image.path = resized_path + folder + '/' + label_name + '/' + image;
sharp(path.join(data_path + folder + '/' + label_name, '/' + image))
.resize(IMG_HEIGHT, IMG_WIDTH)
.jpeg({
quality: 90
}).toFile(path.join(resized_path + folder + '/' + label_name, '/' + image));
images.push(new_image)
}
}
});
});
};
// remove dublicate labels
uniq_labels = [...new Set(labels)];
// console.log(images)
// results:
// { label: 'cats',
// path: '/Users/mac/MachineLearning/cats-dogs/resized/train/cats/cat.999.jpg'
// }, {
// label: 'cats',
// path: '/Users/mac/MachineLearning/cats-dogs/resized/train/cats/cat.999.jpg'
// } ...more lines
}
how do I load and and pass this data to tensorflow.js?