key Bindings are added

This commit is contained in:
Falko Victor Habel 2024-03-11 09:50:18 +01:00
parent 1d93b751e3
commit 993c1c5309
6 changed files with 88 additions and 20 deletions

33
main.py
View File

@ -4,14 +4,14 @@ from PIL import Image
import scripts.get_sys_info as system_code import scripts.get_sys_info as system_code
from scripts.ClosePopup import ClosePopup from scripts.ClosePopup import ClosePopup
from scripts.settings import Settings from scripts.Settings import Settings
from scripts.Converter import Converter from scripts.Converter import Converter
from scripts.Labeling import Labeling from scripts.Labeling import Labeling
from scripts.folder_mangement.OpenFolder import OpenFolder from scripts.folder_mangement.OpenFolder import OpenFolder
from scripts.folder_mangement.CreateFolder import CreateFolder from scripts.folder_mangement.CreateFolder import CreateFolder
from scripts.folder_mangement.SwitchFolder import SwitchFolder from scripts.folder_mangement.SwitchFolder import SwitchFolder
from scripts.SaveData import SaveData
SELECTION_TEXT = "Select your Directory and your Export Directory" SELECTION_TEXT = "Select your Directory and your Export Directory"
@ -42,7 +42,6 @@ class App(Ctk.CTk):
self.current_labeling_frame = None self.current_labeling_frame = None
self.last_opened_labeling_frame = None self.last_opened_labeling_frame = None
self.window_informaton_save = () self.window_informaton_save = ()
# Top Menu # Top Menu
# Widgets # Widgets
self.settings_btn = Ctk.CTkButton(self, text="Settings", width=100, command=self.open_settings, font=self.my_font) self.settings_btn = Ctk.CTkButton(self, text="Settings", width=100, command=self.open_settings, font=self.my_font)
@ -53,7 +52,8 @@ class App(Ctk.CTk):
# Alignment # Alignment
self.place_top_menu() self.place_top_menu()
self.show_main_menu() self.show_main_menu()
self.bind("<Escape>", self.go_back)
# Open Main UI initially # Open Main UI initially
def show_main_menu(self): def show_main_menu(self):
self.window_information.configure(text="Choose if you want to convert or label DATA") self.window_information.configure(text="Choose if you want to convert or label DATA")
@ -83,13 +83,19 @@ class App(Ctk.CTk):
def open_current_labeling_ui(self): def open_current_labeling_ui(self):
self.active_frame.place_forget() self.active_frame.place_forget()
if self.active_frame != self.buttons_frame:
self.active_frame.disable_keybinding()
if self.current_labeling_frame == None: if self.current_labeling_frame == None:
self.window_information.configure(text=SELECTION_TEXT) self.window_information.configure(text=SELECTION_TEXT)
self.Switch_folder = SwitchFolder(master=self, callback=self.open_labeling) self.Switch_folder = SwitchFolder(master=self, callback=self.open_labeling)
self.current_labeling_frame = self.Switch_folder self.current_labeling_frame = self.Switch_folder
self.current_labeling_frame.place(relx=0.5, rely=0.52,relwidth=0.95, relheight=0.85, anchor="center") self.current_labeling_frame.place(relx=0.5, rely=0.52,relwidth=0.95, relheight=0.85, anchor="center")
else:
else:
self.current_labeling_frame.place(relx=0.5, rely=0.52,relwidth=0.95, relheight=0.85, anchor="center") self.current_labeling_frame.place(relx=0.5, rely=0.52,relwidth=0.95, relheight=0.85, anchor="center")
self.current_labeling_frame.enable_keybinding()
self.active_frame = self.current_labeling_frame self.active_frame = self.current_labeling_frame
self.last_frame = self.buttons_frame self.last_frame = self.buttons_frame
@ -98,6 +104,7 @@ class App(Ctk.CTk):
self.last_opened_labeling_frame = self.current_labeling_frame self.last_opened_labeling_frame = self.current_labeling_frame
self.last_frame = self.current_labeling_frame self.last_frame = self.current_labeling_frame
self.current_labeling_frame.place_forget() self.current_labeling_frame.place_forget()
self.current_labeling_frame.disable_keybinding()
self.labeling = Labeling(master=self, img_paths=img_paths, output_path=output, callback=self.open_folder_uis, window_information=self.change_window_information) self.labeling = Labeling(master=self, img_paths=img_paths, output_path=output, callback=self.open_folder_uis, window_information=self.change_window_information)
self.current_labeling_frame = self.labeling self.current_labeling_frame = self.labeling
self.active_frame = self.current_labeling_frame self.active_frame = self.current_labeling_frame
@ -107,6 +114,7 @@ class App(Ctk.CTk):
self.last_opened_labeling_frame = self.current_labeling_frame self.last_opened_labeling_frame = self.current_labeling_frame
self.last_frame = self.current_labeling_frame self.last_frame = self.current_labeling_frame
self.current_labeling_frame.place_forget() self.current_labeling_frame.place_forget()
self.current_labeling_frame.disable_keybinding()
self.labeling.update_active_output_path(output) self.labeling.update_active_output_path(output)
self.current_labeling_frame = self.labeling self.current_labeling_frame = self.labeling
self.active_frame = self.current_labeling_frame self.active_frame = self.current_labeling_frame
@ -116,6 +124,7 @@ class App(Ctk.CTk):
self.last_opened_labeling_frame = self.current_labeling_frame self.last_opened_labeling_frame = self.current_labeling_frame
self.last_frame = self.current_labeling_frame self.last_frame = self.current_labeling_frame
self.current_labeling_frame.place_forget() self.current_labeling_frame.place_forget()
self.current_labeling_frame.disable_keybinding()
if variant == 0: if variant == 0:
self.window_information.configure(text="Create new folder in active directory") self.window_information.configure(text="Create new folder in active directory")
self.create_folder = CreateFolder(master=self,output_path=output, callback=self.reopen_labeling) self.create_folder = CreateFolder(master=self,output_path=output, callback=self.reopen_labeling)
@ -149,6 +158,8 @@ class App(Ctk.CTk):
elif self.active_frame == self.open_folder: elif self.active_frame == self.open_folder:
self.last_frame = self.open_folder self.last_frame = self.open_folder
self.active_frame.place_forget() self.active_frame.place_forget()
if self.active_frame != self.buttons_frame:
self.active_frame.disable_keybinding()
self.window_information.configure(text="Settings") self.window_information.configure(text="Settings")
self.last_opened_labeling_frame = self.current_labeling_frame self.last_opened_labeling_frame = self.current_labeling_frame
self.active_frame = self.settings_frame self.active_frame = self.settings_frame
@ -161,13 +172,14 @@ class App(Ctk.CTk):
self.active_frame = self.converter_frame self.active_frame = self.converter_frame
self.place_ui() self.place_ui()
def go_back(self): def go_back(self, event = None):
if self.active_frame == self.buttons_frame: if self.active_frame == self.buttons_frame:
return return
elif self.active_frame == self.settings_frame: elif self.active_frame == self.settings_frame:
system_code.save_setting_change() system_code.save_setting_change()
if self.last_frame != self.buttons_frame: if self.last_frame != self.buttons_frame:
self.active_frame.place_forget() self.active_frame.place_forget()
self.active_frame.disable_keybinding()
self.active_frame = self.last_frame self.active_frame = self.last_frame
self.last_frame = None self.last_frame = None
self.place_ui() self.place_ui()
@ -179,17 +191,20 @@ class App(Ctk.CTk):
self.switch_to_labeling() self.switch_to_labeling()
elif self.active_frame == self.Switch_folder: elif self.active_frame == self.Switch_folder:
self.active_frame.place_forget() self.active_frame.place_forget()
self.active_frame.disable_keybinding()
self.active_frame = self.buttons_frame self.active_frame = self.buttons_frame
self.last_frame = None self.last_frame = None
self.last_opened_labeling_frame = self.Switch_folder self.last_opened_labeling_frame = self.Switch_folder
self.place_ui() self.place_ui()
elif self.active_frame == self.converter_frame: elif self.active_frame == self.converter_frame:
self.active_frame.place_forget() self.active_frame.place_forget()
self.active_frame.disable_keybinding()
self.active_frame = self.buttons_frame self.active_frame = self.buttons_frame
self.last_frame = None self.last_frame = None
self.place_ui() self.place_ui()
elif self.active_frame == self.labeling: elif self.active_frame == self.labeling:
self.active_frame.place_forget() self.active_frame.place_forget()
self.active_frame.disable_keybinding()
self.active_frame = self.buttons_frame self.active_frame = self.buttons_frame
self.last_frame = None self.last_frame = None
self.last_opened_labeling_frame = self.labeling self.last_opened_labeling_frame = self.labeling
@ -207,12 +222,14 @@ class App(Ctk.CTk):
self.hide_and_prepare_last_labeling() self.hide_and_prepare_last_labeling()
self.change_window_information(None) self.change_window_information(None)
self.active_frame.place_forget() self.active_frame.place_forget()
self.active_frame.disable_keybinding()
self.active_frame = self.labeling self.active_frame = self.labeling
self.place_ui() self.place_ui()
def hide_and_prepare_last_labeling(self): def hide_and_prepare_last_labeling(self):
if self.current_labeling_frame: if self.current_labeling_frame:
self.current_labeling_frame.place_forget() self.current_labeling_frame.place_forget()
self.current_labeling_frame.disable_keybinding()
self.last_opened_labeling_frame = self.current_labeling_frame self.last_opened_labeling_frame = self.current_labeling_frame
self.current_labeling_frame = self.labeling self.current_labeling_frame = self.labeling
@ -223,7 +240,9 @@ class App(Ctk.CTk):
def place_ui(self): def place_ui(self):
self.active_frame.place(relx=0.5, rely=0.52,relwidth=0.95, relheight=0.85, anchor="center") self.active_frame.place(relx=0.5, rely=0.52,relwidth=0.95, relheight=0.85, anchor="center")
if self.active_frame != self.buttons_frame:
self.active_frame.enable_keybinding()
def close_attempt(self): def close_attempt(self):
if self.settings_frame.winfo_ismapped() or self.converter_frame.winfo_ismapped(): if self.settings_frame.winfo_ismapped() or self.converter_frame.winfo_ismapped():
self.popup = ClosePopup(master=self, callback=self.close_program) self.popup = ClosePopup(master=self, callback=self.close_program)

