[BUG] #001 Image Reseting now fixed

This commit is contained in:
Falko Victor Habel 2024-03-29 14:40:30 +01:00
parent 58d6f1ff58
commit 6616d792da
1 changed files with 31 additions and 25 deletions

View File

@ -135,7 +135,7 @@ class Labeling(Ctk.CTkFrame):
def create_labeling(self): def create_labeling(self):
"""adapt button size to window_size""" """adapt button size to window_size"""
self.big_canvas = Ctk.CTkCanvas(self,background="#5f00c7", bd=0, highlightthickness=0) 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.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.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) 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): def set_data_mode(self, value):
system_code.data_mode = value system_code.data_mode = value
if system_code.data_mode == system_code.data_modes[0]: if system_code.data_mode == system_code.data_modes[0]:
self.reset_rectangle() self.reset_canvas()
else: else:
if self.tk_cropped_image is not None: if self.tk_cropped_image is not None:
self.show_img(self.save_cropped) self.show_img(self.save_cropped)
@ -172,7 +172,9 @@ class Labeling(Ctk.CTkFrame):
self.choose_mode.set(system_code.data_mode) self.choose_mode.set(system_code.data_mode)
print("also triggered") print("also triggered")
def reset_image(self):
self.reset_canvas()
self.show_img(self.index)
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."""
@ -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.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) self.source_folder_image = Ctk.CTkImage(self.source_folder_raw_image, size =self.image_btn_size)
def reset_rectangle(self): def reset_canvas(self):
# Check if there is a rectangle to delete """
if self.rect: Adjust the reset_canvas method to remove all rectangles or drawing objects while preserving a specific image.
self.delete_current_rectangle(self.rect) """
for rect in self.labeling_boxes: # Assuming the image you want to keep has a 'picture' tag
self.delete_current_rectangle(rect[0]) # First, remove all items tagged with 'rectangle' from the canvas
self.labeling_boxes = [] self.big_canvas.delete("rectangle")
self.labeling_box_index = 0
# 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.rect = None
self.draw = 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): 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 == system_code.data_modes[1]: 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.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.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]) os.remove(self.img_paths[self.index])
self.index += 1 self.index += 1
self.save_index += 1 self.save_index += 1
@ -244,19 +251,16 @@ class Labeling(Ctk.CTkFrame):
def draw_rects(self, image): def draw_rects(self, image):
self.draw = ImageDraw.Draw(image) self.draw = ImageDraw.Draw(image)
for rect in self.labeling_boxes: for rect_info in self.labeling_boxes:
# Each 'rect' in 'self.labeling_boxes' is expected to be a list with 2 elements: _, coordinates, color, thickness = rect_info
# 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
color = rect[2] # Draw the rectangle on the image
thickness = rect[3]
if thickness <= 0: if thickness <= 0:
thickness = 1 thickness = 1
self.draw.rectangle(coordinates, outline=color, width=thickness) self.draw.rectangle(coordinates, outline=color, width=thickness)
self.labeled = True self.labeled = True
return image return image
def delete_img(self): def delete_img(self):
os.remove(self.img_paths[self.index]) 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` # 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,
outline=system_code.color if system_code.data_mode != "Resize" else "#5f00c7", 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): def on_resize(self, event):
"""Handle window resize events with throttling.""" """Handle window resize events with throttling."""
@ -376,7 +381,8 @@ class Labeling(Ctk.CTkFrame):
# 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 == system_code.data_modes[1] 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 == 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 self.end_x, self.end_y = event.x, event.y
@ -525,7 +531,7 @@ class Labeling(Ctk.CTkFrame):
self.display_image() self.display_image()
# Clear the rectangle on the main canvas if it exists # Clear the rectangle on the main canvas if it exists
self.reset_rectangle() self.reset_canvas()
# Reset rectangle coordinates # Reset rectangle coordinates
self.start_x, self.start_y, self.end_x, self.end_y = None, None, None, None self.start_x, self.start_y, self.end_x, self.end_y = None, None, None, None