image-dataset
Version:
Tool to build image dataset: collect, classify, review
50 lines (49 loc) • 1.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.main = main;
const fs_1 = require("@beenotung/tslib/fs");
const path_1 = require("path");
const tensorflow_helpers_1 = require("tensorflow-helpers");
const db_1 = require("./db");
const timer_1 = require("@beenotung/tslib/timer");
async function main() {
console.log('rename by content hash: scanning directories...');
await scanDir2('dataset');
await scanDir2('classified');
await scanDir('unclassified');
console.log('rename by content hash: done.');
}
async function scanDir2(dir) {
let classNames = await (0, fs_1.getDirFilenames)(dir);
for (let className of classNames) {
await scanDir((0, path_1.join)(dir, className));
}
}
let rename_image = db_1.db.prepare(/* sql */ `
update image
set filename = :new_filename
where filename = :old_filename
`);
async function scanDir(dir) {
let filenames = await (0, fs_1.getDirFilenames)(dir);
filenames = filenames.filter(filename => !(0, tensorflow_helpers_1.isContentHash)(filename));
if (filenames.length == 0) {
return;
}
let timer = (0, timer_1.startTimer)('scan dir ' + JSON.stringify(dir));
timer.setEstimateProgress(filenames.length);
for (let filename of filenames) {
let file = (0, path_1.join)(dir, filename);
let newFile = await (0, tensorflow_helpers_1.renameFileByContentHash)(file);
let newFilename = (0, path_1.basename)(newFile);
rename_image.run({
new_filename: newFilename,
old_filename: filename,
});
timer.tick();
}
timer.end();
}
if (process.argv[1] == __filename) {
main();
}