From 638f4877ce4f2d65d7a190d6590496d091f79bce Mon Sep 17 00:00:00 2001 From: Leonard Kugis Date: Tue, 21 Mar 2023 19:20:48 +0100 Subject: Implemented index handling --- file-tagger.py | 16 ++++++++++++---- gui.py | 33 ++++++++++++++++++++------------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/file-tagger.py b/file-tagger.py index a36b602..6a602eb 100644 --- a/file-tagger.py +++ b/file-tagger.py @@ -78,19 +78,25 @@ def tmsu_tag(base, file, tags, untag=True): def walk(args): logger = logging.getLogger(__name__) logger.info("Walking files ...") + mime = magic.Magic(mime=True) files = [os.path.abspath(os.path.join(dp, f)) for dp, dn, filenames in os.walk(args["base"]) for f in filenames] logger.debug("Files: {}".format(files)) logger.info("Number of files found: {}".format(len(files))) + if args["index"] >= len(files): + logger.error("Invalid start index. index = {}, number of files = {}".format(args["index"], len(files))) + return + if args["predict_images"]: from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions from tensorflow.keras.preprocessing import image from tensorflow.keras.models import Model model = ResNet50(weights="imagenet") - for file_path in files: - logger.info("Handling file {}".format(file_path)) + for i in range(args["index"], len(files)): + file_path = files[i] + logger.info("Handling file {}, {}".format(i, file_path)) tags = tmsu_tags(args["base"], file_path) not_empty = bool(tags) logger.info("Existing tags: {}".format(tags)) @@ -120,7 +126,7 @@ def walk(args): if args["gui_tag"]: while(True): # For GUI inputs (rotate, ...) logger.debug("Showing image GUI ...") - ret = GuiImage(img, tags).loop() + ret = GuiImage(i, file_path, img, tags).loop() tags = set(ret[1]).difference({''}) if ret[0] == GuiImage.RETURN_ROTATE_90_CLOCKWISE: img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE) @@ -134,7 +140,7 @@ def walk(args): if args["gui_tag"]: while(True): logger.debug("Showing generic tagging GUI ...") - ret = GuiTag(file_path, tags).loop() + ret = GuiTag(i, file_path, tags).loop() tags = set(ret[1]).difference({''}) if ret[0] == GuiTag.RETURN_NEXT: break @@ -156,6 +162,7 @@ if __name__ == "__main__": 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('--gui-tag', nargs='?', const=1, default=False, type=bool, help='Show GUI for tagging (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('-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() @@ -176,6 +183,7 @@ if __name__ == "__main__": "predict_images_top": args.predict_images_top, "gui_tag": args.gui_tag, "open_system": args.open_system, + "index": args.index, "verbosity": args.verbose } diff --git a/gui.py b/gui.py index 6b6a4a2..c5920d2 100644 --- a/gui.py +++ b/gui.py @@ -14,6 +14,7 @@ class GuiMain(object): self.__predict_images_top = StringVar(self.__master, value=str(args["predict_images_top"])) self.__gui_tag = BooleanVar(self.__master, value=args["gui_tag"]) self.__open_system = BooleanVar(self.__master, value=args["open_system"]) + self.__index = StringVar(self.__master, value=str(args["index"])) validate_number = (self.__master.register(self.__validate_number)) @@ -25,7 +26,9 @@ class GuiMain(object): Entry(self.__master, textvariable=self.__predict_images_top, validate='all', validatecommand=(validate_number, '%P')).grid(row=2, column=1, columnspan=1) Checkbutton(self.__master, text="Show GUI for tagging", variable=self.__gui_tag).grid(row=3, column=0, columnspan=4, sticky=W) Checkbutton(self.__master, text="Open all files with system default", variable=self.__open_system).grid(row=4, column=0, columnspan=4, sticky=W) - Button(self.__master, text="Start", command=self.__master.destroy).grid(row=5, column=0, columnspan=4) + Label(self.__master, text="Start at index:").grid(row=5, column=0) + Entry(self.__master, textvariable=self.__index, validate='all', validatecommand=(validate_number, '%P')).grid(row=5, column=1, columnspan=1) + Button(self.__master, text="Start", command=self.__master.destroy).grid(row=6, column=0, columnspan=4) def loop(self): self.__master.mainloop() @@ -35,6 +38,7 @@ class GuiMain(object): self.__args["predict_images_top"] = int(self.__predict_images_top.get()) self.__args["gui_tag"] = self.__gui_tag.get() self.__args["open_system"] = self.__open_system.get() + self.__args["index"] = int(self.__index.get()) return self.__args def __browse(self): @@ -51,14 +55,15 @@ class GuiTag(object): RETURN_NEXT = 0 RETURN_ABORT = 1 - def __init__(self, file, tags): + def __init__(self, index, file, tags): self.__ret = self.RETURN_NEXT self.__master = Tk() self.__tags = StringVar(self.__master, value=','.join(tags)) - Label(self.__master, text="File: {}".format(file)).grid(row=0, column=0, columnspan=2) - Entry(self.__master, textvariable=self.__tags).grid(row=1, column=0, columnspan=2, sticky="we") - Button(self.__master, text="Next", command=self.__handle_next).grid(row=2, column=0) - Button(self.__master, text="Abort", command=self.__handle_abort).grid(row=2, column=1) + Label(self.__master, text="Index: {}".format(index)).grid(row=0, column=0, columnspan=2) + Label(self.__master, text="File: {}".format(file)).grid(row=1, column=0, columnspan=2) + Entry(self.__master, textvariable=self.__tags).grid(row=2, column=0, columnspan=2, sticky="we") + Button(self.__master, text="Next", command=self.__handle_next).grid(row=3, column=0) + Button(self.__master, text="Abort", command=self.__handle_abort).grid(row=3, column=1) def loop(self): self.__master.mainloop() @@ -78,19 +83,21 @@ class GuiImage(object): RETURN_ROTATE_90_CLOCKWISE = 2, RETURN_ABORT = 3 - def __init__(self, img, tags): + def __init__(self, index, file, img, tags): self.__ret = self.RETURN_NEXT self.__master = Tk() self.__tags = StringVar(self.__master, value=','.join(tags)) self.__image_pil = Image.fromarray(img) self.__image = ImageTk.PhotoImage(image=self.__image_pil) + Label(self.__master, text="Index: {}".format(index)).grid(row=0, column=0, columnspan=4) + Label(self.__master, text="File: {}".format(file)).grid(row=1, column=0, columnspan=4) self.__label = Label(self.__master, width=800, height=800, image=self.__image) - self.__label.grid(row=0, column=0, columnspan=4) - Entry(self.__master, textvariable=self.__tags).grid(row=1, column=0, columnspan=4, sticky="we") - Button(self.__master, text="↺", command=self.__handle_rotate_90_counterclockwise).grid(row=2, column=0) - Button(self.__master, text="↻", command=self.__handle_rotate_90_clockwise).grid(row=2, column=1) - Button(self.__master, text="Next", command=self.__handle_next).grid(row=2, column=2) - Button(self.__master, text="Abort", command=self.__handle_abort).grid(row=2, column=3) + self.__label.grid(row=2, column=0, columnspan=4) + Entry(self.__master, textvariable=self.__tags).grid(row=3, column=0, columnspan=4, sticky="we") + Button(self.__master, text="↺", command=self.__handle_rotate_90_counterclockwise).grid(row=4, column=0) + Button(self.__master, text="↻", command=self.__handle_rotate_90_clockwise).grid(row=4, column=1) + Button(self.__master, text="Next", command=self.__handle_next).grid(row=4, column=2) + Button(self.__master, text="Abort", command=self.__handle_abort).grid(row=4, column=3) def loop(self): self.__master.mainloop() -- cgit v1.2.1