From 6616d792daf4d97471802f6e39a6f92285a6f652 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Fri, 29 Mar 2024 14:40:30 +0100 Subject: [PATCH] [BUG] #001 Image Reseting now fixed --- scripts/Labeling.py | 56 +++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/scripts/Labeling.py b/scripts/Labeling.py index 36d20fe..4a98a18 100644 --- a/scripts/Labeling.py +++ b/scripts/Labeling.py @@ -135,7 +135,7 @@ class Labeling(Ctk.CTkFrame): def create_labeling(self): """adapt button size to window_size""" self.big_canvas = Ctk.CTkCanvas(self,background="#5f00c7", bd=0, highlightthickness=0) - self.reset_btn = Ctk.CTkButton(self, text="reset Image", width=100, command=self.reset_rectangle, font=self.my_font) + self.reset_btn = Ctk.CTkButton(self, text="reset Image", width=100, command=self.reset_image, font=self.my_font) self.create_folder_btn = Ctk.CTkButton(self,image=self.create_folder_image, text="", width=100, command=self.create_new_folder, font=self.my_font) self.open_folder_btn = Ctk.CTkButton(self, image=self.open_folder_image,text="", width=100, command=self.open_new_folder, font=self.my_font) self.save_img_btn = Ctk.CTkButton(self, text="Save Image", width=100, command=self.save_and_load, font=self.my_font) @@ -159,7 +159,7 @@ class Labeling(Ctk.CTkFrame): def set_data_mode(self, value): system_code.data_mode = value if system_code.data_mode == system_code.data_modes[0]: - self.reset_rectangle() + self.reset_canvas() else: if self.tk_cropped_image is not None: self.show_img(self.save_cropped) @@ -172,7 +172,9 @@ class Labeling(Ctk.CTkFrame): self.choose_mode.set(system_code.data_mode) print("also triggered") - + def reset_image(self): + self.reset_canvas() + self.show_img(self.index) def adjust_image_sizes_for_buttons(self): """Adjust the image sizes based on the current height of the buttons and update them accordingly.""" @@ -209,18 +211,23 @@ class Labeling(Ctk.CTkFrame): self.open_folder_image = Ctk.CTkImage(self.open_folder_raw_image, size =self.image_btn_size) self.source_folder_image = Ctk.CTkImage(self.source_folder_raw_image, size =self.image_btn_size) - def reset_rectangle(self): - # Check if there is a rectangle to delete - if self.rect: - self.delete_current_rectangle(self.rect) - for rect in self.labeling_boxes: - self.delete_current_rectangle(rect[0]) - self.labeling_boxes = [] - self.labeling_box_index = 0 + def reset_canvas(self): + """ + Adjust the reset_canvas method to remove all rectangles or drawing objects while preserving a specific image. + """ + # Assuming the image you want to keep has a 'picture' tag + # First, remove all items tagged with 'rectangle' from the canvas + self.big_canvas.delete("rectangle") + + # Reset the list of rectangles since they've been removed from the canvas + self.labeling_boxes.clear() + self.labeling_box_index = 0 + + # Reset state variables self.rect = None self.draw = None - self.preview_canvas.delete("all") - + self.preview_canvas.delete("all") # Assuming you also want to clear the preview canvas + def save_and_load(self): save_path = f"{self.active_output_path}/{self.save_index:03d}{system_code.img_format}" if system_code.data_mode == system_code.data_modes[1]: @@ -235,7 +242,7 @@ class Labeling(Ctk.CTkFrame): self.resolution = self.save_cropped.size self.data_saver.append_to_json_file(save_path, self.resolution, self.labeled, self.labeling_boxes, self.mpenn_data[0]) - self.reset_rectangle() + self.reset_canvas() os.remove(self.img_paths[self.index]) self.index += 1 self.save_index += 1 @@ -244,19 +251,16 @@ class Labeling(Ctk.CTkFrame): def draw_rects(self, image): self.draw = ImageDraw.Draw(image) - for rect in self.labeling_boxes: - # Each 'rect' in 'self.labeling_boxes' is expected to be a list with 2 elements: - # The first element could be a rectangle object or identifier (we will not use it here) - # The second element is a list of coordinates in the format [start_x, start_y, end_x, end_y] - coordinates = rect[1] # Getting the coordinates list + for rect_info in self.labeling_boxes: + _, coordinates, color, thickness = rect_info - color = rect[2] - thickness = rect[3] + # Draw the rectangle on the image if thickness <= 0: thickness = 1 self.draw.rectangle(coordinates, outline=color, width=thickness) + self.labeled = True - return image + return image def delete_img(self): os.remove(self.img_paths[self.index]) @@ -354,7 +358,8 @@ class Labeling(Ctk.CTkFrame): # 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, outline=system_code.color if system_code.data_mode != "Resize" else "#5f00c7", - width=system_code.thickness // self.img_factor_x if system_code.data_mode != "Resize" else 1) + width=system_code.thickness // self.img_factor_x if system_code.data_mode != "Resize" else 1, + tags=("rectangle",)) def on_resize(self, event): """Handle window resize events with throttling.""" @@ -376,7 +381,8 @@ class Labeling(Ctk.CTkFrame): # 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, 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 == system_code.data_modes[1] else 1) + width=system_code.thickness // self.img_factor_x if system_code.data_mode == system_code.data_modes[1] else 1, + tags=("rectangle",)) self.end_x, self.end_y = event.x, event.y @@ -525,7 +531,7 @@ class Labeling(Ctk.CTkFrame): self.display_image() # Clear the rectangle on the main canvas if it exists - self.reset_rectangle() + self.reset_canvas() # Reset rectangle coordinates self.start_x, self.start_y, self.end_x, self.end_y = None, None, None, None