autotag: add option for taking list of files from a text file.

Add an option for taking the list of input files from a text file:

  ./autotag -i files.txt

This is useful in conjunction with the `find` command:

  find ~/images/ -name '*.jpg' | ./autotag -i -
This commit is contained in:
evazion
2022-06-22 17:14:40 -05:00
parent e598c7de16
commit d43ce9c4e7
+12 -3
View File
@@ -12,14 +12,23 @@ from more_itertools import ichunked
@click.option("-n", "--limit", default=50, type=int, show_default=True, help="The maximum number of tags to return per image.")
@click.option("-b", "--batch", "bs", default=128, type=int, show_default=True, help="The number of images to process per batch.")
@click.option("-c", "--csv", is_flag=True, help="Output CSV instead of JSON.")
@click.option("-i", "--input-file", type=click.File(), help="Read list of image filenames from text file.")
@click.option("-g/-f", "--group-tags/--flatten-tags", default=True, show_default=True, help="Output rows in {filename, tags} format or {filename, tag, score} format.")
@click.option("-N", "--name-only", is_flag=True, help="Output only the filename without the full path or extension.")
@click.option("-m", "--model", default="models/model.pth", type=click.Path(exists=True), show_default=True, help="The model to use.")
@click.argument("files", nargs=-1, type=click.Path(exists=True, allow_dash=True, path_type=Path), required=True)
def main(files, threshold, limit, bs, csv, group_tags, name_only, model):
@click.argument("files", nargs=-1, type=click.Path(exists=True, allow_dash=True, path_type=Path))
def main(files, threshold, limit, bs, csv, input_file, group_tags, name_only, model):
autotagger = Autotagger(model)
for filepaths in ichunked(get_filepaths(files), bs):
if input_file:
paths = (Path(line.rstrip()) for line in input_file)
elif len(files) > 0:
paths = get_filepaths(files)
else:
click.echo(click.get_current_context().get_help())
click.get_current_context().exit()
for filepaths in ichunked(paths, bs):
filepaths = list(filepaths)
files = [click.open_file(filepath, "rb") for filepath in filepaths]
predictions = autotagger.predict(files, threshold=threshold, limit=limit, bs=bs)