image2tags
Version:
An npm package that generates keywords from an image
83 lines (82 loc) • 3.17 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const index_1 = require("./index");
const path = __importStar(require("path"));
const program = new commander_1.Command();
program
.version('1.0.0')
.description('A CLI tool to get tags for a given image.')
.argument('<imagePath>', 'Path to the image file.')
.option('-k, --topK <number>', 'Number of top predictions to return', '10')
.option('--json', 'Output results in JSON format')
.action(async (imagePath, options) => {
try {
const fullPath = path.resolve(imagePath);
const topK = parseInt(options.topK, 10);
if (!options.json) {
console.log('Processing image...');
}
const tags = await (0, index_1.getImageTags)(fullPath, topK);
if (options.json) {
console.log(JSON.stringify(tags, null, 2));
}
else {
console.log('\nDetected Tags:');
tags.forEach(tag => {
const probability = (tag.probability * 100).toFixed(2);
console.log(`- ${tag.className} (Probability: ${probability}%)`);
});
}
}
catch (error) {
if (options.json) {
const errorMessage = error instanceof Error ? error.message : 'An unknown error occurred.';
console.error(JSON.stringify({ error: 'Could not classify the image.', details: errorMessage }, null, 2));
}
else {
if (error instanceof Error) {
console.error(`\nError: ${error.message}`);
}
else {
console.error('\nAn unknown error occurred.');
}
}
process.exit(1);
}
});
program.parse(process.argv);