image-classifier-ts
Version:
Command line tool to auto-classify images, renaming them with appropriate labels. Uses Node and Google Vision API.
123 lines • 5.32 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArgsParser = void 0;
var DefaultArgs_1 = require("./DefaultArgs");
var UsageText_1 = require("./UsageText");
var NUM_MANDATORY_ARGS = 4;
var HelpNeededError = /** @class */ (function (_super) {
__extends(HelpNeededError, _super);
function HelpNeededError() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.name = "HelpNeededError";
return _this;
}
return HelpNeededError;
}(Error));
var ArgsParser;
(function (ArgsParser) {
function getArgs() {
try {
var args = DefaultArgs_1.DefaultArgs.getDefault();
if (process.argv.length < NUM_MANDATORY_ARGS) {
throw new HelpNeededError("Incorrect arguments");
}
updateConfigFromArgs(args);
updateConfigFromOptionalArgs(args);
return args;
}
catch (error) {
if (error.name === "HelpNeededError") {
UsageText_1.UsageText.showUsage();
return null;
}
console.error(error);
return null;
}
}
ArgsParser.getArgs = getArgs;
function updateConfigFromArgs(args) {
args.imageInputDir = process.argv[2];
args.imageOutputDir = process.argv[3];
}
function updateConfigFromOptionalArgs(args) {
var _loop_1 = function (i) {
var optionArg = process.argv[i];
var optionParts = optionArg.split("=");
var option = optionParts[0].trim();
var value = optionParts.length > 1 ? optionParts[1].trim() : null;
var assertHasValue = function (message) {
if (!value) {
throw new Error(message);
}
return value;
};
var assertHasNumericValue = function (message, min, max) {
var textValue = assertHasValue(message);
var numberValue = Number.parseInt(textValue, 10);
if (!isFinite(numberValue)) {
throw new Error(message);
}
if (numberValue < min || numberValue > max) {
throw new Error(message);
}
return numberValue;
};
switch (option) {
case "-derivedLocationFormat":
args.options.derivedLocationFormat = assertHasValue("derivedLocationFormat must have a value, like derivedLocationFormat={country}_{area1}_{area2}_{area3}");
break;
case "-dryRun":
args.options.dryRun = true;
break;
case "-filenameFormat":
args.options.filenameFormat = assertHasValue("filenameFormat must have a value, like filenameFormat={year}/{topLabel}/{combinedLabels}--{filename}");
break;
case "-h":
case "-help":
throw new HelpNeededError();
case "-geoCode":
args.options.geoCode = true;
break;
case "-locationFormat":
args.options.locationFormat = assertHasValue("locationFormat must have a value, like locationFormat={country}_{area1}_{area2}_{area3}");
break;
case "-minScore":
args.options.minScore = assertHasNumericValue("minScore must have a value, like: minScore=0.8", 0, 1);
break;
case "-replaceOnMove":
args.options.replaceOnMove = true;
break;
case "-topNLabels":
args.options.topNLabels = assertHasNumericValue("topNLabels must have a value, like: topNLabels=2", 1, 10);
break;
case "-verbosity":
var verbosityNumber = assertHasNumericValue("verbosity must have a value (1-3), like: verbosity=2", 1, 3);
args.options.verbosity = verbosityNumber;
break;
case "":
break;
default:
throw new Error("unrecognised option ".concat(optionArg));
}
};
for (var i = NUM_MANDATORY_ARGS; i < process.argv.length; i++) {
_loop_1(i);
}
}
})(ArgsParser = exports.ArgsParser || (exports.ArgsParser = {}));
//# sourceMappingURL=ArgsParser.js.map