View File

@ -267,7 +267,6 @@ class Converter(Ctk.CTkFrame):
return percent return percent
def align(self): def align(self):
self.enable_keybinding()
# output placing # output placing
self.input_entry.place( self.input_entry.place(
relx=0.10, relx=0.10,

View File

@ -1,7 +1,6 @@
import customtkinter as Ctk import customtkinter as Ctk
import os import os
from PIL import Image, ImageTk, ImageDraw from PIL import Image, ImageTk, ImageDraw
import io
from scripts.SaveData import SaveData from scripts.SaveData import SaveData
import scripts.get_sys_info as system_code import scripts.get_sys_info as system_code
@ -36,6 +35,7 @@ class Labeling(Ctk.CTkFrame):
# bounding boxes # bounding boxes
self.labeling_boxes = [] self.labeling_boxes = []
self.data_mode_index = system_code.data_modes.index(system_code.data_mode)
self.labeling_box_index = 0 self.labeling_box_index = 0
self.original_factor = None self.original_factor = None
self.img_factor_x = None self.img_factor_x = None
@ -45,9 +45,35 @@ class Labeling(Ctk.CTkFrame):
self.mpenn_data = self.data_saver.search_in_folder(self.active_output_path) self.mpenn_data = self.data_saver.search_in_folder(self.active_output_path)
self.save_index = self.data_saver.find_highest_image_number(self.mpenn_data[0]) self.save_index = self.data_saver.find_highest_image_number(self.mpenn_data[0])
self.bind("<n>", self.save_and_load)
self.start() self.start()
def enable_keybinding(self):
"""
r: Remove the picture
j: jump in Time
l: Labeling / Resize Switch
o: Open Folder
c: Create Folder
s: Switch Folder
Return: Save Image
"""
print("gettriggered")
self.master.bind("<r>", self.delete_current_rectangle, add="+")
self.master.bind("<j>", self.skip_time, add="+")
self.master.bind("<l>", self.set_data_mode_binding, add="+")
self.master.bind("<o>", self.open_new_folder, add="+")
self.master.bind("<c>", self.create_new_folder, add="+")
self.master.bind("<s>", self.source_folder_dialog, add="+")
self.master.bind("<Return>", self.save_and_load, add="+")
def disable_keybinding(self):
self.master.unbind("<r>")
self.master.unbind("<j>")
self.master.unbind("<l>")
self.master.unbind("<o>")
self.master.unbind("<c>")
self.master.unbind("<s>")
self.master.unbind("<Return>")
def update_active_output_path(self, output_path): def update_active_output_path(self, output_path):
self.active_output_path = output_path self.active_output_path = output_path
@ -132,7 +158,7 @@ class Labeling(Ctk.CTkFrame):
def set_data_mode(self, value): def set_data_mode(self, value):
system_code.data_mode = value system_code.data_mode = value
if system_code.data_mode == "Resize": if system_code.data_mode == system_code.data_modes[0]:
self.reset_rectangle() self.reset_rectangle()
else: else:
if self.tk_cropped_image is not None: if self.tk_cropped_image is not None:
@ -140,6 +166,13 @@ class Labeling(Ctk.CTkFrame):
else: else:
self.show_img(self.img_paths[self.index]) self.show_img(self.img_paths[self.index])
def set_data_mode_binding(self, value = None):
self.data_mode_index = 1 - self.data_mode_index
system_code.data_mode = system_code.data_modes[self.data_mode_index]
self.choose_mode.set(system_code.data_mode)
print("also triggered")
def adjust_image_sizes_for_buttons(self): def adjust_image_sizes_for_buttons(self):
"""Adjust the image sizes based on the current height of the buttons and update them accordingly.""" """Adjust the image sizes based on the current height of the buttons and update them accordingly."""
@ -190,7 +223,7 @@ class Labeling(Ctk.CTkFrame):
def save_and_load(self): def save_and_load(self):
save_path = f"{self.active_output_path}/{self.save_index:03d}{system_code.img_format}" save_path = f"{self.active_output_path}/{self.save_index:03d}{system_code.img_format}"
if system_code.data_mode == "Labeling": if system_code.data_mode == system_code.data_modes[1]:
if len(self.labeling_boxes) == 0: if len(self.labeling_boxes) == 0:
self.labeled = False self.labeled = False
else: else:
@ -316,7 +349,7 @@ class Labeling(Ctk.CTkFrame):
self.start_x = event.x self.start_x = event.x
self.start_y = event.y self.start_y = event.y
# If there's an existing rectangle, remove it # If there's an existing rectangle, remove it
if self.rect and (system_code.data_mode == "Resize"): if self.rect and (system_code.data_mode == system_code.data_modes[0]):
self.delete_current_rectangle(self.rect) self.delete_current_rectangle(self.rect)
# Create an initial 1x1 rectangle that will be adjusted in `on_drag` # Create an initial 1x1 rectangle that will be adjusted in `on_drag`
self.rect = self.big_canvas.create_rectangle(self.start_x, self.start_y, self.start_x+1, self.start_y+1, self.rect = self.big_canvas.create_rectangle(self.start_x, self.start_y, self.start_x+1, self.start_y+1,
@ -342,12 +375,12 @@ class Labeling(Ctk.CTkFrame):
self.delete_current_rectangle(self.rect) self.delete_current_rectangle(self.rect)
# Start a new rectangle # Start a new rectangle
self.rect = self.big_canvas.create_rectangle(self.start_x, self.start_y, self.start_x+1, self.start_y+1, self.rect = self.big_canvas.create_rectangle(self.start_x, self.start_y, self.start_x+1, self.start_y+1,
outline=system_code.color if system_code.data_mode == "Labeling" else "#5f00c7", outline=system_code.color if system_code.data_mode == system_code.data_modes[1] else "#5f00c7",
width=system_code.thickness // self.img_factor_x if system_code.data_mode == "Labeling" else 1) width=system_code.thickness // self.img_factor_x if system_code.data_mode == system_code.data_modes[1] else 1)
self.end_x, self.end_y = event.x, event.y self.end_x, self.end_y = event.x, event.y
if system_code.data_mode == "Resize": if system_code.data_mode == system_code.data_modes[0]:
# Maintain 1:1 aspect ratio for resize mode # Maintain 1:1 aspect ratio for resize mode
delta_x = self.end_x - self.start_x delta_x = self.end_x - self.start_x
delta_y = self.end_y - self.start_y delta_y = self.end_y - self.start_y
@ -376,7 +409,7 @@ class Labeling(Ctk.CTkFrame):
self.big_canvas.coords(self.rect, self.start_x, self.start_y, self.end_x, self.end_y) self.big_canvas.coords(self.rect, self.start_x, self.start_y, self.end_x, self.end_y)
self.display_cropped_part() # New line to display the cropped area self.display_cropped_part() # New line to display the cropped area
self.dragging = False # Reset dragging flag self.dragging = False # Reset dragging flag
if system_code.data_mode == "Labeling" and self.rect is not None: if system_code.data_mode == system_code.data_modes[1] and self.rect is not None:
coordinates = [self.start_x, self.start_y, self.end_x, self.end_y] coordinates = [self.start_x, self.start_y, self.end_x, self.end_y]
coordinates = self.subtract_image_from_canvas(coordinates) coordinates = self.subtract_image_from_canvas(coordinates)
coordinates = self.multiply_array(coordinates, self.img_factor_x, self.img_factor_y) coordinates = self.multiply_array(coordinates, self.img_factor_x, self.img_factor_y)

View File

@ -29,7 +29,6 @@ class CreateFolder(Ctk.CTkFrame):
self.create_new_data_folder() self.create_new_data_folder()
def create_folder(self): def create_folder(self):
self.enable_keybinding()
self.create_folder_label = Ctk.CTkLabel(self, text="Type name for new Object:", width=100, font=self.my_font) self.create_folder_label = Ctk.CTkLabel(self, text="Type name for new Object:", width=100, font=self.my_font)
self.create_folder_entry = Ctk.CTkEntry(self, placeholder_text="New Data Object", width=100, font=self.my_font) self.create_folder_entry = Ctk.CTkEntry(self, placeholder_text="New Data Object", width=100, font=self.my_font)
self.create_folder_name_btn = Ctk.CTkButton(self, text="Create", width=100, command=self.create_new_data_folder, font=self.my_font) self.create_folder_name_btn = Ctk.CTkButton(self, text="Create", width=100, command=self.create_new_data_folder, font=self.my_font)

View File

@ -26,6 +26,16 @@ class OpenFolder(Ctk.CTkScrollableFrame):
else: else:
self.make_buttons() self.make_buttons()
self.bind("<Configure>", self.on_configure, '+') self.bind("<Configure>", self.on_configure, '+')
def enable_keybinding(self):
# this function has to be in all Functions but is only needed in
# some Classes
pass
def disable_keybinding(self):
# this function has to be in all Functions but is only needed in
# some Classes
pass
def on_configure(self, event=None): def on_configure(self, event=None):
self.rearrange_buttons() self.rearrange_buttons()

View File

@ -20,6 +20,14 @@ class SwitchFolder(Ctk.CTkFrame):
self.img_paths = None self.img_paths = None
self.create_selection() self.create_selection()
def enable_keybinding(self):
self.master.bind("<Return>", self.open_labeling, add="+")
def disable_keybinding(self):
self.master.unbind("<Return>")
# the selection part # the selection part
def create_selection(self): def create_selection(self):
# input # input
@ -33,7 +41,7 @@ class SwitchFolder(Ctk.CTkFrame):
self.error_label = Ctk.CTkLabel(self, text=self.error_txt, width=100, font=self.my_font) self.error_label = Ctk.CTkLabel(self, text=self.error_txt, width=100, font=self.my_font)
self.place_label_selection() self.place_label_selection()
def open_labeling(self): def open_labeling(self, value = None):
source_path = self.input_entry.get() source_path = self.input_entry.get()
self.output_path = self.output_entry.get() self.output_path = self.output_entry.get()
if not os.path.exists(source_path): if not os.path.exists(source_path):