aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeonard Kugis <leonard@kug.is>2023-03-31 03:16:10 +0200
committerLeonard Kugis <leonard@kug.is>2023-03-31 03:16:10 +0200
commita76c7a1839573052ea7694ea63aea31a6cd137f7 (patch)
tree770d9eab0e61f8f95d61b6d633b184461de26bce
parent8d026f6a9eba0bc3905b945543f0e88a19c5f5cc (diff)
Added options to choose prediction backend and skip tagged files.
-rw-r--r--file-tagger.py21
-rw-r--r--gui.py34
2 files changed, 39 insertions, 16 deletions
diff --git a/file-tagger.py b/file-tagger.py
index 70909c8..36953ec 100644
--- a/file-tagger.py
+++ b/file-tagger.py
@@ -30,8 +30,15 @@ def walk(args):
return
if args["predict_images"]:
- #predictor = Predictor(Predictor.BackendTorch(top=args["predict_images_top"]))
- predictor = Predictor(Predictor.BackendTensorflow(top=args["predict_images_top"], detail=(not args["predict_images_skip_detail"]), detail_factor=args["predict_images_detail_factor"]))
+ backend = {
+ "torch": Predictor.BackendTorch,
+ "tensorflow": Predictor.BackendTensorflow,
+ "keras": Predictor.BackendTensorflow
+ }.get(args["predict_images_backend"])
+ if backend == Predictor.BackendTorch:
+ predictor = Predictor(Predictor.BackendTorch(top=args["predict_images_top"]))
+ elif backend == Predictor.BackendTensorflow:
+ predictor = Predictor(Predictor.BackendTensorflow(top=args["predict_images_top"], detail=(not args["predict_images_skip_detail"]), detail_factor=args["predict_images_detail_factor"]))
for i in range(args["index"], len(files)):
file_path = files[i]
@@ -40,6 +47,10 @@ def walk(args):
not_empty = bool(tags)
logger.info("Existing tags: {}".format(tags))
+ if (not_empty and args["skip_tagged"]):
+ logger.info("Already tagged, skipping.")
+ continue
+
if args["open_system"]:
open_system(file_path)
@@ -95,13 +106,15 @@ if __name__ == "__main__":
parser.add_argument('-f', '--file-dir', nargs='?', default='.', type=dir_path, help='File directory for walking (default: %(default)s)')
parser.add_argument('-g', '--gui', nargs='?', const=1, default=False, type=bool, help='Show main GUI (default: %(default)s)')
parser.add_argument('--predict-images', nargs='?', const=1, default=False, type=bool, help='Use prediction for image tagging (default: %(default)s)')
+ parser.add_argument('--predict-images-backend', nargs='?', const=1, choices=["torch", "tensorflow", "keras"], default="torch", type=str.lower, help='Determines which backend should be used for keyword prediction (default: %(default)s)')
parser.add_argument('--predict-images-top', nargs='?', const=1, default=10, type=int, help='Defines how many top prediction keywords should be used (default: %(default)s)')
parser.add_argument('--predict-images-detail-factor', nargs='?', const=1, default=2, type=int, help='Width factor for detail scan, multiplied by 224 for ResNet50 (default: %(default)s)')
parser.add_argument('--predict-images-skip-detail', nargs='?', const=1, default=False, type=bool, help='Skip detail scan in image prediction (default: %(default)s)')
parser.add_argument('--gui-tag', nargs='?', const=1, default=False, type=bool, help='Show GUI for tagging (default: %(default)s)')
parser.add_argument('--gui-image-length', nargs='?', const=1, default=800, type=int, help='Length of longest side for preview (default: %(default)s)')
parser.add_argument('--open-system', nargs='?', const=1, default=False, type=bool, help='Open all files with system default (default: %(default)s)')
- parser.add_argument('-s', '--skip-prompt', nargs='?', const=1, default=False, type=bool, help='Skip prompt for file tags (default: %(default)s)')
+ parser.add_argument('--skip-prompt', nargs='?', const=1, default=False, type=bool, help='Skip prompt for file tags (default: %(default)s)')
+ parser.add_argument('--skip-tagged', nargs='?', const=1, default=False, type=bool, help='Skip already tagged files (default: %(default)s)')
parser.add_argument('-i', '--index', nargs='?', const=1, default=0, type=int, help='Start tagging at the given file index (default: %(default)s)')
parser.add_argument('-v', '--verbose', action="count", default=0, help="Verbosity level")
args = parser.parse_args()
@@ -121,6 +134,7 @@ if __name__ == "__main__":
"file_dir": args.file_dir,
"gui": args.gui,
"predict_images": args.predict_images,
+ "predict_images_backend": args.predict_images_backend,
"predict_images_top": args.predict_images_top,
"predict_images_detail_factor": args.predict_images_detail_factor,
"predict_images_skip_detail": args.predict_images_skip_detail,
@@ -128,6 +142,7 @@ if __name__ == "__main__":
"gui_image_length": args.gui_image_length,
"open_system": args.open_system,
"skip_prompt": args.skip_prompt,
+ "skip_tagged": args.skip_tagged,
"index": args.index,
"verbosity": args.verbose
}
diff --git a/gui.py b/gui.py
index f20801e..7bd30ad 100644
--- a/gui.py
+++ b/gui.py
@@ -1,5 +1,6 @@
from tkinter import *
from tkinter import filedialog
+from tkinter import ttk
from PIL import Image, ImageTk
import logging
from enum import Enum
@@ -13,6 +14,7 @@ class GuiMain(object):
self.__base = StringVar(self.__master, value=args["base"])
self.__file_dir = StringVar(self.__master, value=args["file_dir"])
self.__predict_images = BooleanVar(self.__master, value=args["predict_images"])
+ self.__predict_images_backend = StringVar(self.__master, value=str(args["predict_images_backend"]))
self.__predict_images_top = StringVar(self.__master, value=str(args["predict_images_top"]))
self.__predict_images_skip_detail = BooleanVar(self.__master, value=args["predict_images_skip_detail"])
self.__predict_images_detail_factor = StringVar(self.__master, value=str(args["predict_images_detail_factor"]))
@@ -20,6 +22,7 @@ class GuiMain(object):
self.__gui_image_length = StringVar(self.__master, value=str(args["gui_image_length"]))
self.__open_system = BooleanVar(self.__master, value=args["open_system"])
self.__skip_prompt = BooleanVar(self.__master, value=args["skip_prompt"])
+ self.__skip_tagged = BooleanVar(self.__master, value=args["skip_tagged"])
self.__index = StringVar(self.__master, value=str(args["index"]))
validate_number = (self.__master.register(self.__validate_number))
@@ -31,19 +34,22 @@ class GuiMain(object):
Entry(self.__master, textvariable=self.__file_dir).grid(row=1, column=1, columnspan=2)
Button(self.__master, text="Browse", command=lambda: self.__browse(self.__file_dir)).grid(row=1, column=3)
Checkbutton(self.__master, text="Use prediction for image tagging", variable=self.__predict_images).grid(row=2, column=0, columnspan=4, sticky=W)
- Label(self.__master, text="Number of top results:").grid(row=3, column=0)
- Entry(self.__master, textvariable=self.__predict_images_top, validate='all', validatecommand=(validate_number, '%P')).grid(row=3, column=1, columnspan=1)
- Checkbutton(self.__master, text="Skip detail scan in image prediction", variable=self.__predict_images_skip_detail).grid(row=4, column=0, columnspan=4, sticky=W)
- Label(self.__master, text="Width factor for detail scan:").grid(row=5, column=0)
- Entry(self.__master, textvariable=self.__predict_images_detail_factor, validate='all', validatecommand=(validate_number, '%P')).grid(row=5, column=1, columnspan=1)
- Checkbutton(self.__master, text="Show GUI for tagging", variable=self.__gui_tag).grid(row=6, column=0, columnspan=4, sticky=W)
- Label(self.__master, text="Image GUI preview size:").grid(row=7, column=0)
- Entry(self.__master, textvariable=self.__gui_image_length, validate='all', validatecommand=(validate_number, '%P')).grid(row=7, column=1, columnspan=1)
- Checkbutton(self.__master, text="Open all files with system default", variable=self.__open_system).grid(row=8, column=0, columnspan=4, sticky=W)
- Checkbutton(self.__master, text="Skip prompt for file tags", variable=self.__skip_prompt).grid(row=9, column=0, columnspan=4, sticky=W)
- Label(self.__master, text="Start at index:").grid(row=10, column=0)
- Entry(self.__master, textvariable=self.__index, validate='all', validatecommand=(validate_number, '%P')).grid(row=10, column=1, columnspan=1)
- Button(self.__master, text="Start", command=self.__master.destroy).grid(row=11, column=0, columnspan=4)
+ Label(self.__master, text="Image prediction backend:").grid(row=3, column=0)
+ ttk.Combobox(self.__master, textvariable=self.__predict_images_backend, values=("torch", "tensorflow", "keras"), state="readonly").grid(row=3, column=1)
+ Label(self.__master, text="Number of top results:").grid(row=4, column=0)
+ Entry(self.__master, textvariable=self.__predict_images_top, validate='all', validatecommand=(validate_number, '%P')).grid(row=4, column=1, columnspan=1)
+ Checkbutton(self.__master, text="Skip detail scan in image prediction", variable=self.__predict_images_skip_detail).grid(row=5, column=0, columnspan=4, sticky=W)
+ Label(self.__master, text="Width factor for detail scan:").grid(row=6, column=0)
+ Entry(self.__master, textvariable=self.__predict_images_detail_factor, validate='all', validatecommand=(validate_number, '%P')).grid(row=6, column=1, columnspan=1)
+ Checkbutton(self.__master, text="Show GUI for tagging", variable=self.__gui_tag).grid(row=7, column=0, columnspan=4, sticky=W)
+ Label(self.__master, text="Image GUI preview size:").grid(row=8, column=0)
+ Entry(self.__master, textvariable=self.__gui_image_length, validate='all', validatecommand=(validate_number, '%P')).grid(row=8, column=1, columnspan=1)
+ Checkbutton(self.__master, text="Open all files with system default", variable=self.__open_system).grid(row=9, column=0, columnspan=4, sticky=W)
+ Checkbutton(self.__master, text="Skip prompt for file tags", variable=self.__skip_prompt).grid(row=10, column=0, columnspan=4, sticky=W)
+ Checkbutton(self.__master, text="Skip already tagged files", variable=self.__skip_tagged).grid(row=11, column=0, columnspan=4, sticky=W)
+ Label(self.__master, text="Start at index:").grid(row=12, column=0)
+ Entry(self.__master, textvariable=self.__index, validate='all', validatecommand=(validate_number, '%P')).grid(row=12, column=1, columnspan=1)
+ Button(self.__master, text="Start", command=self.__master.destroy).grid(row=13, column=0, columnspan=4)
def loop(self):
self.__master.mainloop()
@@ -51,6 +57,7 @@ class GuiMain(object):
self.__args["base"] = self.__base.get()
self.__args["file_dir"] = self.__file_dir.get()
self.__args["predict_images"] = self.__predict_images.get()
+ self.__args["predict_images_backend"] = self.__predict_images_backend.get()
self.__args["predict_images_top"] = int(self.__predict_images_top.get())
self.__args["predict_images_skip_detail"] = self.__predict_images_skip_detail.get()
self.__args["predict_images_detail_factor"] = int(self.__predict_images_detail_factor.get())
@@ -58,6 +65,7 @@ class GuiMain(object):
self.__args["gui_image_length"] = int(self.__gui_image_length.get())
self.__args["open_system"] = self.__open_system.get()
self.__args["skip_prompt"] = self.__skip_prompt.get()
+ self.__args["skip_tagged"] = self.__skip_tagged.get()
self.__args["index"] = int(self.__index.get())
return self.__args