Fix bug in Annotation UI Parking Management (#11647)

Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
Muhammad Rizwan Munawar 2024-05-05 17:31:36 +05:00 committed by GitHub
parent 03da2944d7
commit bbbd92b1fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -17,7 +17,20 @@ class ParkingPtsSelection:
"""Initializes the UI for selecting parking zone points in a tkinter window.""" """Initializes the UI for selecting parking zone points in a tkinter window."""
self.master = master self.master = master
master.title("Ultralytics Parking Zones Points Selector") master.title("Ultralytics Parking Zones Points Selector")
self.initialize_ui()
# Resizable false
master.resizable(False, False)
# Setup canvas for image display
self.canvas = tk.Canvas(master, bg="white")
# Setup buttons
button_frame = tk.Frame(master)
button_frame.pack(side=tk.TOP)
tk.Button(button_frame, text="Upload Image", command=self.upload_image).grid(row=0, column=0)
tk.Button(button_frame, text="Remove Last BBox", command=self.remove_last_bounding_box).grid(row=0, column=1)
tk.Button(button_frame, text="Save", command=self.save_to_json).grid(row=0, column=2)
# Initialize properties # Initialize properties
self.image_path = None self.image_path = None
@ -33,21 +46,6 @@ class ParkingPtsSelection:
self.canvas_max_width = 1280 self.canvas_max_width = 1280
self.canvas_max_height = 720 self.canvas_max_height = 720
def initialize_ui(self):
"""Setup UI components."""
# Setup buttons
button_frame = tk.Frame(self.master)
button_frame.pack(side=tk.TOP)
tk.Button(button_frame, text="Upload Image", command=self.upload_image).grid(row=0, column=0)
tk.Button(button_frame, text="Remove Last BBox", command=self.remove_last_bounding_box).grid(row=0, column=1)
tk.Button(button_frame, text="Save", command=self.save_to_json).grid(row=0, column=2)
# Setup canvas for image display
self.canvas = tk.Canvas(self.master, bg="white")
self.canvas.pack(side=tk.BOTTOM)
self.canvas.bind("<Button-1>", self.on_canvas_click)
def upload_image(self): def upload_image(self):
"""Upload an image and resize it to fit canvas.""" """Upload an image and resize it to fit canvas."""
self.image_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.png;*.jpg;*.jpeg")]) self.image_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.png;*.jpg;*.jpeg")])
@ -68,11 +66,18 @@ class ParkingPtsSelection:
canvas_height = min(self.canvas_max_height, self.img_height) canvas_height = min(self.canvas_max_height, self.img_height)
canvas_width = int(canvas_height * aspect_ratio) canvas_width = int(canvas_height * aspect_ratio)
self.canvas.config(width=canvas_width, height=canvas_height) # Check if canvas is already initialized
if self.canvas:
self.canvas.destroy() # Destroy previous canvas
self.canvas = tk.Canvas(self.master, bg="white", width=canvas_width, height=canvas_height)
resized_image = self.image.resize((canvas_width, canvas_height), Image.LANCZOS) resized_image = self.image.resize((canvas_width, canvas_height), Image.LANCZOS)
self.canvas_image = ImageTk.PhotoImage(resized_image) self.canvas_image = ImageTk.PhotoImage(resized_image)
self.canvas.create_image(0, 0, anchor=tk.NW, image=self.canvas_image) self.canvas.create_image(0, 0, anchor=tk.NW, image=self.canvas_image)
self.canvas.pack(side=tk.BOTTOM)
self.canvas.bind("<Button-1>", self.on_canvas_click)
# Reset bounding boxes and current box # Reset bounding boxes and current box
self.bounding_boxes = [] self.bounding_boxes = []
self.current_box = [] self.current_box = []
@ -80,6 +85,9 @@ class ParkingPtsSelection:
def on_canvas_click(self, event): def on_canvas_click(self, event):
"""Handle mouse clicks on canvas to create points for bounding boxes.""" """Handle mouse clicks on canvas to create points for bounding boxes."""
self.current_box.append((event.x, event.y)) self.current_box.append((event.x, event.y))
x0, y0 = event.x - 3, event.y - 3
x1, y1 = event.x + 3, event.y + 3
self.canvas.create_oval(x0, y0, x1, y1, fill="red")
if len(self.current_box) == 4: if len(self.current_box) == 4:
self.bounding_boxes.append(self.current_box) self.bounding_boxes.append(self.current_box)
@ -87,7 +95,13 @@ class ParkingPtsSelection:
self.current_box = [] self.current_box = []
def draw_bounding_box(self, box): def draw_bounding_box(self, box):
"""Draw bounding box on canvas.""" """
Draw bounding box on canvas.
Args:
box(list): Bounding box data
"""
for i in range(4): for i in range(4):
x1, y1 = box[i] x1, y1 = box[i]
x2, y2 = box[(i + 1) % 4] x2, y2 = box[(i + 1) % 4]
@ -115,7 +129,6 @@ class ParkingPtsSelection:
height_scaling_factor = self.img_height / canvas_height height_scaling_factor = self.img_height / canvas_height
bounding_boxes_data = [] bounding_boxes_data = []
for box in self.bounding_boxes: for box in self.bounding_boxes:
print("Bounding Box ", bounding_boxes_data)
rescaled_box = [] rescaled_box = []
for x, y in box: for x, y in box:
rescaled_x = int(x * width_scaling_factor) rescaled_x = int(x * width_scaling_factor)